// Scripts used in www.advantagepayroll.com

//This function is used to open a popup window with the Advantage disclaimer at the top.
//It eliminates the need for multiple framed pages on the server.  To use this function, use the following syntax:
//		onClick="popup_with_disclaimer('http://www.adobe.com/products/acrobat/readstep.html');"

function popup_with_disclaimer(theUrl) {
	var theNewWin = "/popups/disclaimer.html?" + theUrl;
	window.open(theNewWin, "popupWin", "width=850,height=670,resizable=yes,scrollbars=yes,status=yes");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function validateSearch(searchStr) {
	var obj = form1.txtSearchField;  //contents of search field
    var str 	= obj.value ; // value to be 'tested' 
	var errFound = 0;
	
	if (str.length < 1) {
		alert("To perform a search, please enter at\nleast one search term, and click \"GO\".");
		form1.txtSearchField.focus();
		return false;
	}
	else {
		for(var i=0; i<str.length; i++) {
			var j=str.charAt(i);
			switch (j) {
				case '!': errFound = 1; 
				break;  
					
				case '@': errFound = 1; 
				break;
					
				case '#': errFound = 1; 
				break;
					
				case '$': errFound = 1; 
				break;
				
				case '%': errFound = 1; 
				break;
				
				case '^': errFound = 1; 
				break;
				
				case '&': errFound = 1; 
				break;
				
				case '*': errFound = 1; 
				break;
				
				case '(':	errFound = 1; 
				break;
					   
				case ')':	errFound = 1; 
				break;
				
				case '-':	errFound = 1; 
				break;
				
				case '+': errFound = 1;
				break;
				
				case '=': errFound = 1; 
				break;
				
				case '?': errFound = 1; 
				break;
				
				case '\'': errFound = 1; 
				break;
				
				default: errFound = 0; 
				break;
			}
			if (errFound == 1) {
				alert("Please enter only letters, numbers, or commas in your search.");
				form1.txtSearchField.focus();
				return false;
			}
		}
		return true;
	}
}

// This is the function that performs form verification.  It will be invoked
// from the onSubmit() event handler.  The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined.  Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        }
    }

    // Now, if there were any errors, then display the messages, and
    // return true to prevent the form from being submitted.  Otherwise
    // return false
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

//This function is used in clientdemo.asp
function isNumRange(obj, min, max){
	var obj = strContact.employees
    var str 	= obj.value ; // value to be 'tested' 
    var name 	= obj.name  ; // name of the field	
    var errMsg	= "Please enter a number between " + min + " and "+ max
    	errMsg +="\nfor " + name + ". (A Required entry)" ;
    	errMsg +="\nYou entered \"" + str +"\"." ;
// check for blank field
        if (str==""){
          alert(errMsg) ; // tell the user what is wrong
          obj.focus()   ; // focus on the field with the problem
          obj.select()  ; // select the contents of the field with the problem
          return false  ; // tell wrap around function that this value fails
        }
// check for non-digits
        for (var i=0; i < str.length; i++){  
        var ch = str.charAt(i)
        if (ch < "0" || ch > "9"){       
       	 alert(errMsg) ; // tell the user what is wrong
          obj.focus()   ; // focus on the field with the problem
          obj.select()  ; // select the contents of the field with the problem
          return false  ; // tell wrap around function that this value fails
         }
        }
// check if within range 
	var val = parseInt(str, 10)
	if ((val < min) || (val > max)) {
          alert(errMsg) ; // tell the user what is wrong
          obj.focus()   ; // focus on the field with the problem
          obj.select()  ; // select the contents of the field with the problem
          return false  ; // tell wrap around function that this value fails
	 }
    return true         ; // tell wrap around function that this value passes
}

