



// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
	var error = "";
	if (strng == "") {
	   error = "Please enter your password.\n";
	}
    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    

function checkStringLength (strng,length) {
var error = "";
if (strng == "") {
   error = "EMPTY";
}
  	var illegalChars = /\W/;// allow letters, numbers, and underscores
 	
 	var legalChars = /\w\s/;//
 	
    if (strng.length <length ) {
       error = "WRONGLENGTH";
    }
    
return error;
}       


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {

var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(str,strVal,length){

  var resultVal=checkStringLength (str,length)
  //alert(resultVal);
  switch(resultVal){
  	case "EMPTY": 
		alert("The "+strVal+" Should not be Empty")
		return false
	    break;
  	case "WRONGLENGTH": 
		alert("The "+strVal+" Should have atleast "+length+" Character(s)")
		return false
		break;
	case "ILLEGALCHARS": 
		alert("Please enter only alpa numeric and / or _ in "+strVal+"")
		return false
		break;
	case "INVALID":
	    alert("Please enter Valid"+strVal);
	    return false
		break;
	default : 
		return true;
  }//end switch
  }//end fn


// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}    


function isBlank(testStr)  {
	var error = "";

	 if ( (testStr == null) || (testStr.length == 0) ){    // nothing entered?
	   error = "The required information has not been filled in.\n"
	   alert(error)
	 }
	 //for (var i = 0; i < testStr.length; i++){  // all spaces?
	 //  if (testStr.charAt(i) != " ")
	  //   return false;
	 //}
	 return true;
}




function isNumeric (s,fieldname)
{   var i;
	//theField = eval("document.forms[0]." + fieldname)
	theField = eval("document.forms[0]." + fieldname)
    if (isEmpty(s)) 
	{
		setTimeout("theField.focus()",1500)
		setTimeout("theField.select()",0)
       if (isNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isNumeric.arguments[1] == true);
	 }

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
		{
		setTimeout("theField.focus()",1500)
     	setTimeout("theField.select()",0)
		return false;
		}
    }

    return true;
}

function isFormattedNumeric (s,specialchar)
{   var i;
    if (isEmpty(s)) 
	{
       if (isNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isNumeric.arguments[1] == true);
	 }

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (!isDigit(c) && !(c == specialchar))
		{
	   error = "please use numbers.\n"
	   alert(error)
		}
    }
    return true;
}


function checkNumber1 (thisField) { // This will kick out anything that's not a number. use onBlur()
    re = new RegExp ("[^0-9]");
    if ((re != null) && (re.test(thisField.value))) {
      alert("Health Card ID consists of numbers only. Please see your ID Card");
      thisField.focus();
      thisField.select();
      return false;
    }
    return true;
}

function checkNumber2 (thisField,length) { // This will kick out anything that's not a number. use onBlur()
    re = new RegExp ("[^0-9]");
    if ((re != null) && (re.test(thisField.value))) {
      alert("Zip Code consists of numbers only. Please enter a correct Zip Code");
      //thisField.focus();
      //thisField.select();
      return false;
    }
    return true;
}
//Checking State code


function checkState(strng,strVal)
 {
  
  var temp=strng.length;
  var validState= new RegExp ("^[a-zA-Z]+$");
 if(temp==null|| temp=="")
 {
	alert("please enter State Code");
    return false;
   } 
 if(temp==validState || temp > 2)
  {
    alert("please enter a valid State Code");
    return false;
   } 
 
  else
     return true;
  }
 // checking ZIp code
 function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}




function checkEmail (strng,strVal) {
if (strng == "") {
   alert("Please enter Requestor Email Address.")
   return false
}
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       alert("Please enter a valid email address in "+strVal)
       return false
    }else {
//test email for illegal characters
       var illegalChars= "/[\(\)\<\>\,\;\:\\\"\[\]]/"
         if (strng.match(illegalChars)) {
          alert("The email address contains illegal characters in "+strVal)
          return false
       }
    }
return true;    
}

// phone number - strip out delimiters and check for 10 digits

function checkPhoneold (strng,strVal) {
if (strng == "") {
   alert("Please enter "+strVal);
   return false;
}
var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
      alert(strVal+" contains illegal characters.");
  	  return false;
    }    
    if (!(stripped.length == 10)) {
		alert(strVal+" is wrong length, It should contain at least 10 numerals.\n Make Sure to include area code.");
		return false;
    } 
