var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if (!left) left = (screen.width/2) - width/2;
  if (!top) top = (screen.height/2) - height/2;
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function confirmLink(theLink, theSqlQuery)
{
    var is_confirmed = confirm('Tem a certeza que \n' + theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////
//FRMCHK

document.formValidationValue = true;

function findFormObj(n, formName) { 
  var p,i,x;  
  var d=document; 
  x=d.forms[formName][n];
  return x;
}

function validateForm(formName, argsArr) { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args= new Array();
  args = argsArr;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=findFormObj(args[i],formName); vname = args[i+1];
    if (val) { if (vname!="") {nm=vname;} else {nm=val.name; nm=nm.toUpperCase();} if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' tem que conter um endereço de e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' tem que ser numérico.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' tem que conter um número entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' é de preenchimento obrigatório.\n'; }
  } if (errors) alert('Foram detectados os seguintes erros no preenchimento do formulário:\n'+errors);
  document.formValidationValue = (errors == '');
}

function validatePassword(formName, passFld, passConfFld) {
	if (document.formValidationValue==false) return false;
	var ret = false;
	var passObj=findFormObj(passFld,formName);
	var passConfObj=findFormObj(passConfFld,formName);
	if (passObj.value != passConfObj.value) alert ('Palavra-passe não confirmada.'); 
	else ret = true;
	document.formValidationValue = ret;
}