function isValidPhone(phobj) {
    var re = /^\d{10}$/   ; // must be 10 digits
    var phobj = strContact.phone
    var str 	= phobj.value ; // value to be 'tested'
    var name 	= phobj.name  ; // name of the field	
    var errMsg	= "Please enter your Telephone Number." 
    	  errMsg +="\nin  " + name + ". (A Required entry)" ;
    	  errMsg +="\nYou entered \"" + str +"\"." ;    	  
    	  errMsg +="\ntry a pattern like " ;
    	  errMsg +="\n999 999 9999 (10 digits - includes area code)";  
    str = str.replace(/[^0-9]/g,"") ; // remove all non digits before testing
    RegExp.input =str ; // aka RegExp.input
    var newString = str.match(re);
    if (!newString){ 	; // if not OK then the test fails
      alert(errMsg) 	; // tell the user what is wrong
	   	phobj.focus()   	; // focus on the field with the problem
			phobj.select()  	; // select the contents of the field with the problem
			return false  	; // tell wrap around function that this value fails
	   }
			phobj.value = str ;//  write the value back (digits only)      
	    return true     ; // tell wrap around function that this value passes
	}

function isSelected(payrollobj){
	var payrollobj = strContact.frequencypayroll ;
	var name 	= payrollobj.name  ; // name of the field	
	var testValue = false  ; // flag
	var errMsg	= "Please select something from the list for" 
    	 errMsg += "\n  " + name + "." ;
    	 
		   if(payrollobj.value != ""){ 
         	testValue=true   
   			
    	if (testValue==false){
    	alert(errMsg) ; // tell the user what is wrong
   		payrollobj.focus()   ; // focus on the field with the problem
		payrollobj.select()  ; // select the contents of the field with the problem
		return false  ; // tell wrap around function that this value fails
  } }
    	return true     ; // tell wrap around function that this value passes
}

function isValidPostal(obj){
	var objzip = strContact.zip
    var str 	= objzip.value ; // value to be 'tested' 
    var name 	= objzip.name  ; // name of the field	
    var upStr = str.toUpperCase() ; // convert to uppercase
    var errMsg	= "Please enter your valid Postal Code." 
    	  errMsg +="\nfor " + name + ". (A Required entry)" ;
    	  errMsg +="\nYou entered \"" + upStr +"\"." ;   
    	  errMsg +="\nTry a Letter Digit Letter Space Digit Letter Digit"
    	  errMsg +="\ne.g. V1A 2K9  (Canadian Example)" 
    	  errMsg +="\n or 5 digits or 9 digits (US version)"

    objzip.value = upStr; // replace contents with Uppercase version
    //document.write(objzip.value);
    newString = upStr.match( ( /(\b(^[A-Z]\d[A-Z]\s\d[A-Z]\d))|(\d{5})|(\d{9})/g ) );
    //document.write(newString);
   if (!newString){  
   	alert(errMsg) ; // tell the user what is wrong
     	objzip.focus()   ; // focus on the field with the problem
		objzip.select()  ; // select the contents of the field with the problem
		return false  ; // tell wrap around function that this value fails
   }
    return true     ; // tell wrap around function that this value passes
}


//This function checks to make sure the entered string is in proper email format
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function ClearFocus (thisArray,focusArray) {
	var n = 0;
	var m = thisArray.length;
	
	while (n < m) {
		focusArray[thisArray[n]] = 0;
		n = n + 1;
	}
	return (focusArray);
}

function checkClientpasswd(frm)
	{
	var spasswd;
	spasswd = frm.password.value;
	
	if ((spasswd == null) || (spasswd.length < 6))
		{
	    alert("Please Enter A Valid Password");
		return false;
		}
	else
		return true;
}

function checkClientID(frm)
	{
	var sID;
	
	sID = frm.ClientID.value;
	
	if ((sID == null) || (sID.length < 4))
		{
	    alert("This is not a valid Client ID");
		return false;
		}
	else
		return true;
	}