return true;
}
function checkPhone (strng,strVal) {

if (strng == "") {
   alert("Please enter "+strVal);
   return false;
}
   var objRegExp  = /^[1-9]\d{2}\-\s?\d{3}\-\d{4}$/;
    if(!objRegExp.test(strng) && isEmptyC(strng))
    {
         alert("Phone Number is wrong format.\n    Example: 999-999-9999 \n");    
     	return false;
    }
    return true;
}
function isEmptyC(objFormFieldValue)
{		
    var strValue = objFormFieldValue;
    strValue = strValue.split(" ").join("")
    if(strValue.length<1)
    {
        return false;
    }
    return true;
}
function validateFieldValue(strng,strVal){
if(strng==""){
	  alert("Please enter "+strVal);
  	  return false;
    }
return true;
}
//preventing the user from giving invalid values such as special characters etc
function validateNameOnKeyPress(name,e) {
var key;
var keychar;
if(window.event) {
// for IE, e.keyCode or window.event.keyCode can be used
key = e.keyCode;
}
else if(e.which) {
// netscape
key = e.which;
}
else {
// no event, so pass through
return true;
}
var REControlsChar = /[^a-zA-Z._0-9 ]/;
var strPass = name.value;
var lchar = String.fromCharCode(key);
if( (x = lchar.search(REControlsChar) ) != -1) {
	event.returnValue = "";
	name.value = strPass;
	return false;
   }
}
// validate Bulk order Forms  checkOthers Order Quantity	
function checkForEmptyQtyForms(count)
{
  var frmName="";
  var frmqtyNum=0;
  var othfrmName="";
  var othqtyNum=0;
  var frmStatus="no";
  var othStatus="no";
  if (count !=0)
  {
	  for(k=0;k<count;k++){	   		
	   		frmqtyNum  = document.getElementById("qty"+k).value;	   		
	   		if (frmqtyNum != null && frmqtyNum != "")
	   		{
	   			frmqtyNum = parseInt(frmqtyNum);
	   		}
	   		if (frmqtyNum != 0)
	  		{
	  			frmStatus="yes";	  			
	  		} 
	  		if (frmStatus=="yes")
	  		{
	  			break;
	  		}
	  }
  }	  
  for(j=0;j<3;j++){
   		othfrmName = document.getElementById("othfrm"+j).value;
   		othqtyNum  = document.getElementById("orderQty"+j).value;
   		if (parseInt(othfrmName.length) != 0)
  		{
  			othStatus="yes";
  		}
  		if (othStatus=="yes")
  		{
  			break;
  		}
  }
  if (othStatus == "no" && count == 0)
  {
  	alert("Please enter form name and quantity for other form. Quantity must be between 5 and 250.");
  	return false;
  } 
  else if (frmStatus == "no" && othStatus == "no")
  {
  	alert("Please enter quantity for at least one form");
  	return false;
  }
  return true;
}

