// ===================================================================
// Author: David Capredoni <david.capredoni@gmail.com>
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// ===================================================================

/*
    This is a generic code to validate any form.  To use this the following has to be on the form
    1) The form field must have a id and name and it is the first variable in the validate javascript function
       i.e. <form id="frmMain" name="frmMain" action="formprocess.php" method="POST">
    2) All variables must have and id set and the field name must have an id tag that is the same with a suffix of "name"
       i.e. <tr><td id="EMAILname">Email:</td><td><input type="text" name= "EMAIL" id= "EMAIL" value=""></td></tr>
    3) Textfields and textareas can have extra checks done, there is three possible ones, to do this create an
       array and add the field name and the type of check you want.   Key of array is id of field and value is type
       of check to do:  i.e.
           var variables = new Array() 
           variables["ZIPPOSTAL"]="PostalCode";    -- postal code check
           variables["EMAIL"]="Email";   -- email field check
           variables["HPHONE"]="Numeric";   -- numeric check
           variables["HPHONE"]="Phone";   -- phone check
           variables["HPHONE"]="Mod10";   -- mod 10 check
           variables["FNAME"]="NonNumeric";   -- nonnumeric check
           variables["FNAME"]="Name";   -- alpha numeric and ' and -
    4) Checkboxes are not validated unless added to the variable array, in checkbox case, key is variable name and value is checkbox
       i.e.     variables["attr_chkInterests[]"]="Checkbox";   -- numeric check
    5)  To make fields not be check, add them to the optional array
            var optional = new Array();
            optional["prop_1036"]="prop_1036";
    6) To get the validation to work, add the validation function to the submit button
       i.e. <input type="submit" name="submit" value="submit" onclick="return validate('frmMain',variables,optional)">    
    7) To compare two fields  setMatch("password2","PASSWORD");
    8) to change the way errors appear, you can change the error class setErrorClass('formerror');
    9) to say a field has an other field  id muse have other appended then call otherit("ATTR_520")
    10)  to set minimum and max length setMinLength("PASSWORD",5);  setMaxLength("PASSWORD",5);
   
     
*/  
  

