
function redirect_confirm(url, msg)
{
	if(confirm(msg))
	{ 
		window.location.href = url; 
	}
	else
	{
		return false;
	}
}

function redirect_eval(msg, codigo)
{
	if(confirm(msg))
	{ 
		eval(codigo); 
	}
}

function redirect_getAXAH(msg,url,elementContainer,postdata)
{
	if(confirm(msg))
	{ 
		getAXAH(url,elementContainer,postdata);
	}
   
   return false;
}

function redirect_alert(url, msg)
{
	alert(msg); 
	window.location.href = url; 
	return false;
}

function redirectAJAX_alert(msg,url,elementContainer,postdata)
{
	alert(msg); 
	getAXAH(url,elementContainer,postdata);
	return false;
}


function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

//object detection to return the correct object depending upon broswer type. Used by the getAXHA(); function.
function getNewHttpObject() {
    var objType = false;
    try {
        objType = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            objType = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            objType = new XMLHttpRequest();
        }
    }
    return objType;
}

function getAXAH(url,elementContainer,postdata) {
//	screen.updateInterval=0;
   document.getElementById(elementContainer).innerHTML = '<table width="100%" align="center"><tr><td align="center" valign="middle"><img src="imagenes/loading.gif" align="middle" border="0" alt="cargando"></td></tr></table>';

	var theHttpRequest = getNewHttpObject();
   //alert(postdata);
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("POST", url);
	theHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  theHttpRequest.send(postdata);

   function processAXAH(elementContainer){
		var respuesta;
      if (theHttpRequest.readyState == 4) {
         if (theHttpRequest.status == 200) {
            respuesta = theHttpRequest.responseText.split("@@@");
            document.getElementById(elementContainer).innerHTML = respuesta[0];
            if(respuesta.length > 1)
            {
               //alert(respuesta.length);
               for (i=1; i<respuesta.length; i++)
               {
                  //alert(respuesta[i]);
                  eval(respuesta[i]);
               }
            }
         } else {
            document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> HTTP request return the following status message:&nbsp;" + theHttpRequest.statusText +"<\/p>";
         }
      }
   }
}

function getAXAH_form(url,elementContainer,formid){
//	screen.updateInterval=0;
   var Formulario = document.getElementById(formid);
   var longitudFormulario = Formulario.elements.length;
   var cadenaFormulario = ""
   var sepCampos
   sepCampos = ""
   for (var i=0; i <= Formulario.elements.length-1;i++) {
      InStr = cadenaFormulario.indexOf(Formulario.elements[i].name)+1;
      if (InStr == 0)
      {
         cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
         sepCampos="&";
      }
   }
   //alert(cadenaFormulario);

	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("POST", url);
	theHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  theHttpRequest.send(cadenaFormulario);

   function processAXAH(elementContainer){
	var respuesta;
      if (theHttpRequest.readyState == 4) {
         if (theHttpRequest.status == 200) {
            respuesta = theHttpRequest.responseText.split("@@@");
            document.getElementById(elementContainer).innerHTML = respuesta[0];
            if(respuesta.length > 1)
            {
               for (i=1; i<respuesta.length; i++)
               {
                  //alert(respuesta[i]);
                  eval(respuesta[i]);
               }
            }
         } else {
            document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> HTTP request return the following status message:&nbsp;" + theHttpRequest.statusText +"<\/p>";
         }
      }
   }
}

function fieldNumber (e)
{
	var key;
	var keychar;

	key = e.charCode? e.charCode : e.keyCode; 
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	// numbers
	else if ((("0123456789.").indexOf(keychar) > -1))
		return true;

	/*// decimal point jump
	else if (dec && (keychar == "."))
	{
		myfield.form.elements[dec].focus();
		return false;
	}*/
	else
		return false;
} 

function formatCurrency(num) 
{
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
      num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
      cents = "0" + cents;

   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));

   return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function cambia_letreros(id_aviso, texto)
{
   document.getElementById(id_aviso).innerHTML = texto;
}

function actualizaDetallado (idCantidad, idValor, idResultado)
{
	string = "$"; 
	var Valr = document.getElementById(idValor);
	var Cant = document.getElementById(idCantidad);
   var Resultado = 0.00;

   valueValr = parseFloat(Valr.value);
   valueCant = parseFloat(Cant.value);

   Resultado = parseFloat((valueCant)*(valueValr));

   cadenaResultado = formatCurrency(Resultado);
   //alert(cadenaResultado);

   cambia_letreros (idResultado, cadenaResultado);
   return false;
}

function multi2 (valueValr, valueCant, idResultado)
{
   var Resultado = 0.00;

   Resultado = parseFloat((valueCant)*(valueValr));

   cadenaResultado = formatCurrency(Resultado);
   //alert(cadenaResultado);

   cambia_letreros (idResultado, cadenaResultado);
   return false;
}

function limpiar (formid)
{
   var Formulario = document.getElementById(formid);
   for (var i=0; i <= Formulario.elements.length-1;i++) {
      //alert(Formulario.elements[i].type);
      if(Formulario.elements[i].type == "text")
         Formulario.elements[i].value = "";
      if(Formulario.elements[i].type == "select-one")
         Formulario.elements[i].value = "0";

   }
}
/*
function TestInputType(objValue,strRegExp,strError)
{
   var ret = true;

   //alert(objValue);
   var charpos = objValue.search(strRegExp); 
   //alert(objValue.value+" -- "+charpos);
   if(objValue.length > 0 &&  charpos >= 0) 
   { 
      if(strError.length > 1) 
      { 
         alert(strError); 
      }//if 
      ret = false; 
   }//if 
   return ret;
}

function TestRequiredInput(objValue,strError)
{
   var ret = true;
   var val = objValue.value;
   val = val.replace(/^\s+|\s+$/g,"");//trim
   if(eval(val.length) == 0) 
   { 
      if(strError.length > 1) 
      { 
         alert(strError); 
      }//if 
      ret=false; 
   }//if 
   return ret;
}
*/
function validateEmail(email)
{
   var splitted = email.match("^(.+)@(.+)$");
   if(splitted == null) return false;
   if(splitted[1] != null )
   {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
   }
   if(splitted[2] != null)
   {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
         var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
         if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
   }
   return false;
}

function toogle_sel (val, divID)
{
   el_div = document.getElementById(divID);
   if(val == 0)
      el_div.style.display = "none";
   else
      el_div.style.display = "inline";
}

function VerificaCampos(passwordID, passwordConfID, img, flagID)
{
	var valor = true;
	var campo1 = document.getElementById(passwordID);
	var campo2 = document.getElementById(passwordConfID);
	var imagen = document.getElementById(img);
	var flag = document.getElementById(flagID);

	if (campo1.value == campo2.value && campo1.value.length > 3)
	{
		imagen.src = "imgs/accept.png";
		flag.value = "CA5A5";
	}
	else
	{
		imagen.src = "imgs/exclamation.png";
		flag.value = "";
	}

	return true;
}
