var ArrayPeriodos = new Array();

function AgregarAlCarro(){
	var Formulario = 'ComprarProductosForm';
	var FormObj = document.forms[Formulario];
	var cuenta = 0;
	for (i=0; i<FormObj.length; i++) {
		if((FormObj.elements[i].type == 'radio') && (FormObj.elements[i].checked == true)) {
			cuenta = cuenta + 1;
		}
	}
	if(cuenta > 0){
		FormObj.submit();
	}else{
		alert('Para poder realizar el registro debe seleccionar un período.');		
		return false;
	}
}

function MarcarPeriodo(){
	var i, PeriodoSelected = '', PrecioSelected = '', AnoFormated = 'Año', PeriodoFormated;
	var Formulario = 'ComprarProductosForm';
	var FormObj = document.forms[Formulario];

	for (ppindice in ArrayPeriodos){
		if(xGetElementById('pp_periodo_'+ppindice).checked){
			setClass(xGetElementById('pp_periodo_'+ppindice+'_tr'),'wh_periodo_tr_selected');
			PeriodoSelected = ppindice * 1;
			PrecioSelected = ArrayPeriodos[ppindice] * 1;
		}else{
			setClass(xGetElementById('pp_periodo_'+ppindice+'_tr'),'wh_periodo_tr');
		}
	}
	if(PeriodoSelected > 12){AnoFormated = 'Años';}
	PeriodoFormated = PeriodoSelected / 12;
	PeriodoSelected = PeriodoFormated+' '+AnoFormated;
	xGetElementById('lp_monto_periodo').innerHTML = PeriodoSelected;
	xGetElementById('lp_monto_total').innerHTML = FormatPrecio(PrecioSelected);
	if(xGetElementById('lp_bloque_dominio_monto').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_monto','in'); }
	if(xGetElementById('lp_bloque_dominio_fin_1').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_fin_1','out'); }
	if(xGetElementById('lp_bloque_dominio_fin_2').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_fin_2','in'); }
}


//Manejo de chequeo de dominio
//*******************************************************************************
function checkDomain(campo,Pais){
	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,'lp_campoError');
		return false;
	}
	
	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(errorCh != ''){
		alert(errorCh);
		setClass(o_campo,'lp_campoError');
		return false;
	}else{
		xGetElementById('pl_list_periodo').innerHTML = '';
		if(xGetElementById('lp_bloque_dominio_fin_2').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_fin_2','out'); }
		if(xGetElementById('lp_bloque_dominio_fin_1').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_fin_1','in'); }
		if(xGetElementById('lp_bloque_dominio_periodo').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_periodo','out'); }
		if(xGetElementById('lp_bloque_dominio_result_ok').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_result_ok','out'); }
		if(xGetElementById('lp_bloque_dominio_monto').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_monto','out'); }
		if(xGetElementById('lp_bloque_dominio_result_bad').style.display != 'none'){ fadeOutIn('lp_bloque_dominio_result_bad','out'); }
		o_campo.value = nameTemp;
		var TLDSsearch = '.com.mx';
		PonerTapa('lp_compra_box');
		PonerMensaje('tapa');
		FormatearTapa('lp_compra_box');
		makeRequest('/ajax-check-dominios-promopack.php',GetDominiosExtrasResult,'POST','dom='+encodeURIComponent(nameTemp)+'&tld='+encodeURIComponent(TLDSsearch)+'&sug=N&Pais='+Pais+'&periodo=all');
	}
}

function GetDominiosExtrasResult(xmlhttp) {
	switch (xmlhttp.readyState){
		case 1: 			
			//onLoading();
			break;
		case 2:
			//onLoaded();
			break;
		case 3:
			//onInteractive();
			break;
		case 4:
			var i;
			var TLDsinpunto;
			var perLOOP;
			var PrecioPeriodoMinimo;
			var PeriodoSTR = '';
			var responseXML = xmlhttp.responseXML;
			var Dominios = responseXML.getElementsByTagName('dominio');
			var Periodos = responseXML.getElementsByTagName('periodo');
			
			if(Dominios.length > 0){
				for (i=0; i<Dominios.length; i++) {
					if(Dominios[i].getAttribute('status') == 'S'){
						TLDsinpunto = Dominios[i].getAttribute('tld');
						TLDsinpunto = TLDsinpunto.replace(/\./g,"");
						PeriodoSTR += '<input name="cbxDominio[]" id="cbxDominio" type="hidden" value="'+Dominios[i].getAttribute('idna')+Dominios[i].getAttribute('tld')+'" />\n';
						PrecioPeriodoMinimo = Dominios[i].getAttribute('precio')*1;
						SacarMensaje();
						SacarTapa();
						xGetElementById('lp_dominio_disp').innerHTML = Dominios[i].firstChild.nodeValue+Dominios[i].getAttribute('tld');
						
						if(Periodos.length > 0){
							PeriodoSTR += '<table cellspacing="0" cellpadding="2" align="center" style="margin-top:6px; border:1px solid #CCCCCC;">\n';
							for (perLOOP=0; perLOOP<Periodos.length; perLOOP++) {
								ArrayPeriodos[Periodos[perLOOP].getAttribute('periodo')] = Periodos[perLOOP].getAttribute('precio');
								PeriodoSTR += '  <tr id="pp_periodo_'+Periodos[perLOOP].getAttribute('periodo')+'_tr" class="wh_periodo_tr">\n';
								PeriodoSTR += '    <td align="left" height="10" valign="middle" width="3"><input name="optPeriodo_'+Dominios[i].getAttribute('idna')+'_'+TLDsinpunto+'" id="pp_periodo_'+Periodos[perLOOP].getAttribute('periodo')+'" type="radio" value="'+Periodos[perLOOP].getAttribute('periodo')+'-XXXX" class="reg_dom_alt_item_radio" onclick="MarcarPeriodo();" /></td>\n';
								PeriodoSTR += '    <td align="left" height="10" valign="middle" class="wh_periodo_periodo">'+(Periodos[perLOOP].getAttribute('periodo') / 12)+'&nbsp;'+FormatPeriodo2(Periodos[perLOOP].getAttribute('periodo') / 12)+'</td>\n';
								PeriodoSTR += '    <td align="left" height="10" valign="middle" class="wh_periodo_precio"><span class="wh_periodo_moneda">'+simboloMoneda+'</span><span class="wh_periodo_numero"> '+FormatPrecio(Periodos[perLOOP].getAttribute('precio')*1)+'</span></td>\n';
								/* PeriodoSTR += '    <td align="left" height="10" valign="middle" class="wh_periodo_ahorro">ahorra: <span class="wh_periodo_moneda">'+simboloMoneda+'</span> '+FormatPrecio((PrecioPeriodoMinimo - ((Periodos[perLOOP].getAttribute('precio')*1) / (Periodos[perLOOP].getAttribute('periodo') / 12)))*(Periodos[perLOOP].getAttribute('periodo') / 12))+'</td>\n'; */
								PeriodoSTR += '  </tr>\n';
							}
							PeriodoSTR += '</table>';
							xGetElementById('pl_list_periodo').innerHTML = PeriodoSTR;
						}
						
						if(xGetElementById('lp_bloque_dominio_result_ok').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_result_ok','in'); }
						if(xGetElementById('lp_bloque_dominio_periodo').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_periodo','in'); }
					}else{
						SacarMensaje();
						SacarTapa();
						xGetElementById('lp_dominio_nodisp').innerHTML = Dominios[i].getAttribute('idna')+Dominios[i].getAttribute('tld');
						if(xGetElementById('lp_bloque_dominio_result_bad').style.display == 'none'){ fadeOutIn('lp_bloque_dominio_result_bad','in'); }
					}
				}
			}else{
				SacarMensaje();
				SacarTapa();
			}
			break;
	}
}