//    var RE_EMAIL = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    var RE_EMAIL = /^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/
    var RE_CA_POSTAL_CODE = /([A-Za-z]\d[A-Za-z]\d[A-Za-z]\d)/;
    var RE_CA_POSTAL_CODE_1 = /([A-Za-z]\d[A-Za-z])/;
    var RE_CA_POSTAL_CODE_2 = /(\d[A-Za-z]\d)/;
    var RE_US_POSTAL_CODE = /[0-9][0-9][0-9][0-9][0-9]/;
    var RE_PHONE_3 = /[0-9][0-9][0-9]/;
    var RE_PHONE_4 = /[0-9][0-9][0-9][0-9]/;
    var NAME_Regxp = /^([a-zA-Z\s\'\-]+)$/;
    
    var postalCountry = "";
    var matchs = new Array();
    var matchfind = new Array();
    var maxlength = new Array();
    var minlength = new Array();
    var clickedcheck = new Array();
    var clickedline = "~~";
    var conditionalcheck = new Array();
    var conditionalline = "~~";
    var labelchange = new Array();
    var agecheck = new Array();
    var errorchange = new Array();
    var profarray = new Array();
    var profloaded = 0;
    //phone validation
    var digits = "0123456789";
    // non-digit characters which are allowed in phone numbers
    var phoneNumberDelimiters = "()-_ ";  
    // characters which are allowed in international phone numbers
    // (a leading + is OK)
    var validWorldPhoneChars = phoneNumberDelimiters + "+";
    // Minimum no of digits in an international phone no.
    var minDigitsInIPhoneNumber = 10;
    
    var profPath = "";
    errorColour = "#FF0000";
    errorClass = "";
    preError = "-";
    requiredText = " is required.\n";
    postalCodeText = " is not a valid Postal Code.\n";
    postalCodeTextCA = " is not a valid Canadian Postal Code with a format \"ANA NAN\" (where A=Alpha and N=Number).\n";
    postalCodeTextUS = " is not a valid United States Zip Code with a format \"NNNNN\" (where N=Number).\n";
    emailText = " is not a valid Email.\n";
    nameText = " is not a valid name.\n";
    matchText = " do not match.\n";
    numberText = " is not a valid number.\n";
    mod10Text = " is not a valid card number.  Only enter the last 9 digits of the card.\n";
    phoneText = " is not a valid phone number.\n";
    nonnumberText = " has numbers.\n";
    errorText = "The following errors were encountered:\n\n";
    atleastText = "At least one of the following is required: ";
    ageText = " is not a valid age, the age must be at least ";
    minlengthText = " has to be a length greater than or equal to ";
    maxlengthText = " has to be a length less than or equal to ";
    profmess = " has a restricted word.\n";
    
    if(typeof(DOMParser) == 'undefined') {
    	DOMParser = function() {}
    	   DOMParser.prototype.parseFromString = function(str, contentType) {
    	   if(typeof(ActiveXObject) != 'undefined') {
    	       var xmldata = new ActiveXObject('MSXML.DomDocument');
    	       xmldata.async = false;
    	       xmldata.loadXML(str);
    	       return xmldata;
    	   } else if(typeof(XMLHttpRequest) != 'undefined') {
    	       var xmldata = new XMLHttpRequest;
    	       if(!contentType) {
    	           contentType = 'application/xml';
    	       }
    	       xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
    	       if(xmldata.overrideMimeType) {
    	           xmldata.overrideMimeType(contentType);
    	       }
    	       xmldata.send(null);
    	       return xmldata.responseXML;
    	   }
    	}
    }
    
    
    function isFrench() {
        requiredText = " Obligatoire.\n";
        postalCodeText = " n'est pas un Code Postal valide .\n";
        postalCodeTextCA = " n'est pas un Code Postal valide .\n";
        postalCodeTextUS = " n'est pas un Code Postal valide .\n";
        emailText = " n'est pas un E-mail valide.\n";
        nameText = " n'est pas un nom valide.\n";
        matchText = " ne pas \u00e9galer.\n";
        numberText = " n'est pas un nombre valide.\n";
        mod10Text = " n'est pas un num\u00e9ro de carte valide.  Entrez seulement les 9 derniers chiffres de votre carte.\n";
        phoneText = " n'est pas un num\u00e9ro de t\u00e9l\u00e9phone valide.\n";
        nonnumberText = " a des nombres.\n";
        errorText = "Les erreurs suivantes se sont produites :\n\n";
        atleastText = "Au moins un des suivre est exig\u00e9: ";
        ageText = " n'est pas un \u00e2ge valide, l'\u00e2ge doit \u00eatre au moins ";
        minlengthText = " doit \u00eatre une longueur plus grande que ";
        maxlengthText = " doit \u00eatre une longueur moins que ";
    }

    function setErrorClass(field1) {
    	errorClass = field1;
    }

    function setProfPath(field1) {
    	profPath = field1;
    }

    function setPostalCountry(field1) {
    	postalCountry = field1;
    }

    function setMatch(match1,match2) {
        matchs[match1 + "~~" + match2] = match1 + "~~" + match2;
        matchfind[match1] = match1;
    }

    function setMinLength(field1,length) {
        minlength[field1] = length;
    }

    function setMaxLength(field1,length) {
        maxlength[field1] = length;
    }

    //must be select boxes
    function setAgeCheck(dayfield,monthfield,yearfield,age) {
    	agecheck[yearfield] = monthfield + "!!" + dayfield + "!!" + yearfield + "!!" + age;
    }

    function setLabel(checkfield,labelfield) {
    	labelchange[checkfield] = labelfield;
    }

    function setError(checkfield,errorfield) {
    	errorchange[checkfield] = errorfield;
    }

    //at least one checkbox clicked
    function oneclicked(clickvalues) {   
        if (clickvalues.length > 0) {
            clickedcheck[clickvalues[0]] = clickvalues[0];
            currclickedline = "";    
            for(var i = 0; i < clickvalues.length; i++) {
                if (currclickedline != "") {
                    currclickedline += "!!"
                }
                currclickedline += clickvalues[i];        
            }
            clickedline += currclickedline + "~~"; 
        }
    }

    function otherit(oname) {
        var ovname = "";
        var ov = document.getElementById(oname);
        if (ov.options) {
	        for ( k =0; k < ov.options.length; k++) {
	            if (ov.options[k].selected) {
	                ovname = ov.options[k].value;
	            }
	        }
        }
        var oov = document.getElementById(oname + "other");
        if (oov) {
	        if (ovname.toUpperCase() == "OTHER") {
	            oov.disabled = false;
	            oov.style.backgroundColor = "#ffffff";
	        } else {
	            oov.disabled = true;
	            oov.value = "";
	            oov.style.backgroundColor = "#eeeeee";
	        }
        }
    }

    //this function is to allow one field to not be editable until a particular value on another field is selected
    function dependentFieldCheck(fieldToCheck,acceptableFieldValues,fieldToEnable) {
        selvalue = "";
        
        dproceed = 0;
        
        ftc = document.getElementById(fieldToCheck);
        if (ftc) {
            dproceed = 1;            	
        } else {
    	    if (document.getElementsByName(fieldToCheck)) {
    	    	ftc = document.getElementsByName(fieldToCheck);
    		    dproceed = 1;
    	    }
        }
        if (dproceed == 1) {
	        var fieldToCheckType = ftc.type;
	        if (fieldToCheckType == "select-one") {
	            for ( k =0; k < document.getElementById(fieldToCheck).options.length; k++) {
	                if (document.getElementById(fieldToCheck).options[k].selected) {
	                 	selvalue = document.getElementById(fieldToCheck).options[k].value;
	                }
	            }
	        } else if (fieldToCheckType == "text" || fieldToCheckType == "textarea" || fieldToCheckType == "password" || fieldToCheckType == "file") {
	        	selvalue = document.getElementById(fieldToCheck).value;
	        } else if (fieldToCheckType == "radio" || fieldToCheckType == "checkbox") {
	            for ( k =0; k < document.getElementsByName(fieldToCheck).length; k++) {	
	                if (document.getElementsByName(fieldToCheck)[k].checked) {
	                    selvalue = document.getElementsByName(fieldToCheck)[k].value;
	                }
	            }
	        } else if (fieldToCheckType == undefined) {
                if (document.getElementsByName(fieldToEnable).length != undefined) {
					for ( k =0; k < document.getElementsByName(fieldToEnable).length; k++) {	
		                if (document.getElementsByName(fieldToCheck)[k].checked) {
				            selvalue = document.getElementsByName(fieldToEnable)[k].value;
		                }
  				    }
                } else {
                	if (document.getElementById(fieldToEnable)) {
                	    selvalue = document.getElementById(fieldToEnable).value;
	                } else {
	                	if (document.getElementsByName(fieldToEnable)) {
	                		selvalue = document.getElementsByName(fieldToEnable).value;
	                	} 
	                }
                }
	        }
	
	        fieldValues = "," + acceptableFieldValues + ","
	        fieldValues = fieldValues.replace(" ,",",");
	        fieldValues = fieldValues.replace(", ",",");

        	ftce = document.getElementById(fieldToEnable);
            if (ftce) {
            } else {
        	    if (document.getElementsByName(fieldToEnable)) {
        	    	ftce = document.getElementsByName(fieldToEnable);
        	    }
            }
            var fieldToEnableType = ftce.type;
  
	        //any value is accepted will have "<ANY>"
	        if ((acceptableFieldValues == "<ANY>" && selvalue != "") || fieldValues.indexOf(","+ selvalue + ",") >= 0) {
	        	if (fieldToEnableType == "radio" || fieldToEnableType == "checkbox") {
					for ( k =0; k < document.getElementsByName(fieldToEnable).length; k++) {	
				        document.getElementsByName(fieldToEnable)[k].disabled = false;
  				    } 
	        	} else { 
	        		if (fieldToEnableType == undefined) {
                        if (document.getElementsByName(fieldToEnable).length != undefined) {
        					for ( k =0; k < document.getElementsByName(fieldToEnable).length; k++) {	
        				        document.getElementsByName(fieldToEnable)[k].disabled = false;
          				    }
                        } else {
    	                	if (document.getElementById(fieldToEnable)) {
    	                	    document.getElementById(fieldToEnable).disabled = false;
    		                } else {
    		                	if (document.getElementsByName(fieldToEnable)) {
    		                		document.getElementsByName(fieldToEnable).disabled = false;
    		                	} 
    		                }
                        }
	        		} else {
	                	if (document.getElementById(fieldToEnable)) {
	                	    document.getElementById(fieldToEnable).disabled = false;
		                } else {
		                	if (document.getElementsByName(fieldToEnable)) {
		                		document.getElementsByName(fieldToEnable).disabled = false;
		                	}
		                }
                    }
	            }
	        } else {
	        	if (fieldToEnableType == "radio" || fieldToEnableType == "checkbox") {
					for ( k =0; k < document.getElementsByName(fieldToEnable).length; k++) {	
					        selvalue = document.getElementsByName(fieldToEnable)[k].disabled = true;
					}
                } else {
	        		if (fieldToEnableType == undefined) {
                        if (document.getElementsByName(fieldToEnable).length != undefined) {
        					for ( k =0; k < document.getElementsByName(fieldToEnable).length; k++) {	
        				        document.getElementsByName(fieldToEnable)[k].disabled = true;
          				    }
                        } else {
    	                	if (document.getElementById(fieldToEnable)) {
    	                	    document.getElementById(fieldToEnable).disabled = true;
    		                } else {
    		                	if (document.getElementsByName(fieldToEnable)) {
    		                		document.getElementsByName(fieldToEnable).disabled = true;
    		                	}
    		                }
                        }
	        		} else {
	                	if (document.getElementById(fieldToEnable)) {
	                	    document.getElementById(fieldToEnable).disabled = true;
		                } else {
		                	if (document.getElementsByName(fieldToEnable)) {
		                		document.getElementsByName(fieldToEnable).disabled = true;
		                	}
		                }
                    }
        		}
	        }
        }
    }


    function enableDate() {
        var text = document.getElementById("<%=CIMXmlCreator.GNOTETYPE%>").options[document.getElementById("<%=CIMXmlCreator.GNOTETYPE%>").selectedIndex].text;
        if (text == "Reminder" || text == "Followup") {
            document.getElementById("add<%=CIMXmlCreator.GNOTETYPE%>").style.display = '';
            optional = new Array();
        } else {
            document.getElementById("add<%=CIMXmlCreator.GNOTETYPE%>").style.display = 'none';
            document.getElementById("<%=CIMXmlCreator.GNOTEFOLLOWUP_DATE%>").value = "";
            optional = new Array();
            optional["<%=CIMXmlCreator.GNOTEFOLLOWUP_DATE%>"] = "<%=CIMXmlCreator.GNOTEFOLLOWUP_DATE%>";
        }
    }



    //only check field if other field (condition) has a value (conditionvalue) and the other field is of type (conditiontype)
    function conditional(field,condition,conditionvalue,conditiontype) {
        conditionalcheck[field] = field;
        conditionalline += field + "!!" + condition + "!!" + conditionvalue + "!!" + conditiontype + "~~";
    }

    function validate(frmName, variables, optional) {
        var elem = document.getElementById(frmName).elements;
        error = 0;
        errorList = "";
        errorList2 = "~~";
        errorListCheck = "";
        for(var i = 0; i < elem.length; i++) {

   	    labelfield = elem[i].name;
	    if (elem[i].name in labelchange) {
		labelfield = labelchange[elem[i].name];
	    }
 
   	    errorfield = "";
	    if (elem[i].name in errorchange) {
		errorfield = errorchange[elem[i].name];
	    }

            //check conditional
            cmustcheck = 0;
            if (elem[i].name in conditionalcheck) {
                splitstring = conditionalline.split("~~");
                for(j = 0; j < splitstring.length; j++) {
                    if (splitstring[j] != "") {
           	        cc = splitstring[j];
                        ccsplit = cc.split("!!");
                        if (ccsplit[0] == elem[i].name) {
                        	checkfield = ccsplit[1];
                        	checkvalue = ccsplit[2];
                        	checktype = ccsplit[3];
                                if (checktype == "select-one") {
                                    for ( k =0; k < document.getElementById(checkfield).options.length; k++) {
                                        if (document.getElementById(checkfield).options[k].selected) {
                                         	if (document.getElementById(checkfield).options[k].value == checkvalue) {
                                         	    cmustcheck = 1;
                                         	}
                                        }
                                    }
                                } else if (checktype == "text" || checktype == "textarea" || checktype == "password" || checktype == "file") {
                                	if (document.getElementById(checkfield).value == checkvalue) {
                                         	    cmustcheck = 1;
                                        }
                                } else if (checktype == "radio" || checktype == "checkbox") {
					                for ( k =0; k < document.getElementsByName(checkfield).length; k++) {	
					                    if (document.getElementsByName(checkfield)[k].checked) {
	                                         	if (document.getElementsByName(checkfield)[k].value == checkvalue) {
	                                         	    cmustcheck = 1;
	                                         	}
					                    }
					                }
                                }
                                
                                if (cmustcheck == 0) {
                                	
       			                origtitle = document.getElementById(labelfield + "name").innerHTML;
                  	            if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
			                        num1 = origtitle.indexOf(">");
			                        //if (num1 > 20 && num1 < 40) {
			                            origtitle = origtitle.substr(num1+1);
			                        //}
			                    }
			                origtitle = origtitle.replace("</span>","")
			                origtitle = origtitle.replace("</SPAN>","")
			                origtitle2 = origtitle.replace("*","")
		                   	origtitle2 = origtitle2.replace(")",""); 
		                   	origtitle2 = origtitle2.replace("(",""); 
		                   	origtitle2 = origtitle2.replace("?",""); 
	                        if (errorListCheck.search(origtitle2) == -1) {
		                            document.getElementById(labelfield + "name").innerHTML = origtitle;
		                    }
                          }
                        }
                    }
                }
            }
        
            if (elem[i].type == "text" || elem[i].type == "textarea" || elem[i].type == "password" || elem[i].type == "file") {
                if (trim(elem[i].value) != "") {
                    if (elem[i].id in minlength || elem[i].id in maxlength || elem[i].id in matchfind || elem[i].id in variables) {
                        cmustcheck = 1;
                    }
                }
            }
            //DC added in disabled check
            if (elem[i].disabled == false && (elem[i].name in optional == false || cmustcheck == 1)) {
        
	            if (elem[i].type == "text" || elem[i].type == "textarea" || elem[i].type == "password" || elem[i].type == "file") {
	              if (elem[i].disabled == false) {
	                origtitle = document.getElementById(labelfield + "name").innerHTML;
	                if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
	                    num1 = origtitle.indexOf(">");
	                    //if (num1 > 20 && num1 < 40) {
	                        origtitle = origtitle.substr(num1+1);
	                    //}
	                }
	                origtitle = origtitle.replace("</span>","");
	                origtitle = origtitle.replace("</SPAN>","");

	            	if (trim(elem[i].value) == "") {
	                    error = 1;
	                    if (errorfield != "") {
	                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
	                             errorList += preError+ errorfield+ "\n";
	                             errorList2 += errorfield + "~~";
	                             errorListCheck += origtitle + "\n";
                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
	                        }
	                    } else {
	                    	origtitle2 = origtitle.replace("*","")
	                    	origtitle2 = origtitle2.replace(")",""); 
	                    	origtitle2 = origtitle2.replace("(",""); 
	                    	origtitle2 = origtitle2.replace("?",""); 
	                        if (errorList2.search("~~" + origtitle2 + requiredText+ "~~") == -1) {
	                             errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + requiredText;
	                             errorList2 += origtitle2 + requiredText+ "~~";
	                             errorListCheck += origtitle + "\n";
                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
	                        }
	                    }
	                    if (errorClass == "") {
	                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
	                    } else {
	                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
	                    }
	                } else {
	                    //check if in array
	                    if (elem[i].id in variables) {

	                        if (variables[elem[i].id].toLowerCase()  == "profanity") {
                                //do profanity check
	                            if (!profanity(elem[i].value)) { 
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
	  		                        	origtitle2 = origtitle2.replace("(",""); 
	  		                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + profmess+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + profmess;
		                                    errorList2 += origtitle2 + profmess+ "~~";
	 	                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
 		                    	    origtitle2 = origtitle2.replace("(",""); 
 		                    	    origtitle2 = origtitle2.replace("?",""); 
                 		            if (errorListCheck.search(origtitle2) == -1) {
  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
  	                                }
	                            }
	                        }
	                    	
	                        if (variables[elem[i].id].toLowerCase()  == "postalcode_1") {
                                epmess = postalCodeTextCA;
	                            if (!isValidCAPostalCode_1(removeSpaces(elem[i].value))) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
	  		                        	origtitle2 = origtitle2.replace("(",""); 
	  		                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + epmess+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + epmess;
		                                    errorList2 += origtitle2 + epmess+ "~~";
	 	                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
 		                    	    origtitle2 = origtitle2.replace("(",""); 
 		                    	    origtitle2 = origtitle2.replace("?",""); 
                 		            if (errorListCheck.search(origtitle2) == -1) {
  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
  	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "postalcode_2") {
                                epmess = postalCodeTextCA;
	                            if (!isValidCAPostalCode_2(removeSpaces(elem[i].value))) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
	  		                        	origtitle2 = origtitle2.replace("(",""); 
	  		                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + epmess+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + epmess;
		                                    errorList2 += origtitle2 + epmess+ "~~";
	 	                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
 		                    	    origtitle2 = origtitle2.replace("(",""); 
 		                    	    origtitle2 = origtitle2.replace("?",""); 
                 		            if (errorListCheck.search(origtitle2) == -1) {
  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
  	                                }
	                            }
	                        }

	                    	if (variables[elem[i].id].toLowerCase()  == "postalcode") {
	                    		var stripVal = stripCharsInBag(elem[i].value,"_ ");
	                    		//remove mask
                                if (stripVal.length > 0 || (elem[i].name in optional == false && stripVal.length == 0)) {
		                    		epmess = postalCodeText;
		                            if (!isValidCAPostalCode(removeSpaces(elem[i].value)) && (postalCountry == "" || postalCountry == "CA"))  {
	                                         epmess = postalCodeTextCA;
		                            }
	
		                            if (!isValidUSPostalCode(removeSpaces(elem[i].value)) && postalCountry == "US") {
	                                         epmess = postalCodeTextUS;
	                                }
	
		                            if ((!isValidCAPostalCode(removeSpaces(elem[i].value)) && (postalCountry == "" || postalCountry == "CA")) || (!isValidUSPostalCode(removeSpaces(elem[i].value)) && postalCountry == "US")) {
		                                error = 1;
										if (errorfield != "") {
					                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
					                             errorList += preError+ errorfield+ "\n";
					                             errorList2 += errorfield + "~~";
					                             errorListCheck += origtitle + "\n";
				                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
					                        }
					                    } else {
			                                origtitle2 = origtitle.replace("*","")
				                    	    origtitle2 = origtitle2.replace(")",""); 
		  		                        	origtitle2 = origtitle2.replace("(",""); 
		  		                        	origtitle2 = origtitle2.replace("?",""); 
			                                if (errorList2.search("~~" + origtitle2 + epmess+ "~~") == -1) {
			                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + epmess;
			                                    errorList2 += origtitle2 + epmess+ "~~";
		 	                                    errorListCheck += origtitle + "\n";
		                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
			                                }                            
		                                }
					                    if (errorClass == "") {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
					                    } else {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
					                    }
		                            } else {
		                            	origtitle2 = origtitle.replace("*","")
	 		                    	    origtitle2 = origtitle2.replace(")",""); 
	 		                    	    origtitle2 = origtitle2.replace("(",""); 
	 		                    	    origtitle2 = origtitle2.replace("?",""); 
	                 		            if (errorListCheck.search(origtitle2) == -1) {
	  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
	  	                                }
		                            }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "email") {
	                            if (!isValidEmailAddress(elem[i].value.toLowerCase())) { 
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                        	origtitle2 = origtitle2.replace("(",""); 
			                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + emailText+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + emailText;
		                                    errorList2 += origtitle2 + emailText+ "~~";
	           	                            errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
		                    	    origtitle2 = origtitle2.replace("(",""); 
		                    	    origtitle2 = origtitle2.replace("?",""); 
              		                if (errorListCheck.search(origtitle2) == -1) {
 	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
 	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "name") {
	                            if (!isValidName(elem[i].value)) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                        	origtitle2 = origtitle2.replace("(",""); 
			                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + nameText+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + nameText;
		                                    errorList2 += origtitle2 + nameText+ "~~";
	           	                            errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
		                    	    origtitle2 = origtitle2.replace("(",""); 
		                    	    origtitle2 = origtitle2.replace("?",""); 
              		                if (errorListCheck.search(origtitle2) == -1) {
 	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
 	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "phone") {
	                        	var stripVal = stripCharsInBag(elem[i].value,validWorldPhoneChars);
	                        	if (!checkInternationalPhone(elem[i].value)  && (stripVal.length > 0 || (elem[i].name in optional == false && stripVal.length == 0))) {
	                            	error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + phoneText+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + phoneText;
		                                    errorList2 += origtitle2 + phoneText+ "~~";
	            	                        errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
		                    	    origtitle2 = origtitle2.replace(")",""); 
		                     	    origtitle2 = origtitle2.replace("(",""); 
		                     	    origtitle2 = origtitle2.replace("?",""); 
               		                if (errorListCheck.search(origtitle2) == -1) {
    	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
    	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "phone_3") {
                                epmess = phoneText;
	                            if (!isValidPhone_3(removeSpaces(elem[i].value))) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
	  		                        	origtitle2 = origtitle2.replace("(",""); 
	  		                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + epmess+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + epmess;
		                                    errorList2 += origtitle2 + epmess+ "~~";
	 	                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
 		                    	    origtitle2 = origtitle2.replace("(",""); 
 		                    	    origtitle2 = origtitle2.replace("?",""); 
                 		            if (errorListCheck.search(origtitle2) == -1) {
  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
  	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "phone_4") {
                                epmess = phoneText;
	                            if (!isValidPhone_4(removeSpaces(elem[i].value))) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
	  		                        	origtitle2 = origtitle2.replace("(",""); 
	  		                        	origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + epmess+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + epmess;
		                                    errorList2 += origtitle2 + epmess+ "~~";
	 	                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
 		                    	    origtitle2 = origtitle2.replace(")",""); 
 		                    	    origtitle2 = origtitle2.replace("(",""); 
 		                    	    origtitle2 = origtitle2.replace("?",""); 
                 		            if (errorListCheck.search(origtitle2) == -1) {
  	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
  	                                }
	                            }
	                        }

	                        
	                        
	                        if (variables[elem[i].id].toLowerCase()  == "mod10") {
	                            if (!mod10(elem[i].value)) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + mod10Text+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + mod10Text;
		                                    errorList2 += origtitle2 + mod10Text+ "~~";
	            	                        errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
		                    	    origtitle2 = origtitle2.replace(")",""); 
		                     	    origtitle2 = origtitle2.replace("(",""); 
		                     	    origtitle2 = origtitle2.replace("?",""); 
               		                if (errorListCheck.search(origtitle2) == -1) {
    	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
    	                                }
	                            }

                            }

	                        if (variables[elem[i].id].toLowerCase()  == "numeric") {
	                            if (!isNumeric(elem[i].value)) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + numberText+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + numberText;
		                                    errorList2 += origtitle2 + numberText+ "~~";
	            	                        errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
		                    	    origtitle2 = origtitle2.replace(")",""); 
		                     	    origtitle2 = origtitle2.replace("(",""); 
		                     	    origtitle2 = origtitle2.replace("?",""); 
               		                if (errorListCheck.search(origtitle2) == -1) {
    	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
    	                                }
	                            }
	                        }
	                        if (variables[elem[i].id].toLowerCase()  == "nonnumeric") {
	                            if (hasNumeric(elem[i].value)) {
	                                error = 1;
									if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+ errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
				                             errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + nonnumberText+ "~~") == -1) {
		                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + nonnumberText;
		                                    errorList2 += origtitle2 + nonnumberText+ "~~";
	            	                        errorListCheck += origtitle + "\n";
		                                    errorListCheck = errorListCheck.replace(/\*/gi,"");
		                                }                            
	                                }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
	                            } else {
	                            	origtitle2 = origtitle.replace("*","")
		                    	    origtitle2 = origtitle2.replace(")",""); 
		                    	    origtitle2 = origtitle2.replace("(",""); 
		                    	    origtitle2 = origtitle2.replace("?",""); 
               		                if (errorListCheck.search(origtitle2) == -1) {
    	                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
    	                                }
	                            }
	                        }
	                    }
	                    else {
	                    	    origtitle2 = origtitle.replace("*","")
		                    	origtitle2 = origtitle2.replace(")",""); 
		                    	origtitle2 = origtitle2.replace("(",""); 
		                    	origtitle2 = origtitle2.replace("?",""); 
                                if (errorListCheck.search(origtitle2) == -1) {
                                    document.getElementById(labelfield + "name").innerHTML = origtitle;
                                }
	                    }
                        if (elem[i].id in minlength) {
                                value1 = trim(elem[i].value);
                                if (value1.length < minlength[elem[i].id]) {
                                        error = 1;
										if (errorfield != "") {
					                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
					                             errorList += preError+ errorfield+ "\n";
					                             errorList2 += errorfield + "~~";
					                             errorListCheck += origtitle + "\n";
				                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
					                        }
					                    } else {
			                                origtitle2 = origtitle.replace("*","")
				                    	    origtitle2 = origtitle2.replace(")",""); 
				                         	origtitle2 = origtitle2.replace("(",""); 
				                         	origtitle2 = origtitle2.replace("?",""); 
	                                        if (errorList2.search("~~" + origtitle2 + minlengthText +  minlength[elem[i].id]+ "~~") == -1) {
			                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + minlengthText + minlength[elem[i].id] + ".\n";
			                                    errorList2 += origtitle2 + minlengthText +  minlength[elem[i].id] +"~~";
	            	                            errorListCheck += origtitle + "\n";
	 	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
			                                }                  
			                            }          
					                    if (errorClass == "") {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
					                    } else {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
					                    }
                                } else {
		                            	origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
	 	                                if (errorListCheck.search(origtitle2) == -1) {
	                                            document.getElementById(labelfield + "name").innerHTML = origtitle;
                                        }
                                }
                        }
                        if (elem[i].id in maxlength) {
                                value1 = trim(elem[i].value);
                                if (value1.length > maxlength[elem[i].id]) {
                                        error = 1;
										if (errorfield != "") {
					                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
					                             errorList += preError+ errorfield+ "\n";
					                             errorList2 += errorfield + "~~";
					                             errorListCheck += origtitle + "\n";
				                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
					                        }
					                    } else {
			                                origtitle2 = origtitle.replace("*","")
				                    	    origtitle2 = origtitle2.replace(")",""); 
				                         	origtitle2 = origtitle2.replace("(",""); 
				                         	origtitle2 = origtitle2.replace("?",""); 
	                                        if (errorList2.search("~~" + origtitle2 + maxlengthText +  maxlength[elem[i].id]+ "~~") == -1) {
			                                    errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + maxlengthText + maxlength[elem[i].id] + ".\n";
			                                    errorList2 += origtitle2 + maxlengthText +  maxlength[elem[i].id] +"~~";
	            	                            errorListCheck += origtitle + "\n";
	 	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
			                                }                            
			                            }
					                    if (errorClass == "") {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
					                    } else {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
					                    }
                                } else {
		                            	origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
	 	                                if (errorListCheck.search(origtitle2) == -1) {
	                                            document.getElementById(labelfield + "name").innerHTML = origtitle;
                                        }
                                }
                        }

                        if (elem[i].id in matchfind) {
                            for(var matchfull in matchs) {
                                matchstring = matchfull.split("~~");
                                match1 = matchstring[0];                            
                                match2 = matchstring[1];                            
                                if (match1 == elem[i].id) {
	                                origtitle1 = document.getElementById(match2 + "name").innerHTML;
	                                if (origtitle1.toUpperCase().indexOf("<SPAN") >= 0) {
	  		                            num1 = origtitle1.indexOf(">");
	  		                            //if (num1 > 20 && num1 < 40) {
	  		                                origtitle1 = origtitle1.substr(num1+1);
			                            //}
			                        }
			                        origtitle1 = origtitle1.replace("</span>","")
			                        origtitle1 = origtitle1.replace("</SPAN>","")
	
	                                if (trim(elem[i].value) != trim(document.getElementById(match2).value)) {
		                                error = 1;
		                                origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                         	origtitle2 = origtitle2.replace("(",""); 
			                         	origtitle2 = origtitle2.replace("?",""); 
		                                origtitle2a = origtitle1.replace("*","")
			                    	    origtitle2a = origtitle2a.replace(")",""); 
			                       	    origtitle2a = origtitle2a.replace("(",""); 
			                       	    origtitle2a = origtitle2a.replace("?",""); 
		                                if (errorList2.search("~~" + origtitle2 + ", " + origtitle2a +  matchText+ "~~") == -1) {
//dc
		                                    errorList += preError+"\u0022"+ttrim(origtitle1)+"\u0022" +", " +"\u0022"+ ttrim(origtitle) +"\u0022"+  matchText;
		                                    errorList2 += origtitle2 +", " + origtitle2a +  matchText + "~~";
	 	                                    errorListCheck += origtitle + ", " + origtitle1 + "\n";
		                                }                            
					                    if (errorClass == "") {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
					                    } else {
					                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
					                    }
		                            } else {
		                            	origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                      	    origtitle2 = origtitle2.replace("(",""); 
			                      	    origtitle2 = origtitle2.replace("?",""); 
	 	                                if (errorListCheck.search(origtitle2) == -1) {
	                                            document.getElementById(labelfield + "name").innerHTML = origtitle;
	                                    }
		                            }
	                            }
	                        }
                        } 

	            	}
	              } else {
	                  //remove highlights
	                  origtitle = document.getElementById(labelfield + "name").innerHTML;
	                  if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
	                      num1 = origtitle.indexOf(">");
	                      //if (num1 > 20 && num1 < 40) {
	                          origtitle = origtitle.substr(num1+1);
  	                      //}
	                  }
	                  origtitle = origtitle.replace("</span>","")
	                  origtitle = origtitle.replace("</SPAN>","")
                      document.getElementById(labelfield + "name").innerHTML = origtitle;
	              }
	            } else if (elem[i].type == "select-one") {
	            	
		            	//age check
		            	if (elem[i].name in agecheck) {
	
				            origtitle = document.getElementById(labelfield + "name").innerHTML;
	    	                if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
			                    num1 = origtitle.indexOf(">");
			                    //if (num1 > 20 && num1 < 40) {
			                        origtitle = origtitle.substr(num1+1);
	  		                    //}
			                }
	  		                origtitle = origtitle.replace("</span>","")
			                origtitle = origtitle.replace("</SPAN>","")
	
		            	    agevalues = agecheck[elem[i].name];	
                            agesplit = agevalues.split("!!");
                            monthfield = agesplit[0];
                            dayfield = agesplit[1];
                            yearfield = agesplit[2];
                            age = agesplit[3];
                            for ( k =0; k < document.getElementById(monthfield).options.length; k++) {
                                if (document.getElementById(monthfield).options[k].selected) {
                                    month = document.getElementById(monthfield).options[k].value;
                                }
                            }

                            for ( k =0; k < document.getElementById(dayfield).options.length; k++) {
                                if (document.getElementById(dayfield).options[k].selected) {
                                    day = document.getElementById(dayfield).options[k].value;
                                }
                            }
                            for ( k =0; k < document.getElementById(yearfield).options.length; k++) {
                                if (document.getElementById(yearfield).options[k].selected) {
                                    year = document.getElementById(yearfield).options[k].value;
                                }
                            }
                            if(getAge(month,day,year) < age) {
		                        error = 1;
	
						        errorfieldage = "";
						        fa = elem[i].name + "age";
								if (fa in errorchange) {
									errorfieldage = errorchange[fa];
								}
								if (errorfieldage != "") {
				                        if (errorList2.search("~~" + errorfieldage + "~~") == -1) {
				                             errorList += preError+"\u0022"+ttrim(errorfieldage)+"\u0022" + "\n";
				                             errorList2 += errorfieldage + "~~";
		               	                     errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                } else {
			                	    origtitle2 = origtitle.replace("*","")
			                    	origtitle2 = origtitle2.replace(")",""); 
			                    	origtitle2 = origtitle2.replace("(",""); 
			                    	origtitle2 = origtitle2.replace("?",""); 
		                        	if (errorList2.search("~~" + origtitle2 + ageText + age+ "~~") == -1) {
		                              	    errorList += preError+"\u0022"+ttrim(origtitle) +"\u0022"+ ageText + age + "\n";
		                              	    errorList2 += origtitle2 + ageText + age + "~~";
		                                    errorListCheck += origtitle + "\n";
	                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
		                        	}
		                        }
			                    if (errorClass == "") {
			                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
			                    } else {
			                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
			                    }
                            } else {
                            	origtitle2 = origtitle.replace("*","")
		                    	origtitle2 = origtitle2.replace(")",""); 
		                    	origtitle2 = origtitle2.replace("(",""); 
		                    	origtitle2 = origtitle2.replace("?",""); 
		                        if (errorListCheck.search(origtitle2) == -1) {
		                            document.getElementById(labelfield + "name").innerHTML = origtitle;
		                        }
	  	                    } 
		            	}
	            	
		                for ( j =0; j < elem[i].options.length; j++) {
		                    if (elem[i].options[j].selected) { 
			                    origtitle = document.getElementById(labelfield + "name").innerHTML;
	 	                        if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
				                    num1 = origtitle.indexOf(">");
				                    //if (num1 > 20 && num1 < 40) {
				                        origtitle = origtitle.substr(num1+1);
				                    //}
				                }
				                origtitle = origtitle.replace("</span>","")
				                origtitle = origtitle.replace("</SPAN>","")

		                        if (elem[i].options[j].value == "") {
		                            error = 1;
	
				                    if (errorfield != "") {
				                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
				                             errorList += preError+errorfield+ "\n";
				                             errorList2 += errorfield + "~~";
			                                 errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    } else {
				                    	origtitle2 = origtitle.replace("*","")
			                    	    origtitle2 = origtitle2.replace(")",""); 
			                     	    origtitle2 = origtitle2.replace("(",""); 
			                     	    origtitle2 = origtitle2.replace("?",""); 
				                        if (errorList2.search("~~" + origtitle2 + requiredText+ "~~") == -1) {
				                             errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + requiredText;
				                             errorList2 += origtitle2 + requiredText + "~~";
			                                 errorListCheck += origtitle + "\n";
			                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
				                        }
				                    }
				                    if (errorClass == "") {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
				                    } else {
				                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
				                    }
		                        } else {
	                                origtitle2 = origtitle.replace("*","")
			                    	origtitle2 = origtitle2.replace(")",""); 
			                    	origtitle2 = origtitle2.replace("(",""); 
			                    	origtitle2 = origtitle2.replace("?",""); 
		                            if (errorListCheck.search(origtitle2) == -1) {
		                                document.getElementById(labelfield + "name").innerHTML = origtitle;
		                            }
		                        }
	                        }
	                    }
	            } else if (elem[i].type == "radio" || elem[i].type == "checkbox") {
	                mustcheck = 0;
	                if (elem[i].type == "radio") {
	                    mustcheck = 1;                
	                } else {
		                if (elem[i].name in variables) {
		                    if (variables[elem[i].name].toLowerCase()  == "checkbox") {
	                                mustcheck = 1;                
		                    }
	                    }
	                }

	                if (mustcheck == 1 || cmustcheck == 1) {
		                checked = 0;
		                for ( j =0; j < document.getElementsByName(elem[i].name).length; j++) {	
		                    if (document.getElementsByName(elem[i].name)[j].checked) {
		                    	checked = 1;
		                    }
		                }
		                origtitle = document.getElementById(labelfield + "name").innerHTML;
 	                    if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
 		                    num1 = origtitle.indexOf(">");
		                    //if (num1 > 20 && num1 < 40) {
		                        origtitle = origtitle.substr(num1+1);
		                    //}
		                }
		                origtitle = origtitle.replace("</span>","");
		                origtitle = origtitle.replace("</SPAN>","");

		                if (checked == 0) {
		                    error = 1;
		                    if (errorfield != "") {
		                        if (errorList2.search("~~" + errorfield + "~~") == -1) {
		                             errorList += preError+errorfield+ "\n";
		                             errorList2 += errorfield + "~~";
	                                 errorListCheck += origtitle + "\n";
	                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
		                        }
		                    } else {
		                    	origtitle2 = origtitle.replace("*",""); 
		                    	origtitle2 = origtitle2.replace(")",""); 
		                    	origtitle2 = origtitle2.replace("(",""); 
		                    	origtitle2 = origtitle2.replace("?",""); 
		                        if (errorList2.search("~~" + origtitle2 + requiredText+ "~~") == -1) {
 		                             errorList += preError+"\u0022"+ttrim(origtitle)+"\u0022" + requiredText;
 		                             errorList2 += origtitle2 + requiredText + "~~";
	                                 errorListCheck += origtitle + "\n";
	                                 errorListCheck = errorListCheck.replace(/\*/gi,"");
	                                 
		                        }
		                    }
		                    if (errorClass == "") {
		                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
		                    } else {
		                        document.getElementById(labelfield + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
		                    }
		                } else {
		                    origtitle2 = origtitle.replace("*","");
		                   	origtitle2 = origtitle2.replace(")",""); 
		                   	origtitle2 = origtitle2.replace("(",""); 
		                   	origtitle2 = origtitle2.replace("?",""); 
    		                if (errorListCheck.search(origtitle2) == -1) {
 		                        document.getElementById(labelfield + "name").innerHTML = origtitle;
 		                    }
		                }
	                }

                        //check if at least one clicked in group
	                if (elem[i].name in clickedcheck) {
	                        splitstring = clickedline.split("~~");
	    	                for(j = 0; j < splitstring.length; j++) {
	    	                    if (splitstring[j] != "") {
	                   	        cc = splitstring[j];
	                                ccsplit = cc.split("!!");
	                                if (ccsplit[0] == elem[i].name) {
	                                    onefound  = 0;
	     	                            for(k = 0; k < ccsplit.length; k++) {
	      	                                if (ccsplit[k] != "") {
								                for ( l =0; l < document.getElementsByName(ccsplit[k]).length; l++) {	
								                    if (document.getElementsByName(ccsplit[k])[l].checked) {
								                    	onefound = 1;
								                    }
								                }
	                                        }
	                                    }
	                                    if (onefound == 0) {
	                                          errorList += preError + atleastText;
	                                          errorList2 += atleastText + "~~";
	                                    }
	     	                            for(k = 0; k < ccsplit.length; k++) {
	      	                                if (ccsplit[k] != "") {
							                    origtitle = document.getElementById(ccsplit[k] + "name").innerHTML;
			 	                                if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
							                        num1 = origtitle.indexOf(">");
							                        //if (num1 > 20 && num1 < 40) {
							                            origtitle = origtitle.substr(num1+1);
							                        //}
							                    }
							                    origtitle = origtitle.replace("</span>","")
							                    origtitle = origtitle.replace("</SPAN>","")
	                                            if (onefound == 0) {
							                        error = 1;
							                        errorList += "\u0022" + ttrim(origtitle)+"\u0022";
							                        errorList2 += origtitle + "~~";
							                        if (k < (ccsplit.length -1)) {
							                            errorList += ",";
							                            //errorList2 += ",";
							                        }
			       	                                errorListCheck += origtitle + "\n";
			                                        errorListCheck = errorListCheck.replace(/\*/gi,"");
								                    if (errorClass == "") {
								                        document.getElementById(ccsplit[k] + "name").innerHTML = "<span style='white-space:nowrap;color:"+ errorColour  + "'>" + origtitle + "</span>";
								                    } else {
								                    	document.getElementById(ccsplit[k] + "name").innerHTML = "<span style='white-space:nowrap' class='"+ errorClass  + "'>" + origtitle + "</span>";
								                    }
									            } else {
									            	    origtitle2 = origtitle.replace("*","")
								                    	origtitle2 = origtitle2.replace(")",""); 
								                    	origtitle2 = origtitle2.replace("(",""); 
								                    	origtitle2 = origtitle2.replace("?",""); 
				            		                    if (errorListCheck.search(origtitle2) == -1) {
									                        document.getElementById(ccsplit[k] + "name").innerHTML = origtitle;
									                    }
									            }
			                                }
	                                    }
	                                    if (onefound == 0) {
	                                          errorList += "\n";
	                                          //errorList2 += "\n";
	                                    }
	    	                        }
                  	        }
	                    }        
	                }

	            }
            } else {
                origtitle = document.getElementById(labelfield + "name").innerHTML;
		    	if (origtitle.toUpperCase().indexOf("<SPAN") >= 0) {
		    	    num1 = origtitle.indexOf(">");
	                origtitle = origtitle.substr(num1+1);
	            }
		        origtitle = origtitle.replace("</span>","");
		    	origtitle = origtitle.replace("</SPAN>","");
		    	document.getElementById(labelfield + "name").innerHTML = origtitle;
            }
        } 
        if (error == 1) {
            errorList = errorList.replace(/:/g,"");
            errorList = errorList.replace(/\*/g,"");
            errorList = errorList.replace(/<B[^>]*?>/ig,"");
            errorList = errorList.replace(/<\/B>/ig,"");
            errorList = errorList.replace(/&amp;/ig,"&");
            errorList = errorList.replace(/&nbsp;/ig," ");

            errorfield = errorText;
	    if ("error" in errorchange) {
		errorfield = errorchange["error"];
	    }
            alert(errorfield + errorList);
        	return false;
        } else {
            return true;
        }
	}
	
	function ttrim( str ) {
        str = str.replace(/&nbsp;/ig," ");
		str = rTrim( lTrim( str ) );
        str = str.replace(/:/g,"");
        str = str.replace(/\*/g,"");
        str = str.replace(/<B[^>]*?>/ig,"");
        str = str.replace(/<\/B>/ig,"");
        str = str.replace(/&amp;/ig,"&");
        str = str.replace(/(^\s*)|(\s*$)/gi,"");
   	    str = str.replace(/[ ]{2,}/gi," ");
  	    str = str.replace(/\n /,"\n");
  	    return str;
	}
	
	
	function isEmpty( str ) {   
        return ( ( str == null ) || ( str.length == 0 ) );
    }
	
	function lTrim( str ) {
        return str.replace( /^\s+/, '' );
    }

    function rTrim( str ) {
        return str.replace( /\s+$/, '' );
    }

    function trim( str ) {
        str = str.replace(/(^\s*)|(\s*$)/gi,"");
   	    str = str.replace(/[ ]{2,}/gi," ");
  	    str = str.replace(/\n /,"\n");
        return rTrim( lTrim( str ) );
    }
	
	function isValidEmailAddress( str ) {
        return RE_EMAIL.test( trim( str ) );
    }

	function isValidName( str ) {
        return NAME_Regxp.test( trim( str ) );
    }

    function isValidCAPostalCode( str ) {
        return RE_CA_POSTAL_CODE.test( trim( str ) );
    }

    function isValidPhone_3( str ) {
    	return RE_PHONE_3.test( trim( str ) );
    }

    function isValidPhone_4( str ) {
    	return RE_PHONE_4.test( trim( str ) );
    }
    
    function isValidCAPostalCode_1( str ) {
        return RE_CA_POSTAL_CODE_1.test( trim( str ) );
    }

    function isValidCAPostalCode_2( str ) {
        return RE_CA_POSTAL_CODE_2.test( trim( str ) );
    }
    
    function isValidUSPostalCode( str ) {
        return RE_US_POSTAL_CODE.test( trim( str ) );
    }

    function mod10(sText) {
        var IsMod10=true;
        var optimumnumber=sText;
        var optimumlength=sText.length -1;  //8 for optimum
        var sum=0;
        var multiplier=sText.length;  //9 for optimum
        for (i=0; i < optimumlength; i++) {
            var optimumdigit=optimumnumber.charAt(i);
            sum=sum+(optimumdigit * multiplier);
            multiplier=(multiplier - 1);
        }

        optimumtest=sum.toString();
        stringlength = (optimumtest.length - 1);
        var tester=optimumtest.charAt(stringlength);
        if (tester != 0) tester = ( 10 - tester); 
        var tester2 = sText.charAt(8);
        if (tester != tester2)  {
            IsMod10=false;
        }
        return IsMod10;
    }

    function isNumeric(sText) {
        var ValidChars = "0123456789%-()";
        var IsNumber=true;
        var Char;

        for (i = 0; i < sText.length && IsNumber == true; i++) { 
            Char = sText.charAt(i); 
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
        return IsNumber;
    }
    
    function hasNumeric(sText) {
        var ValidChars = "0123456789%-()";
        var hasNumber=false;
        var Char;

        for (i = 0; i < sText.length && hasNumber == false; i++) { 
            Char = sText.charAt(i); 
            if (ValidChars.indexOf(Char) >= 0) {
                hasNumber = true;
            }
        }
        return hasNumber;
    }

    function removeSpaces(string) {
    	var tstring = "";
	    string = '' + string;
    	splitstring = string.split(" ");
  	    for(i = 0; i < splitstring.length; i++)
  	        tstring += splitstring[i];
	    return tstring;
    }
    
	
	
    function getAge(dob_month, dob_day, dob_year) {

        var yearNow;
        var monthNow;
        var dob_year;

        var now = new Date();
        var today = new Date(now.getYear(),now.getMonth(),now.getDate());

        yearNow = now.getYear();
        if (yearNow < 200) {
            yearNow += 1900;
        }
        if (dob_year < 200) {
            dob_year += 1900;
        }

        monthNow = now.getMonth();
        dateNow = now.getDate();

        dob_month = parseInt(dob_month);
        dob_day   = parseInt(dob_day);
        dob_year  = parseInt(dob_year);

        var age = yearNow-dob_year;
        if((dob_month >= monthNow)&&(dob_day > dateNow)) {
          age--;
        }
        return age;
    }

    function checkDate(month, day, year) {

       var originalDate = month + "/" + day + "/" + year;
       var checkDate = new Date(originalDate);

       if((checkDate.getMonth()+1) == month) {
         return true; 
       } else {
         return false;
       }
    }
	
	function isInteger(s)
	{   var i;
	    for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character is number.
	        var c = s.charAt(i);
	        if (((c < "0") || (c > "9"))) return false;
	    }
	    // All characters are numbers.
	    return true;
	}
	function stripwhite(s)
	{   var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not a whitespace, append to returnString.
	    for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character isn't whitespace.
	        var c = s.charAt(i);
	        if (c != " ") returnString += c;
	    }
	    return returnString;
	}

	function stripCharsInBag(s, bag)
	{   var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character isn't whitespace.
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    return returnString;
	}

	function checkInternationalPhone(strPhone){
		var bracket=3
		strPhone=stripwhite(strPhone)
		if(strPhone.indexOf("+")>1) return false
		if(strPhone.indexOf("-")!=-1)bracket=bracket+1
		if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
		var brchr=strPhone.indexOf("(")
		if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+4)!=")")return false
		if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
	
	//formatting of phone and postal code
	function loadPostal(id,spot) {
        pcode = document.getElementById(id);
        var text = pcode.value.toUpperCase();
        var text1 = "";
        var numbercheck = 0;
        for (i = 0; i < text.length; i++) {  
            var c = text.charAt(i);
            if (numbercheck == 1) {
                if (((c >= "0") && (c <= "9"))) {
                    text1 += c;
                    numbercheck = 0;
                }
            } else {
                if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(c) >= 0) {
                    text1 += c;
                    numbercheck = 1;
                }
            }
        }
        position = text1.length
        if (text1.length > 6) {
            text1.substr(0,6);
        } else {
            i = text1.length;
            while (i < 6) {
                text1+= "_";
                i++;
            }
        }
        if (position >= 3) {
            position++;
        }
        var newtext = text1.substr(0,3)+" " + text1.substr(3,3);         
        pcode.value = newtext;
        if (spot != 0) {
	        if (pcode.setSelectionRange) {
	           pcode.focus();
	           pcode.setSelectionRange(position,position);
	        } else {
	            if(pcode.createTextRange) { 
					range=pcode.createTextRange(); 
					range.collapse(true); 
					range.moveEnd('character',position); 
					range.moveStart('character',position); 
					range.select(); 
			    } 
	        }
        }
    }

	function profanity(value) {
		if (profloaded == 0) {
			url = profPath + "prof.xml";
			callback_function = "profread"; 
			var http_request = false; 
		    if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		        http_request = new XMLHttpRequest(); 
		        if (http_request.overrideMimeType) { 
		            http_request.overrideMimeType('text/xml'); 
		        } 
		    } else if (window.ActiveXObject) { // IE 
		        try { 
		            http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
		        } catch (e) { 
		            try { 
		                http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
		            } catch (e) {} 
		        } 
		    } 

		    if (!http_request) { 
		        alert('Unfortunately you browser doesn\'t support this feature.'); 
		        return false; 
		    } 
		    //syncronous call
		    http_request.open('GET', url, false); 
		    http_request.send(null);
	        profread(http_request.responseText);
	        profloaded = 1;
		}
		return profcheck(value);	
	}
		  
    function profcheck(value) {  
		var swords = value.split(" ");
		for(jb = 0; jb < swords.length; jb++) {
			if (swords[jb].toUpperCase() in profarray) {
				return false; 
			} 
		}
		return true;
    }
	 
	function profread(result_string) { 
		    result_string = result_string.replace( /^\s+/, '' );
 	  	    result_string = result_string.replace( /\s+$/, '' );
            var xmlobject = (new DOMParser()).parseFromString(result_string, "text/xml");
			// get a reference to the root-element "fields"
			var root = xmlobject.getElementsByTagName('words')[0];
			var fielda = root.getElementsByTagName("word");
			for (ib = 0 ; ib < fielda.length; ib++) {
				profarray[fielda[ib].firstChild.nodeValue.toUpperCase()] = fielda[ib].firstChild.nodeValue.toUpperCase();
			}
	} 
	
    function loadPhone(id,spot) {
        phone = document.getElementById(id);
        var text = phone.value;
        var text1 = "";
        for (i = 0; i < text.length; i++) {  
            var c = text.charAt(i);
            if (((c >= "0") && (c <= "9"))) {
                text1 += c;
            }
        }
        position = text1.length
        if (text1.length > 10) {
            text1.substr(0,10);
        } else {
            i = text1.length;
            while (i < 10) {
                text1+= "_";
                i++;
            }
        }
        if (position < 3) {
            position++;
        } else if (position >= 3 && position < 6) {
            position+=3;
        } else {
            position+=4;
        }
        var newtext = "(" + text1.substr(0,3)+") " + text1.substr(3,3) +"-" + text1.substr(6,4);         
        phone.value = newtext;
        if (spot != 0) {
	        if (phone.setSelectionRange) {
	           phone.focus();
	           phone.setSelectionRange(position,position);
	        } else {
	            if(phone.createTextRange) { 
					range=phone.createTextRange(); 
					range.collapse(true); 
					range.moveEnd('character',position); 
					range.moveStart('character',position); 
					range.select(); 
			    } 
	        }
        }
    }
	