// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
	function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}
	
	function calcularTotal(cantidadItems,cantidadMeses){
		nuevo_monto_total = 0;
		for(i=0;i<cantidadItems;i++){
			monto_i = $('montoMensual'+i).readAttribute('alt');

			nuevo_monto_i = monto_i * cantidadMeses;
			nuevo_monto_total = nuevo_monto_total + nuevo_monto_i;

			nuevo_monto_i = formatNumber(nuevo_monto_i,2,',','.','','','-','');
			$('cantSubTotal'+i).update(nuevo_monto_i);
			
			if(i > 0){
				$('cantMeses'+i).disabled 	= 	false;
				$('cantMeses'+i).value 		= 	cantidadMeses;
				$('cantMeses'+i).disabled 	= 	true;				
			}
		}
		$('cantTotal').update(formatNumber(nuevo_monto_total,2,',','.','','','-',''));
	}
	
	function actualizarLinkCompra(link){
		$('mensajeError').hide();
		$('mensajeError2').hide();
	
		if($('dominio').value == ''){
			alert('Debe escribir un dominio');
			$('mensajeError2').show();
			document.location.href='#mensajeError2';			
			return false;
		}
		registro = '';
		switch($('registroPrevioDominio').value){
			case 'SI':
				registro = '/registro/previo';
				break;
			case 'NO':
				var radioGrp = document['forms']['formDominio']['gestionDominio'];
				radioValue = '';
				for(i=0; i < radioGrp.length; i++){
					if (radioGrp[i].checked == true) {
					var radioValue = radioGrp[i].value;
					}
				}
				if(radioValue == 'registroESW'){
					registro = '/registro/registroESW';
				}else if(radioValue == 'registroTercero'){
					registro = '/registro/registroTercero';				
				}else{
					alert('Debe seleccionar si usted va a registrar el dominio o desea que nosotros lo hagamos por usted');
					return false;
				}
				break;
			default:
		}
		$('linkCompra').setAttribute('href',link+'cantMeses/'+$('cantMeses0').value+'/dominio/'+$('dominio').value+$('extensionDominio').value+registro);
		if(!valido){
			$('mensajeError').show();
			document.location.href='#mensajeError';
		}else{
			$('mensajeError').hide();		
		}
		return valido;
	}
function buscarDominio(dominio){
	$('imgOk').hide();
	$('imgError').hide();
	$('imgCargando').show();
	$('dominio').style.backgroundColor = '';
	valido = false;
	dir = '/comprar/orden/buscardominio/dominio/'+dominio;
	new Ajax.Request(dir,
	{
		method:'get',
		onSuccess: function(transport){
			var data = transport.responseText;
			if(data == 1){
				$('imgError').show();
				$('dominio').style.backgroundColor = 'salmon';
				valido = false;
			}else if(data == 0){
				$('imgOk').show();
				valido = true;
				$('mensajeError').hide();
			}else{
				alert('Error:'+data);
			}
			$('imgCargando').hide();
		},
		onFailure: function(){ alert('Error buscando el dominio, vuelva a intentar') }
	});
}
var valido = true;
