function limpiarOpciones(){
	var obj_Radio = document.getElementsByName('opcion_asigdom'); var i;
	for (i=0;i<obj_Radio.length;i++){
		obj_Radio[i].checked = false;
		setearOpcion(obj_Radio[i].value,'0');
		ocultarOpcion(obj_Radio[i].value);
	}
	if(xGetElementById('atencion_domextras_container').style.display != 'none'){ hide('atencion_domextras_container'); }
	xGetElementById('atencion_domextras_listado').innerHTML = '';
}

function toggleOpcion(){
	if(xGetElementById('atencion_domextras_container').style.display != 'none'){ hide('atencion_domextras_container'); }
	xGetElementById('atencion_domextras_listado').innerHTML = '';
	var obj_Radio = document.getElementsByName('opcion_asigdom'); var i,valor;
	for (i=0;i<obj_Radio.length;i++){
		valor=obj_Radio[i].value;
		if (obj_Radio[i].checked){
			setearOpcion(valor,'1');
			displayOpcion(valor);
		}else{
			setearOpcion(valor,'0');
			ocultarOpcion(valor);
		}
	}
}

function setearOpcion(id,estado){
	xGetElementById('asigdom_'+id+'_titulo').src = Images_Protocolo+':\/\/'+Images_HostName+'/'+idiomaActual+'/imagenes/asig-dom-opcion-'+id+'-'+estado+'.gif';
	setClass(xGetElementById('asigdom_'+id+'_head'),'asigdom_opcion_'+estado);
	setClass(xGetElementById('asigdom_'+id+'_desc'),'asigdom_opcion_desc_'+estado);
	setClass(xGetElementById('asigdom_'+id+'_flecha'),'asigdom_opcion_flecha_'+estado);
}

function displayOpcion(opcion){
	if(xGetElementById('BT'+opcion+'Dominio')){ xGetElementById('BT'+opcion+'Dominio').disabled = false; }
	if(xGetElementById('bloque_'+opcion).style.display == 'none'){ fadeOutIn('bloque_'+opcion,'in'); }
}

function ocultarOpcion(opcion){
	if(xGetElementById('BT'+opcion+'Dominio')){ xGetElementById('BT'+opcion+'Dominio').disabled = true; }
	if(xGetElementById('bloque_'+opcion).style.display != 'none'){ fadeOutIn('bloque_'+opcion,'out'); }
}

/************************************* BUSQUEDA DOMINIO *************************************/
function ToggleTLDS(){
	var i;
	var obj_chkTLD = document.getElementsByName('tlds[]');
	var obj_chkCTRL = xGetElementById('CheckAll');
	for (i=0;i<obj_chkTLD.length;i++){
		if(obj_chkCTRL.checked == true){ obj_chkTLD[i].checked = true;	}else{ obj_chkTLD[i].checked = false; }
	}
}

function CheckTLDS(){
	var i, cuenta=0;
	var obj_chkTLD = document.getElementsByName('tlds[]');
	for (i=0;i<obj_chkTLD.length;i++){
		if(obj_chkTLD[i].checked == true){ cuenta = cuenta + 1; }
	}
	if(cuenta >= obj_chkTLD.length){	xGetElementById('CheckAll').checked = true; }else{ xGetElementById('CheckAll').checked = false; }
}

