// JS (ONLY)

var copy = function(name, to){
	fields = new Array('name', 'company', 'addresse', 'city', 'state', 'ZIP', 'codphone', 'phone', 'codfax', 'fax', 'email', 'docnumber');
	for(x in fields){
		obj = document.getElementsByName(name+'['+fields[x]+']');
		obj = obj[0];
		objto = document.getElementsByName(to+'['+fields[x]+']');
		objto = objto[0];
		objto.value = obj.value;
	}
}

var toogle = function(categories, category){
	for(i = 0; i < categories.length; i++){
		$(categories[i]).style.display = 'none';
	}		
	$(category).style.display = 'block';
}

// CALL TO AJAX

var inputBad = function(name, error){
	obj = document.getElementsByName(name);
	obj = obj[0];
	obj.style.background = 'url('+THEME+'/icons/fail.png)';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = 'right';
	obj.style.paddingRight = '16px';
	obj.style.backgroundColor = '#ADBBD8';
	//$('FAQ').update(error);
} 
var inputGood = function(name){
	obj = document.getElementsByName(name);
	obj = obj[0];
	obj.style.background = 'url('+THEME+'/icons/success.png)';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = 'right';
	obj.style.paddingRight = '16px';
	obj.style.backgroundColor = '#FFFFFF';
}

function inputGoodId (name){
	var obj = document.getElementById(name);
	obj.style.background = 'url('+THEME+'/icons/success.png)';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = 'right';
	obj.style.paddingRight = '16px';
	obj.style.backgroundColor = '#FFFFFF';
}
function inputBadId (name){
	var obj = document.getElementById(name);
	obj.style.background = 'url('+THEME+'/icons/fail.png)';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = 'right';
	obj.style.paddingRight = '16px';
	obj.style.backgroundColor = '#ADBBD8';
}
function inputNoneId (name){
	var obj = document.getElementById(name);
	obj.style.background = 'none';
	obj.style.backgroundRepeat = 'no-repeat';
	obj.style.backgroundPosition = 'right';
	obj.style.paddingRight = '1px';
}

var checkValue = function(Npage, Nrequest, Nobj){
	query('FAQ', Npage, Nrequest, append, 0, Nobj.name, Nobj.value);
}


// AJAX

var XHR = null;

var query = function(objID, Ncategory, Nsubcategory, Naction, varfunction, stopolders, name, value){
	if(XHR != null)
		if(XHR.getStatus() != 200 && stopolders) XHR.abort();
	
	XHR = new Ajax.Request(REQUESTPAGE, 
		{
		onSuccess: function(transport){ varfunction(objID, transport.responseText); },
		onFailure: function(){ alert(messageError); },
	    onLoading: function (){ $(objID).update(loading); },
		parameters: 
			{
				category: Ncategory,
				subcategory: Nsubcategory,
				action: Naction,
				VARname: name,
				VARvalue: value
			}
		});
}

var formQuery = function(formID, objID, Ncategory, Nsubcategory, Naction, varfunction, stopolders, name, value){
	if(XHR != null)
		if(XHR.getStatus() != 200 && stopolders) XHR.abort();
	
	$(formID).action = REQUESTPAGE;
	XHR = $(formID).request({
		onSuccess: function(transport){ varfunction(objID, transport.responseText);},
		onLoading: function(){ $(objID).update(loading); },
		parameters: {
			category: Ncategory,
			subcategory: Nsubcategory,
			action: Naction,
			VARname: name,
			VARvalue: value
			},
		onFailure: function(){ varfunction(objID, "<br>FALLOS EN LA TRANSMISION DEL AJAX<br>");}
		});
}


// FUNCTIONS FOR AJAX
var assign = function(objID, text){
	//alert(text);
	$(objID).update(text);
}

var append = function(objID, text){
	$(objID).update($(objID).innerHTML+text);
}

/* funcion de apoyo para la transferencia de dominios
 * recoge un mensaje de salida tras la creaci�n de un nuevo contacto
 * y lo introduce en el formulario de tramitaci�n de transferencia.
 * Tras es lanza la petici�n Ajax de trasferencia de dominio.
 */
var transferencia = function(objID, text){
	//alert (text);
	$(objID).update(text);
	//alert(document.getElementById('domain[TLD]').value);
	if (document.getElementById('domain[TLD]').value == 'eu')
	{
		document.getElementById('contactID[]').value = document.getElementById('EURIDID').value;
		//alert ('eurid');
	}
	else
	{
		document.getElementById('contactID[]').value = document.getElementById('ICANNID').value;
		//alert ('icann');
	}	
	formQuery('transfer', 'result_transfer', 'domains', 'transfer', 'main', assign)
}

