//////////////////////////////////////////////////////////////////
// 		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==12){
		hora = 12;
		time = "PM";
	}
	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 = 11;
		time = "PM";
	}
	if(hora==00){
		hora = 12;
		time = "AM";
	}
	Fecha = mes+'/'+dia+'/'+ayo+' '+hora+':'+minuto+' '+time;
	TargetDate = Fecha;
	//TargetDate = "05/27/2011 4:32 pM";
	CountActive = true;
	CountStepper = -1;
	LeadingZero = true;
	DisplayFormat = "%D% Dia %H%h %M%m %S%s";
	FinishMessage = "&iexcl;&iexcl;La oferta ha terminado!!";

	CountStepper = Math.ceil(CountStepper);
	if (CountStepper == 0)
	  CountActive = false;
	var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	//putspan();
	var dthen = new Date(TargetDate);
	var dnow = new Date();
	if(CountStepper>0)
	  ddiff = new Date(dnow-dthen);
	else
	  ddiff = new Date(dthen-dnow);
	gsecs = Math.floor(ddiff.valueOf()/1000);
	CountBack(gsecs);
}

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) {
	  if (secs < 0) {
		document.getElementById("cntdwn").innerHTML = FinishMessage;
		return;
	  }
	DisplayStr = DisplayFormat
	if(calcage(secs,86400,100000)>0){
  		DisplayStr = DisplayStr.replace(/%D%/g, 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, 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("cntdwn").innerHTML = DisplayStr;
 
}

/*function putspan() {
 document.write("<span id='cntdwn'></span>");
}*/





	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
	