function checkDomain(campo){
	var lastDot, nameTemp, extTemp, ext='', partes, i, ParteFinal, ValidChar=true, ch, o_campo, errorCh='';
	var reg_expTLD = /[0-9a-z]/i;

	o_campo = xGetElementById(campo);
	nameTemp = o_campo.value;
	nameTemp = nameTemp.toLowerCase();

	if((nameTemp == '')){
		alert("Ingrese un nombre dominio sin las cadena http:// o www.");
		setClass(o_campo,'campoError');
		return false;
	}

	while (ValidChar){
		if(reg_expTLD.test(nameTemp.charAt(nameTemp.length - 1))){
			ValidChar = false;
		}else{
			nameTemp = nameTemp.substring(0,nameTemp.length - 1);
		}
	}

	partes = nameTemp.split(".");

	for(i = 0; i < partes.length; i++){
		lastDot = nameTemp.lastIndexOf(".");
		extTemp = nameTemp.substring(lastDot,nameTemp.length);
		if(checkTld(extTemp)){
			ext = extTemp+ext;
			nameTemp = nameTemp.substring(0,lastDot);
		}
	}

	if(nameTemp.indexOf('http://') == 0) {
		nameTemp = nameTemp.replace("http://", "");
	}

	if(nameTemp.indexOf('https://') == 0) {
		nameTemp = nameTemp.replace("https://", "");
	}

	if(nameTemp.indexOf('http:/') == 0) {
		nameTemp = nameTemp.replace("http:/", "");
	}

	if(nameTemp.indexOf('wwww.') == 0) {
		nameTemp = nameTemp.replace("wwww.", "");
	}

	if(nameTemp.indexOf('www.') == 0) {
		nameTemp = nameTemp.replace("www.", "");
	}

	if(nameTemp.indexOf('ww.') == 0) {
		nameTemp = nameTemp.replace("ww.", "");
	}
	
	if(!CheckDomainCharacters(nameTemp)){
		errorCh = "El nombre dominio no está bien formado o tiene caracteres no permitidos.\n Utilice solamente letras, números o '-'\nElimine también las cadena http:// o www.";
	}

	if (ext.length < 2) {
		errorCh = "Por favor, ingrese un nombre de dominio con una extensión (TLD) válida.";
	}

	if(errorCh != ''){
		alert(errorCh);
			setClass(o_campo,'campoError');
		return false;
	}else{
			xGetElementById('id_plan_asociar').value = '';
			FormatearTapa('tengo_form_container');
			PonerTapa('container_busqueda');

		xGetElementById('NombreDominio').value = nameTemp;
		PonerMensaje('tapa');
		xGetElementById('atencion_domextras_listado').innerHTML = '';
		xGetElementById('whoisData').innerHTML = '';
		makeRequest('/ajax-check-dominios-promopack.php',GetDominiosExtrasResult,'POST','dom='+encodeURIComponent(nameTemp)+'&tld=.com,.net,.org');
		makeRequest('/site/sp/internacional/whois',GetWhoisResult,'POST','accion=buscarDominio&NombreDominio='+encodeURIComponent(nameTemp)+encodeURIComponent(ext));
	}
}

function GetDominiosExtrasResult(xmlhttp) {
	switch (xmlhttp.readyState){
		case 1:
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var i,ListadoHTML='',ErrorCartel='';
			var responseXML = xmlhttp.responseXML;
			var Dominios = responseXML.getElementsByTagName('dominio');
			var Errores = responseXML.getElementsByTagName('error');

			if(Errores.length > 0){
				SacarMensaje();
				SacarTapa();
				for (i=0; i<Errores.length; i++) {
					ErrorCartel += Errores[i].firstChild.nodeValue+'\n';
				}
				alert(ErrorCartel);
			}else{
				if(Dominios.length > 0){
					avail = 'N'
					for (i=0; i<Dominios.length; i++) {
						for( attr = 0; attr< Dominios.length; attr++ ){
							switch(Dominios[i].attributes[attr].nodeName){
								case 'status':
									avail = Dominios[i].attributes[attr].nodeValue;;
								break;
								case 'tld':
									availTLD = Dominios[i].attributes[attr].nodeValue;
								break;
							}
						}
						if (avail == 'S') {
								ListadoHTML += "<img src='"+Images_Protocolo+":\/\/"+Images_HostName+"/"+idiomaActual+"/imagenes/ic_item_square_green.gif' align='absmiddle'>&nbsp;"+Dominios[i].firstChild.nodeValue+availTLD+"<br />\n";
								ListadoHTML += "<input type='hidden' name='tlds[]' value='"+availTLD.replace('.','')+"'/>";
							}
					}

						xGetElementById('atencion_domextras_listado').innerHTML = ListadoHTML;
						SacarMensaje();
						SacarTapa();
					//if(xGetElementById('bloque_tengo').style.display != 'none'){ fadeOutIn('bloque_tengo','out'); }
					if (ListadoHTML.length > 0) {
						if (xGetElementById('atencion_domextras_container').style.display == 'none') {
							fadeOutIn('atencion_domextras_container', 'in');
						}
					}
				}else{
					//document.asigdom_form.submit();
				}
			}
			break;
	}
}


function GetWhoisResult(xmlhttp){
	switch (xmlhttp.readyState){
		case 1:
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var i,ListadoHTML='',ErrorCartel='';
			var responseXML = xmlhttp.responseXML;
			var WhoisData = responseXML.getElementsByTagName('whois');
			xGetElementById('whoisData').innerHTML = WhoisData[0].firstChild.nodeValue;
			if (xGetElementById('whoisDataContainer').style.display == 'none') {
						fadeOutIn('whoisDataContainer', 'in');
			}
			SacarMensaje();
			SacarTapa();
			break;
	}
}

/************************************* INI LOGIN *************************************/
function ToggleLoginBlock(destino){
	hide('chkout1_container_login_olvido_ok');
	if(destino == 'olvido'){
		LimpiarErrorLogin();
		hide('chkout1_container_login_login');
		show('chkout1_container_login_olvido');
	}
	if(destino == 'login'){
		LimpiarErrorOlvido();
		hide('chkout1_container_login_olvido');
		show('chkout1_container_login_login');
	}
}