function checkOthersQuantity()
{
  var flag="N";
  var frmName;
  var qtyNum;
  
  
	for(k=0;k<3;k++){
	   frmName = document.getElementById("othfrm"+k).value;
	   qtyNum  = document.getElementById("orderQty"+k).value;
	
	   if(frmName != "" || qtyNum != "")
	   {
//	     flag="Y";	     
	     if(qtyNum == "")
	     {
	        alert("Please enter a quantity for "+frmName+". Quantity must be between 5 and 250.");
	        document.getElementById("orderQty"+k).focus();
	        return false;
	     }     
	     if(qtyNum != "")
	     {
	        if(validateQuantity(qtyNum,"Quantity")==false)
	        {
	            document.getElementById("orderQty"+k).focus();
	        	return false;
	        }
	     }
	   }//end of if
  	}//end of for

  //alert("flag-->"+flag);
/**
  if(flag=="N")
  {
    alert("Please enter a quantity for other form. Quantity must be between 5 and 250.");
    document.getElementById("othfrm0").focus();
    return false;
  }
*/  
  return true;
}
 
 
// validation for non-numeric characters in the order Quantity field in BulkorderForm and BulkorderCertificates form
function validateOrderQtyOnKeyPress(name,e) {
var key;
var keychar;
if(window.event) {
// for IE, e.keyCode or window.event.keyCode can be used
key = e.keyCode;
}
else if(e.which) {
// netscape
key = e.which;
}
else {
// no event, so pass through
return true;
}
var REControlsChar = /[^._0-9 ]/;
var strPass = name.value;
var lchar = String.fromCharCode(key);
if( (x = lchar.search(REControlsChar) ) != -1) {
	event.returnValue = "";
	name.value = strPass;
	return false;
   }
}	
//This function will validate the plan number ,group number and Quantity
function validatePlan()
{
  var flag="N";
  var planNum;
  var grpNum;
  var qtyNum;
//alert("Inside validatePlan");
for(k=0;k<6;k++){
   planNum = document.getElementById("plan"+k).value;
   grpNum  = document.getElementById("grp"+k).value;
   qtyNum  = document.getElementById("qty"+k).value;

   if(planNum != "" || grpNum != "" || qtyNum != "")
   {
     flag="Y";
     if(planNum == "" || grpNum == "" || qtyNum == "")
     {
        alert("Please enter Group Number, Order Quantity for "+ planNum+".\n Enter Form Number if known.");
        document.getElementById("grp"+k).focus();
        return false;
     }
     if(qtyNum != "")
     {
        if(validateQuantity(qtyNum,"Quantity")==false)
        {
        	document.getElementById("qty"+k).focus();
        	return false;
        }
     }
   }//end of if
  }//end of for

  //alert("flag-->"+flag);

  if(flag=="N")
  {
    alert("Please enter values for at least One Plan");
    document.getElementById("plan0").focus();
    return false;
  }
  return true;
}
//This function will the range of the Quantity
function validateQuantity(strng,strVal){
	var qtyNum;
    qtyNum=parseInt(strng,10);	
	if(qtyNum < 5){
		alert("Please enter a quantity between min(5) and max(250).\nThe quantity you entered is "+strng);
		return false;
	}
	if(qtyNum > 250){
		alert("Please enter a quantity between min(5) and max(250).\nThe quantity you entered is "+strng);
		return false;
	}
	return true;
}
function validationQuantity(count,forms){	
		var checkCondition;
		var formNumber;
		var qtyNumber;
		var minimumQty;
		var maximumQty;
		var otherFormName;
		if (forms)
		{
			formNumber = document.getElementById("frm"+count).value;
			if((document.getElementById("qty"+count).value).length != 0){		
				qtyNumber  = parseInt(document.getElementById("qty"+count).value);
				minimumQty = parseInt(document.getElementById("minQuantity"+count).value);
				maximumQty = parseInt(document.getElementById("maxQuantity"+count).value);		
				if(qtyNumber < minimumQty){
					alert("Please enter a quantity between min("+ minimumQty + ") and max(" + maximumQty +").\nThe quantity you entered is " +qtyNumber);
					document.getElementById("qty"+count).focus();
					return false;
				}
				if(qtyNumber > maximumQty){
					alert("Please enter a quantity between min("+ minimumQty + ") and max(" + maximumQty +").\nThe quantity you entered is " +qtyNumber);
					document.getElementById("qty"+count).focus();
					return false;
				}
			}//end of if
		} else {
			otherFormName = document.getElementById("othfrm"+count).value;
			if (!(otherFormName == ""))
			{
				if((document.getElementById("orderQty"+count).value).length != 0){
					qtyNumber  = parseInt(document.getElementById("orderQty"+count).value);				
					minimumQty = "5";
					maximumQty = "250";				
					if(qtyNumber < parseInt(minimumQty)){
						alert("Please enter a quantity between min("+ minimumQty + ") and max(" + maximumQty +").\nThe quantity you entered is " +qtyNumber);
						document.getElementById("orderQty"+count).focus();
						return false;
					}
					if(qtyNumber > parseInt(maximumQty)){
						alert("Please enter a quantity between min("+ minimumQty + ") and max(" + maximumQty +").\nThe quantity you entered is " +qtyNumber);
						document.getElementById("orderQty"+count).focus();
						return false;
					}
				}	
			}
		}
    return true;
}// end of validationQuantity()

