//////////////////////////////////////////////////////////////////
// 		Libreria comun de rutinas
//////////////////////////////////////////////////////////////////


	// Abrir ventana
	function win(theURL,winName,ancho,alto,barras) {
		var winl = (screen.width - ancho) / 2;
		var wint = (screen.height - alto) / 2;
		var paramet='top='+wint+',left='+winl+',width='+ancho+',height='+alto+',scrollbars='+barras+'';
		var splashWin=window.open(theURL,winName,paramet);
		splashWin.focus();
	}

	// Abrir ventana eficiente
	function popc(a){
		var enlace = a.getAttribute("href");
		var nombre = a.getAttribute("nombre");
		var ancho = a.getAttribute("ancho");
		var alto = a.getAttribute("alto");
		var barras = a.getAttribute("barras");
		if(isNaN(ancho)) ancho = 300;
		if(isNaN(alto)) alto = 250;
		if(isNaN(barras)) barras = 0;
		win(enlace,nombre,ancho,alto,barras);
	}
	

	// Validar email
	function validarEmail(valor) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)) {
			return (true);
		} else {
			return (false);
		}
	};

// Contar
/*
function contar(ayo,mes,dia,hora,minuto,segundo,id) {
	time = "AM";
	if (hora==13){
		hora = 01;
		time = "PM";
	}
	if(hora==14){
		hora = 02;
		time = "PM";
	}
	if(hora==15){
		hora = 03;
		time = "PM";	
	}
	if(hora==16){
		hora = 04;
		time = "PM";	
	}
	if(hora==17){
		hora = 05;
		time = "PM";
	}
	if(hora==18){
		hora = 06;
		time = "PM";
	}
	if(hora==19){
		hora = 07;
		time = "PM";
	}
	if(hora==20){
		hora = 08;
		time = "PM";	
	}
	if(hora==21){
		hora = 09;
		time = "PM";
	}
	if(hora==22){
		hora = 10;
		time = "PM";
	}
	if(hora==23){
		hora = 11;
		time = "PM";
	}
	if(hora==24){
		hora = 12;
		time = "PM";
	}
	Fecha = mes+'/'+dia+'/'+ayo+' '+hora+':'+minuto+'0 '+time;
	TargetDate = Fecha;
	//TargetDate = "05/27/2011 4:32 pM";
	CountActive = true;
	CountStepper = -1;
	LeadingZero = true;
	DisplayFormat = "%D% d&iacute;a %H%h %M%m %S%s";
	FinishMessage = "Termin&oacute; la oferta!!";

	
	var dthen = new Date(TargetDate);
	var dnow = new Date();
	
	ddiff = new Date(dthen-dnow);
	gsecs = Math.floor(ddiff.valueOf()/1000);
	CountBack(gsecs,id);
}

function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return  s;
}

function CountBack(secs,id) {
	DisplayFormat = "%D% d&iacute;a %H%h %M%m %S%s";
	FinishMessage = "Termin&oacute; la oferta!!";
	  if (secs < 0) {
		document.getElementById('contar' + id).innerHTML = FinishMessage;
		return;
	  }
	DisplayStr = DisplayFormat
	if(calcage(secs,86400,100000)>0){
  		DisplayStr = DisplayStr.replace(/%D%/g, parseInt(calcage(secs,86400,100000)));
		if (calcage(secs,86400,100000)>1){
			DisplayStr = DisplayStr.replace(/Dia/g, "d&iacute;as");
		}
	}else{
		DisplayStr = DisplayStr.replace(/%D% Dia /g,"");
	}
	if(calcage(secs,3600,24)>0){
  		DisplayStr = DisplayStr.replace(/%H%/g,  parseInt(calcage(secs,3600,24)));
	}else{
		DisplayStr = DisplayStr.replace(/%H%h/g,"");
	}
	
	if(calcage(secs,60,60)>0){
		DisplayStr = DisplayStr.replace(/%M%/g, calcage(secs,60,60));
	}else{
		DisplayStr =DisplayStr.replace(/%M%m/g,"");
	}

	DisplayStr = DisplayStr.replace(/%S%/g, calcage(secs,1,60));
	
	  
  document.getElementById('contar' + id).innerHTML = DisplayStr;

}
*/

	
	function contar(ayo,mes,dia,hora,minuto,segundo,id) {
 	var tiempo = new Date();
	var d	= dia - tiempo.getDate();
	var ms = tiempo.getMonth()+1;
	if (d<0){
		if(ms == 1 || ms == 3 || ms == 5 || ms == 7 || ms == 8 || ms == 10 || ms == 12){
			d = ( 31- tiempo.getDate()) + dia ;
		}
		if(ms == 4 || ms==6 || ms==9 || ms == 11){
			d = ( 30- tiempo.getDate()) + dia ;
		}
		if(ms == 2){
			d = ( 28 - tiempo.getDate()) + dia ;
		}
	}
	
	var h = hora - tiempo.getHours()-1;
	if (h<=0){
		var m = minuto -tiempo.getMinutes() -1  ;
	}else{
		var m = 60 - minuto - tiempo.getMinutes() -1 ;
	}
	if (m<=-1){
		var s = segundo - tiempo.getSeconds();
	}else{
		var s = 60 - segundo - tiempo.getSeconds() ;	
	}
	if (s<=0){
		m=m-1;
	}
	if(m<=0){
		h=h-1;
	}
	  var txt = '';
		if (d > 0) {
			txt=d+' d&iacute;a';
			if (d != 1) { txt+='s'; }
			txt+=' - '; 
		}
		if (h > 0 || d > 0) {
			txt+=h+'h ';
			//if (hora != 1) { txt+='s '; }
		}
		if (m > 0 || h > 0 || d > 0) {
			txt+=m+'m ';
		}
		txt+=s+'s';
		
		 if (d<=0 && m<=0 && s<=0) { 
		  	document.getElementById('contar' + id).innerHTML="<font color='#FF0000'>Termin&oacute; la oferta!!</font>"; 
		}else{
		document.getElementById('contar' + id).innerHTML=txt;
		}
}



	function contar_antigua(ayo,mes,dia,hora,minuto,segundo,id) {
	
	  var dif = 'Dia ' + dia + ' del ' + mes + ' de ' + ayo + ', a las ' + hora + ':';
	  if (minuto < 10) { dif+='0'; }
	  dif+=minuto + '<br>';
	  //document.getElementById('evento' + id).innerHTML=dif
	  var a = new Date();
	  //hora, minuto,segundo, mes,dia a?o.
	  var dif= mktime(hora,minuto,segundo,mes - 1,dia,ayo) - mktime(a.getHours(),a.getMinutes(),a.getSeconds(),a.getMonth(),a.getDate(), a.getFullYear());
	  if (dif < 0) { document.getElementById('contar' + id).innerHTML="<font color='#FF0000'>Termin&oacute; la oferta!!</font>"; }
	  else {
		dia= Math.floor(dif/60/60/24);  
		hora= Math.floor((dif - dia*60*60*24)/60/60);
		minuto= Math.floor((dif - dia*60*60*24 - hora*60*60)/60);
		segundo= Math.floor(dif - dia*60*60*24 - hora*60*60 - minuto*60);
		var txt = '';
		if (dia > 0) {
			txt=dia+' d&iacute;a';
			if (dia != 1) { txt+='s'; }
			txt+=' - '; 
		}
		if (hora > 0 || dia > 0) {
			txt+=hora+'h ';
			//if (hora != 1) { txt+='s '; }
		}
		if (minuto > 0 || hora > 0 || dia > 0) {
			txt+=minuto+'m ';
			//if (minuto != 1) { txt+='s '; }
		}
		txt+=segundo+'s';
		//if (segundo != 1) { txt+='s'; }
		document.getElementById('contar' + id).innerHTML=txt;
		}
	
		function mktime() {
		
			var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
			d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
		 
			var dateManip = {
				0: function(tt){ return d.setHours(tt); },
				1: function(tt){ return d.setMinutes(tt); },
				2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
				3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
				4: function(tt){ return d.setDate(tt+mb); },
				5: function(tt){ return d.setYear(tt+ma); }
			};
			
			for( i = 0; i < argc; i++ ){
				no = parseInt(argv[i]*1);
				if(no && isNaN(no)){
					return false;
				} else if(no){
					// arg is number, let's manipulate date object
					if(!dateManip[i](no)){
						// failed
						return false;
					}
				}
			}
		 
			return Math.floor(d.getTime()/1000);
		} // en mktime

	} // end contar
	