function Loguear(Tipo){
	xGetElementById('login_form_error_desc').innerHTML = '';
	if(xGetElementById('login_form_error_container').style.display != 'none'){ hide('login_form_error_container'); }
	xGetElementById('olvido_form_error_desc').innerHTML = '';
	if(xGetElementById('olvido_form_error_container').style.display != 'none'){ hide('olvido_form_error_container'); }
	FormatearTapa('tengo_form_container_login');
	PonerTapa('tengo_form_container_login');
	PonerMensaje('tapa');
	var strPOST = new Array();
	if(Tipo == 'LOGIN'){
		strPOST.push('usernameLogin='+encodeURIComponent(GetElementFormValue(xGetElementById('usernameLoginChkout1'))));
		strPOST.push('passLogin='+encodeURIComponent(GetElementFormValue(xGetElementById('passLoginChkout1'))));
		strPOST.push('accion='+encodeURIComponent('login'));
		strPOST = strPOST.join('&');
		makeRequest('/ajax-login.php',GetLoginResult,'POST','account=old&'+strPOST+'&origen=asignacion-dominio');
		setClass(xGetElementById('usernameLoginChkout1'),'campo');
		setClass(xGetElementById('passLoginChkout1'),'campo');
	}
	if(Tipo == 'OLVIDO'){
		strPOST.push('usernameOlvido='+encodeURIComponent(GetElementFormValue(xGetElementById('usernameOlvidoChkout1'))));
		strPOST.push('emailOlvido='+encodeURIComponent(GetElementFormValue(xGetElementById('emailOlvidoChkout1'))));
		strPOST.push('accion='+encodeURIComponent('olvido'));
		strPOST = strPOST.join('&');
		makeRequest('/ajax-login.php',GetOlvidoResult,'POST',strPOST);
		setClass(xGetElementById('usernameOlvidoChkout1'),'campo');
		setClass(xGetElementById('emailOlvidoChkout1'),'campo');
	}
}

/* LOGIN */
function LimpiarErrorLogin(){
	xGetElementById('login_form_error_desc').innerHTML = '';
	if(xGetElementById('login_form_error_container').style.display != 'none'){ hide('login_form_error_container'); }
	xGetElementById('passLoginChkout1').value = '';
	xGetElementById('usernameLoginChkout1').value = '';
	setClass(xGetElementById('passLoginChkout1'),'campo');
	setClass(xGetElementById('usernameLoginChkout1'),'campo');
}

function GetLoginResult(xmlhttp) {
	switch (xmlhttp.readyState){
		case 1:
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var ErrorHTML = '', i;
			var responseXML = xmlhttp.responseXML;
			var LoginStatus = responseXML.getElementsByTagName('login');
			var LoginErrors = responseXML.getElementsByTagName('loginError');

			if( LoginStatus[0].getAttribute('logueado') == 's'){
				LimpiarErrorLogin();
				if(xGetElementById('bloque_login_ajax').style.display == 'block'){ fadeOutIn('bloque_login_ajax','out'); }
				LanzarRelated();
			}else{
				if(LoginErrors.length > 0){
					for (i=0; i<LoginErrors.length; i++) {
						ErrorHTML += '- '+LoginErrors[i].firstChild.nodeValue+'<br />';
					}
					if(xGetElementById('login_form_error_container').style.display == 'none'){ fadeOutIn('login_form_error_container','in'); }
					xGetElementById('login_form_error_desc').innerHTML = ErrorHTML;
					FormatearTapa('tengo_form_container_login');
					PonerTapa('tengo_form_container_login');
					if((LoginErrors[0].getAttribute('tipo_error')) == '1'){ setClass(xGetElementById('usernameLoginChkout1'),'campoError'); xGetElementById('usernameLoginChkout1').select(); }
					if((LoginErrors[0].getAttribute('tipo_error')) == '2'){ setClass(xGetElementById('passLoginChkout1'),'campoError'); xGetElementById('passLoginChkout1').value = ''; xGetElementById('passLoginChkout1').select(); }
				}

				if(LoginStatus[0].getAttribute('ventana') == 'inhabilitado'){
					ErrorHTML += 'La cuenta con el usuario <strong>'+LoginStatus[0].getAttribute('usuario')+'</strong> no se encuentra operativa.<br />\n';
					ErrorHTML += 'Por favor, genere una nueva cuenta haciendo <a href="#" onclick="LimpiarErrorLogin(); xGetElementById(\'rb_bloque_new_user\').click(); return false;" tittle="Crear una nueva cuenta." >click aquí</a>\n';
					if(xGetElementById('login_form_error_container').style.display == 'none'){ fadeOutIn('login_form_error_container','in'); }
					xGetElementById('login_form_error_desc').innerHTML = ErrorHTML;
					FormatearTapa('tengo_form_container_login');
					PonerTapa('tengo_form_container_login');
				}
				SacarMensaje();
				SacarTapa();
			}
			break;
	}
}

