// JavaScript Document
function ObtenerFecha(){
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	return(day+"/"+month+"/"+year);	
}

function validarDescuento(idobj){
	if(parseFloat($("#"+idobj).val())>100){
		$("#"+idobj).val(100);
	}
}

function RevelarCampo(idinput, idboton){
	if($("#"+idinput).attr("type")=="password"){
		$("#"+idboton).val("Ocultar");
		$("#"+idinput).attr("type","text");
	}else{		
		$("#"+idboton).val("Mostrar");
		$("#"+idinput).attr("type","password");
	}
}


function validarEmail(idobj){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var Resultado="";
	if ($("#"+idobj).val()==null || $("#"+idobj).val()==''){
		Resultado='No ha introducido el Correo Electrónico';
		$("#"+idobj).focus();
	}else if (!reg.test($("#"+idobj).val())){
		Resultado='No ha introducido un Correo Electrónico válido';
		$("#"+idobj).val("");
		$("#"+idobj).focus();
	}
	return Resultado;
}


function GenerarClave(id){
	var lng=10;
	var clave="";
	var chr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	var ind=0;
	for(var i =0; i<lng;i++){
		ind=Math.floor(Math.random()*(chr.length));
		clave+=chr[ind];
	}
	$("#"+id).val(clave);
}

function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();
    });
}

/*-------- CALCULADORA PUNTOS --------*/

function MostrarCalculadora(id){
	ancho=300;
	alto=400;

	if(id==null){
		id="div_tools";
	}
	
	var datos="pagajax=paginas/arkges/calculadora.php";
	datos+="&offline=1";
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){
			$('#'+id).html(html);
		}
	});
	
	if(id=="div_tools"){
		$("#"+id).css('zIndex','5000');
		$("#"+id).attr('title','Calculadora');
		$('#'+id).dialog({
			autoOpen: true,
			width: ancho,
			height: alto,
			modal:false,
			resizable: false,
			dialogClass: 'dialog',
			closeOnEscape:false,
			/*close: function()
				{$(this).dialog( "close" );
				$(this).dialog( "destroy" );
				$("#"+id).html("");
			},*/
			show: "slide",
			zindex: 100,
			stack:true,
			buttons: {} 
		});	
		$('#principal').css('zIndex',100);	
	}
}

function ExeCalculadora(tecla){
	var operacion=$('#operacioncalc').html();
	var txt=operacion.toString();
	var insertar=true;
	var aux;
	
	//NUMEROS y PUNTO
	if(tecla!="del" && tecla!="intro" && tecla!="AC" && tecla!="neg"){		
		
		//EXCEPCIONES
		if(tecla=="."){
			if(txt.length>0 && txt[txt.length-1]=="."){
				insertar=false;
				
			}else if(txt.length<1 || txt[txt.length-1]=="+" || txt[txt.length-1]=="-" || txt[txt.length-1]=="*" || txt[txt.length-1]=="/"){
				$('#operacioncalc').append("0");
			}
						
		}else if(tecla=="+" || tecla=="-" || tecla=="*" || tecla=="/"){
			if(txt[txt.length-1]=="+" || txt[txt.length-1]=="-"|| txt[txt.length-1]=="*" || txt[txt.length-1]=="/" || txt[txt.length-1]=="."){
				txt=txt.substr(0,(txt.length-1))
				$('#operacioncalc').html(txt);
			}
			
			
		}else if(tecla=="0"){
			aux=txt.split("+")
			aux=aux[aux.length-1].split("-");
			aux=aux[aux.length-1].split("*");
			aux=aux[aux.length-1].split("/");
			aux=aux[aux.length-1];
			if((txt.length==1 && txt[txt.length-1]=="0") || (txt.length>1 && txt[txt.length-1]=="0" && (aux.indexOf(".")<0 && eval(aux)==0)) ){
				insertar=false;
			}
		}else{
			//RESTO DE NÚMEROS
			//no hay que hacer nada	
		}
		
		if(insertar){
			$('#operacioncalc').append(tecla);	
		}
	}else if(tecla=="neg"){
		aux=txt.split("+")
		aux=aux[aux.length-1].split("*");
		aux=aux[aux.length-1].split("/");
		//aux=aux[aux.length-1].split("-");
		aux=aux[aux.length-1];
		
		//aux=aux.replace(")","");
		
		txt=txt.substr(0, (txt.length)-aux.length);
		
		//alert(aux);
		aux=eval(aux)*(-1);
		
		if(eval(aux)<0){
			txt += "(" + aux + ")";
		}else{
			txt += aux;
		}
		
		$('#operacioncalc').html(txt);

				
	}else if(tecla=="intro"){
		//Boton INTRO
		$('#operacioncalc').html( eval($('#displaycalc').html()) );
		
	}else if(tecla=="del"){		
		//Boton SPR
		operacion=$('#operacioncalc').html();

		txt=operacion.toString();
		txt=txt.substr(0, txt.length-1);
		
		if(txt[txt.length-1]==" "){
			txt=txt.substr(0, txt.length-1);
		}
		$('#operacioncalc').html(txt);
		
	}else if(tecla=="AC"){
		$('#operacioncalc').html("");
	}
	
	EvaluarExpresion();
		
}

function EvaluarExpresion(){
	var seguir=true;

	operacion=$('#operacioncalc').html();
	var txt=operacion.toString();

	do{
		if(txt.length>0 && isNaN(txt[txt.length-1]) && (txt[txt.length-1]!=")") ){
			txt=txt.substr(0, txt.length-1);
			seguir=false;		
		}else{
			seguir=true;	
		}
	}while(seguir!=true)
	
	if(txt.length<1) txt=0;
	
	$('#displaycalc').html(eval(txt));
}