function formValidate (formName) {
	var FocusChk = new Array(1000);
	var msg;
	var errors = "";
	
	for (var i=0; i < formName.length; i++) {
		currField = formName.elements[i];
		switch (currField.name) {
			case 'address':
				if (currField.value.length < 2) {
					errors += "- Company Address not entered in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'bustype':
				var n = currField.selectedIndex;
				if (n == 0) {
					errors += "- Type of Business was not specified.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'city':
				if (currField.value.length < 2) {
					errors += "- City not identified.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'clientNumber':
				num1 = currField.value.split("-");
				if ((num1.length != 2) || ((num1[0].length != 2) && (num1[1].length != 4))) {
					errors += "- Client Number not properly entered.\n   (e.g., 00-0000)\n";
					FocusChk[i] = 1;
				}
				break;
			case 'company':
				if (currField.value.length < 2) {
					errors += "- Company Name not provided in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'contactname':
				if (currField.value.length < 2) {
					errors += "- Contact Name not provided in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'email':
				item1 = currField.value.split("@");
				if ((item1.length != 2) || ((item1[item1.length-1] == "") || (item1[item1.length-1] == null))) {
					errors += "- E-mail Address format not valid.\n   (e.g., yourname@domain.com)\n";
					FocusChk[i] = 1;
				} else {
					item2 = item1[1].split(".");
					if (item2.length < 2) {
						errors += "- E-mail Address format not valid.\n   (e.g., yourname@domain.com)\n";
						FocusChk[i] = 1;
					}
					if ((item2[item2.length-1] == "") || (item2[item2.length-1] == null)) {
						errors += "- E-mail Address format not valid.\n   (e.g., yourname@domain.com)\n";
						FocusChk[i] = 1;
					}
				}
				break;
			case 'employees':
				if ((currField.value.length < 1) || (isNaN(currField.value))) {
					errors += "- Number of Employees not properly entered.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'interest':
				var n = currField.selectedIndex;
				if (n == 0) {
					errors += "- Primary Interest was not specified.\n";
					FocusChk[i] = 1;
				} 
				break;
			case 'problem_description':
				if (currField.value.length < 10) {
					errors += "- Comments not stated.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'inquiry':
				if (currField.value.length < 10) {
					errors += "- Inquiry not stated.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'fname':
				if (currField.value.length < 2) {
					errors += "- First Name not provided in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'lname':
				if (currField.value.length < 2) {
					errors += "- Last Name not provided in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'name':
				if (currField.value.length < 2) {
					errors += "- Name not provided in full.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'phone':
				if (currField.value.length < 10) {
					errors += "- Phone Number not completed.\n   (Include area code.)\n";
					FocusChk[i] = 1;
				}
				break;
			case 'fax':
				if (currField.value.length < 10) {
					errors += "- Fax Number not completed.\n   (Include area code.)\n";
					FocusChk[i] = 1;
				}
				break;
			case 'state_province':
				if (currField.value.length < 2) {
					errors += "- State/Province not properly identified.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'state':
				if (currField.value.length < 2) {
					errors += "- State not properly identified.\n";
					FocusChk[i] = 1;
				}
				break;
			case 'zip':
				if (currField.value.length < 5) {
					errors += "- ZIP Code not properly entered.\n   (Must contain at least 5 digits.)\n";
					FocusChk[i] = 1;
				}
				break;
		}
	}
	
	for (var j = 0; j < 1000; j++) {
		if (FocusChk[j] == 1) {
			formName.elements[j].focus();
			j = 1000;
		}
	}
	
	if (!errors) {
		return true;
	}
	
	msg = "__________________________________________________\n\n";
	msg += "This form was not submitted due to the error(s) listed below.\n";
	msg += "Please correct them and resubmit the form.\n";
	msg += "__________________________________________________\n\n";
	msg += errors;
	alert (msg);
	return false;	
}

//This function validates data entered in forms.

//FOR NOW, THIS FUNCTION WILL REMAIN IN INDIVIDUAL PAGES, DUE TO THE CUSTOMIZATION OF EACH INSTANCE OF
//THE FUNCTION.  IN THE FUTURE, IT COULD BE REWRITTEN TO BE REUSED.
/*function ValidateForm(frm)
	{
	   	
	if (!verify(strContact))
		return false;
		
	var stremail = strContact.email.value
	if (!isEmail(stremail))
	//document.write(stremail);
		{
		alert("Please Enter a Valid Email Address")
			strContact.email.focus();
		return (false);
 		}			
 	
return (true);	
}
*/