// FUNCTIONS PARA TRATAMIENTO DE CAPAS

function showDIV(divToShow)

{
	document.getElementById(divToShow).style.display = "block";
}

function hideDIV(divToShow)

{
	document.getElementById(divToShow).style.display = "none";
}

// FUNCIONES PARA CONTROL DE TRASFERENCIAS DE DOMINIOS

var valor = 1;

function actualizaValor(oRad){
	valor = oRad.value;
	}


/* funcion que controla la procedencia de la peticion de traslado de dominio, comprobando si es
 * trasferencia de un contacto nuevo o existente, y de si se trata de un dominio .com o un .es
 */
function compruebaContacto() {
	
	if (document.getElementById('contactoNuevo') != null){
		
		// Trasferencia para un .com .eu .cat
		if (document.getElementById('authcode') != null)
		{
			document.getElementById('authcode').value = escape(document.getElementById('authcodePuro').value);
		}
		
		if (valor == 1){
			// Contacto existente
			document.getElementById('contactID[]').value = document.getElementById('id_contacto').value;
			formQuery('transfer', 'result_transfer', 'domains', 'transfer', 'main', assign)
		} else {
			// Contacto nuevo
			formQuery('contact', 'result_contact', 'contacts', 'create', 'main', transferencia);
		}
	}else{
		// Trasferencia para un .es
		formQuery('transfer', 'result_transfer', 'domains', 'transfer', 'main', assign)
	}
}

// FUNCIONES DE CHEQUEO DE CAMPOS
function checkOneField(name, mins, maxs, type, www){
	
	var campoActual = document.getElementById(name);
	
	if(campoActual.value != "")
	{
	  data=campoActual.value;	
	  data=data.replace(/'/g," ");
	  dataStringCheck = "data="+data;
	  dataStringCheck += "&type="+type+"&max="+maxs+"&min="+mins+"&fname="+name;
	  
	  //alert(dataStringCheck);
	
	  new Ajax.Request(www+'/includes/webServices/FormDataChecker.php', {
		method: "post",
		asynchronous: true,
		postBody: dataStringCheck,
		onSuccess: procesaAJAXCheck,
		onFailure: procesaErrorAJAXCheck
	  });
	  
	}else{
		
		inputNoneId(name);
		
	}
	
	
	
	function procesaAJAXCheck(respuesta)
	{
		var obJSON = respuesta.responseText.evalJSON();
		eval(obJSON['JS']);
		//alert(obJSON['DATA']);
		if(obJSON['DATA'] != 'undefined')
		{
			campoActual.value = obJSON['DATA'];
		}else{
			campoActual.value = "";
		}	
	}
	
	function procesaErrorAJAXCheck(respuesta)
	{
		alert(respuesta.responseText);
	}
	
}


//AJAX PARA MYDOMAINS
function cargaMyDomains(url, id, name, tld){
	
	new Ajax.Request(www+'/includes/webServices/FormDataChecker.php', {
	method: "post",
	asynchronous: true,
	postBody: dataStringCheck,
	onSuccess: procesaAJAXCheck,
	onFailure: procesaErrorAJAXCheck
	  });
	  

	function procesaAJAXCheck(respuesta)
	{
		//alert(respuesta);		
		var obJSON = respuesta.responseText.evalJSON();
		eval(obJSON['JS']);
		if(obJSON['DATA'] != 'undefined')
		{
			campoActual.value = obJSON['DATA'];
		}else{
			campoActual.value = "";
		}	
	}
	
	function procesaErrorAJAXCheck(respuesta)
	{
		alert(respuesta.responseText);
	}
	
}


// CODIFICACION DE CARACTERES A HTML
function encodeMyHtml(string) {
	encodedHtml = escape(string);
	encodedHtml = encodedHtml.replace(/\//g,"%2F");
	encodedHtml = encodedHtml.replace(/\?/g,"%3F");
	encodedHtml = encodedHtml.replace(/=/g,"%3D");
	encodedHtml = encodedHtml.replace(/&/g,"%26");
	encodedHtml = encodedHtml.replace(/@/g,"%40");
	return encodedHtml;
}


// CONTROL DE SERVIDORES DNS

function modificarServer(id_server) {
	document.getElementById('server_'+id_server+'_response').style.display = "block";
	document.getElementById('server_'+id_server).style.display = "none";
}

function efectuarCambioServer(id_server) {
	$('server_'+id_server).update("");
	formQuery('changeserver'+id_server, 'server_'+id_server, 'domain', 'modificar', 'modifyserver', assign);
	document.getElementById('server_'+id_server+'_response').style.display = "none";
	document.getElementById('server_'+id_server).style.display = "block";
}