function findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function getProductCode() {
  
  val=findObj('Product_Code');
  
  if ( val ) {
  
  	val.value=location.search.substr(13);
	val.value=val.value.toUpperCase();
  }
}

function getCodigoProducto() {
  
  val=findObj('Codigo_Producto');
  
  if ( val ) {
  
  	val.value=location.search.substr(13);
	val.value=val.value.toUpperCase();
  }
}

function validarCodigoProducto() {

  var args=validarCodigoProducto.arguments;

  val=findObj(args[0]);
  
  if ( val ) {  
  
	  val.value=val.value.toUpperCase();
  
	  if (val.value.length != 15 || val.value.charAt(7) != '-' ) {

	   	alert('ĦEl "Codigo de Producto" suministrado es invalido!\n');
		return false;  
	  } 
  }  
  return true;
}

function validarCodigoCliente() {

  var args=validarCodigoCliente.arguments;

  val=findObj(args[0]);
  
  if ( val ) {  
    
	  val.value=val.value.toUpperCase();

	  if ( val.value.length != 19 || val.value.charAt(6) != '-' || val.value.charAt(11) != '-' ) {

	   	alert('ĦEl "Codigo de Cliente" suministrado es invalido!\n');
		return false; 
	  } 
  }  
  return true;
}

function validateProductCode() {

  var args=validateProductCode.arguments;

  val=findObj(args[0]);
  
  if ( val ) {  
  
	  val.value=val.value.toUpperCase();
  
	  if (val.value.length != 15 || val.value.charAt(7) != '-' ) {

	   	alert('The supplied "Product Code" is invalid!\n');
		return false;  
	  } 
  }  
  return true;
}

function validateCustomerCode() {
	
  var args=validateCustomerCode.arguments;

  val=findObj(args[0]);
  
  if ( val ) {  
    
	  val.value=val.value.toUpperCase();

	  if ( val.value.length != 19 || val.value.charAt(6) != '-' || val.value.charAt(11) != '-' ) {

	   	alert('The supplied "Customer Code" is invalid!\n');
		return false; 
	  } 
  }  
  return true;
}

function validateForm() { //v4.0

  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;

  for (i=0; i<(args.length-2); i+=3) { 

    test=args[i+2]; 
    val=findObj(args[i]);

    if (val) { 

      nm=args[i+1];	  
	  if (nm == '' ) nm=val.name; 

      if (test.indexOf('isSel')!=-1) {

        if (val.value=='0' && test.charAt(0) == 'R' ) errors+='- '+nm+' is required.\n';
      }
      else

      if ((val=val.value)!="") {	

        if (test.indexOf('isEmail')!=-1) { 

	  p1=val.indexOf('@');
	  p2=val.indexOf('.');

          if (p1<1 || p1==(val.length-1) || p2<1 || p2==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } 
        else 

          if (test!='R') {

            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf('inRange') != -1) { 

	      p=test.indexOf(':');
              min=test.substring(8,p); 
              max=test.substring(p+1);

              if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
            }
          } 
      } 
      else 

        if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; 
    }
  } 

  if (errors) alert('The following error(s) occurred:\t\n\n'+errors);

  return (errors == '');
}

function validarForm() { //v4.0

  var i,p,q,nm,test,num,min,max,errors='',args=validarForm.arguments;

  for (i=0; i<(args.length-2); i+=3) { 

    test=args[i+2]; 
    val=findObj(args[i]);

    if (val) { 

      nm=args[i+1];	  
	  if (nm == '' ) nm=val.name; 

      if (test.indexOf('isSel')!=-1) {

        if (val.value=='0' && test.charAt(0) == 'R' ) errors+='- '+nm+' es obligatorio.\n';
      }
      else

      if ((val=val.value)!="") {	

        if (test.indexOf('isEmail')!=-1) { 

	  p1=val.indexOf('@');
	  p2=val.indexOf('.');

          if (p1<1 || p1==(val.length-1) || p2<1 || p2==(val.length-1)) errors+='- '+nm+' debe contener una direccion e-mail.\n';
        } 
        else 

          if (test!='R') {

            if (isNaN(val)) errors+='- '+nm+' debe contener un numero.\n';
            if (test.indexOf('inRange') != -1) { 

	      p=test.indexOf(':');
              min=test.substring(8,p); 
              max=test.substring(p+1);

              if (val<min || max<val) errors+='- '+nm+' debe contener un numero entre '+min+' y '+max+'.\n';
            }
          } 
      } 
      else 

        if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n'; 
    }
  } 

  if (errors) alert('Se produjeron los siguientes errores:\t\n\n'+errors);

  return (errors == '');  
}

function AdjustSize( formname, objname, objsize ) {
 
 if ( navigator.appName == "Microsoft Internet Explorer" ) 
 
 	for (i=0; i<document.forms.length; i++) 
	
		if ( document.forms[i].name == formname ) 
		
			document[ formname ][ objname ].size = objsize;
}

// Usage: <a href="me.jpg" onclick="return popImage(this.href,'Site author');">link</a>
//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 400;
defaultHeight = 200;

//kinda important
var AutoClose = false;
var AutoCenter = false;

//don't touch
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 50, oH + 100 );\n'+
    'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    'if( !window.opera && '+AutoCenter+') { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div align="center" style="display:table;">'))+
    '<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}