/* OLVIDO */
function LimpiarErrorOlvido(){
	xGetElementById('olvido_form_error_desc').innerHTML = '';
	if(xGetElementById('olvido_form_error_container').style.display != 'none'){ hide('olvido_form_error_container'); }
	xGetElementById('usernameOlvidoChkout1').value = '';
	xGetElementById('emailOlvidoChkout1').value = '';
	setClass(xGetElementById('usernameOlvidoChkout1'),'campo');
	setClass(xGetElementById('emailOlvidoChkout1'),'campo');
}

function GetOlvidoResult(xmlhttp) {
	switch (xmlhttp.readyState){
		case 1:
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var ErrorHTML = '', i;
			var responseXML = xmlhttp.responseXML;
			var LoginStatus = responseXML.getElementsByTagName('login');
			var LoginErrors = responseXML.getElementsByTagName('loginError');

			if(LoginStatus[0].getAttribute('enviado') == 'y'){
				hide('chkout1_container_login_olvido');
				hide('chkout1_container_login_login');
				show('chkout1_container_login_olvido_ok');
				FormatearTapa('tengo_form_container_login');
			}else{
				if(LoginErrors.length > 0){
					for (i=0; i<LoginErrors.length; i++) {
						ErrorHTML += '- '+LoginErrors[i].firstChild.nodeValue+'<br />';
					}
					if(xGetElementById('olvido_form_error_container').style.display == 'none'){ fadeOutIn('olvido_form_error_container','in'); }
					xGetElementById('olvido_form_error_desc').innerHTML = ErrorHTML;
					FormatearTapa('tengo_form_container_login');
					setClass(xGetElementById('usernameOlvidoChkout1'),'campoError'); xGetElementById('usernameOlvidoChkout1').select();
					setClass(xGetElementById('emailOlvidoChkout1'),'campoError'); xGetElementById('emailOlvidoChkout1').select();
				}
			}
			SacarMensaje();
			SacarTapa();
			break;
	}
}


function LanzarRelated(){
	if(xGetElementById('response_list_dominios').style.display != 'none'){ fadeOutIn('response_list_dominios','out'); }
	if(xGetElementById('response_list_atencion').style.display != 'none'){ fadeOutIn('response_list_atencion','out'); }
	if(xGetElementById('response_list_errors').style.display != 'none'){ fadeOutIn('response_list_errors','out'); }
	xGetElementById('response_list_atencion_content').innerHTML = '';
	xGetElementById('response_list_errors_content').innerHTML = '';
	if(xGetElementById('bloque_tengo').style.display != 'none'){
		FormatearTapa('tengo_form_container_login');
		PonerTapa('tengo_form_container_login');
		PonerMensaje('tapa');
	}
	makeRequest('/ajax-get-user-related.php',GetRelatedResult,'POST','');
}

function GetRelatedResult(xmlhttp) {
	switch (xmlhttp.readyState){
		case 1:
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var ErrorHTML = '', SelectHTML = '', i;
			var responseXML = xmlhttp.responseXML;
			var Servicios = responseXML.getElementsByTagName('dominio');
			var Errores = responseXML.getElementsByTagName('error');

			if(Errores.length > 0){
				for (i=0; i<Errores.length; i++) {
					ErrorHTML += '- '+Errores[i].firstChild.nodeValue+'<br />';
				}
				if(xGetElementById('response_list_errors').style.display == 'none'){ fadeOutIn('response_list_errors','in'); }
				FormatearTapa('tengo_form_container_login');
				xGetElementById('response_list_errors_content').innerHTML = ErrorHTML;
			}else{
				if(Servicios.length > 0){
					SelectHTML += '<select name="selector_dominios_cliente" id="selector_dominios_cliente" class="select">\n';
					for (i=0; i<Servicios.length; i++) {
						SelectHTML += '<option value="'+Servicios[i].getAttribute('id')+'">'+Servicios[i].firstChild.nodeValue+'</option>\n';
					}
					SelectHTML += '</select>\n';
					if(xGetElementById('response_list_dominios').style.display == 'none'){ fadeOutIn('response_list_dominios','in'); }
					FormatearTapa('tengo_form_container_login');
					xGetElementById('response_list_dominios_listado').innerHTML = SelectHTML;
				}else{
					if(xGetElementById('response_list_atencion').style.display == 'none'){ fadeOutIn('response_list_atencion','in'); }
					FormatearTapa('tengo_form_container_login');
					xGetElementById('response_list_atencion_content').innerHTML = 'No se encontró, en su cuenta, ningún plan calificado con el cual asociar este producto.<br />Por favor, Utilice un dominio que disponga o adquiera uno para utilizar especificamente con este producto.';
				}
			}

			SacarMensaje();
			SacarTapa();
			break;
	}
}
/************************************* FIN LOGIN *************************************/