// JScript File

/*
The below is the code for the validation. Right now - the only element not working is a validation for "select your size"
The page that it is validating is:
http://www2.dupont.com/Personal_Protection/en_US/sales_support/tyvekgarment.html

Can you please make this check that the site visitor has indicated at least one size? Thanks for your help
*/


function validateNumericField(field) {
		
	var valid = ".0123456789"
	var ok = "yes";
	var temp;
	for (var i = 0; i < field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);		
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		//alert("Invalid entry! Only numbers are accepted!");
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateCharField(field) {
	var valid = "abcdefghijklmnopqrstuvwxyz0123456789"	
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		//alert("Invalid entry! Only characters and numbers are accepted!");
		field.focus();
		field.select();
		return -1;
	}
	return 1;
}



function validateZIP(field) {
	
	var valid = "0123456789-";
	var hyphencount = 0;
	var frmObj = window.document.forms[4];

 if (frmObj.country.value != "United States") {
	 //alert(frmObj.country.value);
	 return true;
 } else {
	 
	if (field.length !=5 && field.length !=10) {
		
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	
	for (var i=0; i < field.length; i++) {
	temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		
		if (valid.indexOf(temp) == "-1") {
			
			alert("Invalid characters in your zip code.  Please reenter.");
			
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			
			return false;
   		}
	}
	return true;
  }
}


//********** Validate Phone *************************//

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 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){

	// 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;
	
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validatePhone(field) {
	
	
	if ((field.value==null)||(field.value=="")){
		alert("Please Enter your Phone Number")
		
		return false
	}
	if (checkInternationalPhone(field.value)==false){
		alert("Please Enter a Valid Phone Number")
		field.value=""
		//field.focus()
		return false
	}
	return true
 }

//****************************************************

function isEmpty(field)
{	
	if (field.value.length ==0 || field.value == null)
	{ 	
		return true; 
	}
	else
	{	
		return false; 
	}
	
}


function validEmail(email) 
{
	// function to validate email address.
	invalidChars = " /:,;";
	
	//if (email == "")
		//return false;
	if (email != "" || email != null) {
	
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) 
			return false;
	}
	
	atPos = email.indexOf("@",1);
	if (atPos == -1)
		return false;
		
	if (email.indexOf("@",atPos+1) > -1)
		return false;
		
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) 
		return false;
	
	if (periodPos+3 > email.length) 
		return false;
		
	return true;
	}
}

function Scroller(iX,iY) {
	window.scrollTo(iX, iY);
}

function validZipState (frmObj) {
				// ******* Vaildate State & Zip *******
			if (frmObj.country.value == 'USA')
			{	
				if (frmObj.state.value.length != 2) {
				alert ("Please select your state");
				frmObj.state.focus();
				return false;
				}
				
				if (frmObj.zip.value.length != 5) {
				alert ("Please enter a valid 5 digit zip code");
				frmObj.zip.focus();
				return false;
				}
			} 
			else if (frmObj.country.value == 'Canada') {	
												
				if (frmObj.state.value.length != 2) {
				alert ("Please select your state");
				frmObj.state.focus();
				return false;
				}
				
				if (frmObj.zip.value.length != 7) {	
				alert ("Please enter a valid postal code. (Example: \"L7J 1W9\")");
				frmObj.zip.focus();
				return false;
				}
			}
			return true
		}

//1) Enter name of mandatory fields
//var fieldRequired = Array("FirstName", "LastName");
//2) Enter field description to appear in the dialog box
//var fieldDescription = Array("First Name", "Last Name");
	
function formCheck(formobj, fieldRequired, fieldDescription){
			
	//3) Enter dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var cursorpos = false;
	var filled    = true;
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++) {
		var obj = formobj.elements[fieldRequired[i]];
		if (obj)  {
			filled  = true;		
            str1 = 	getElemValue(obj) 		
			str1 = str1.replace( /^\s*/, "" );
            str1 = str1.replace( /\s*$/, "" );
            if (str1 == "") {
			    filled = false;
			    alertMsg += " - " + fieldDescription[i] + "\n";
				}
		    }
				
	}
	if (alertMsg.length == l_Msg){
		return true;
	}
	else{
		alert(alertMsg);
		return false;
	}
}


  function getElemValue(aObj)       
    {     
    var delim=',';
    if (typeof(aObj.type) != "undefined") 
        { 
        switch (aObj.type)
            {
		    case "select-one":
		        return aObj.options[aObj.selectedIndex].value;
    		
	        case "select-multiple":
                var j=0;
                var rtn='';
                for (j=0; j<aObj.options.length; j++) {
                    if (aObj.options[j].selected) 
                        if (aObj.options[j].value.length>0)
                        { rtn+=delim+aObj.options[j].value; }
                    }
                if (rtn.length>0) { rtn=rtn.substr(1,rtn.length-1); }
                return rtn;
            
            case 'radio':
            case 'checkbox':
               if (aObj.checked) { return aObj.value; } 
               else return '';
               
            default:
                return aObj.value;
            }
        }
    else {
        //multi-element item 
        var rtn='';
        try {
            var i=0;
            for (i=0; i<aObj.length; i++) {
                switch (aObj[i].type)
                    {
                    case "radio":
                    case "checkbox":
                        if (aObj[i].checked) { rtn+=delim+aObj[i].value; }
                        break;
                        
                    case "select-one":
                        
                        if (+aObj[i].options[aObj[i].selectedIndex].value.length>0)
                            rtn+=delim+aObj[i].options[aObj[i].selectedIndex].value;
                        break;
                        
	                case "select-multiple":
	                    var j=0;
	                    for (j=0; j<aObj[i].options.length; j++) {
	                        if (aObj[i].options[j].selected) 
	                            if (aObj[i].options[j].value.length>0)
	                            { rtn+=delim+aObj[i].options[j].value; }
	                        }
		                break;
                    
                    default:
                        rtn+=delim+aObj[i].value;
                     }
                 }
                
            } //end try
       catch(e) { }
       if (rtn.length>0) { rtn=rtn.substr(1,rtn.length-1); }
        return rtn;
        }
        
    }





function isValid(button) 
{
	frmObj = button.form;
	var items = 0;
	var hazards = 0;
	var fieldRequired;
	var fieldDescription;

	fieldRequired = Array("first","last","jobfunction","coname","address1","city","country","areacode", "phone","email","valuechain","industry","natureofinterest", "nomexthermal");
	fieldDescription = Array("First Name", "Last Name","Job Function","Company", "Address", "City", "Country", "Area Code","Phone Number","E-Mail Address","Question 1","Question 3","Question 4", "Indicate the size for your garment");

	var strArrayField = new Array()
	

	if (formCheck(frmObj, fieldRequired, fieldDescription))
	    {	
		if(!validZipState(frmObj)) {
			return false;
	    }
		

    if (! validEmail(frmObj.email.value) && !isEmpty(frmObj.email) )
		//if (! validEmail(frmObj.email.value))
		{
			alert("Email Address is invalid.");
			frmObj.email.focus();
			return false;
		}


		
		// Validate that at least one hazard was check in question 1
	
		if (frmObj.bio.checked == true) {
			
			//alert("Item 1 is checked");
			hazards++;
			
		}
		
		if (frmObj.chem.checked == true) {
							
			//alert("Item 2 is checked");
			hazards++;
					
		}
		
		if (frmObj.cutpuncture.checked == true) {
											
			//alert("Item 3 is checked");
			hazards++;
											
		}
		
		if (frmObj.dryparticulate.checked == true) {
											
			//alert("Item 4 is checked");
			hazards++;
											
		}
		
		if (frmObj.radiationnuclear.checked == true) {
															
			//alert("Item 4 is checked");
			hazards++;
															
		}
		
		if (frmObj.thermal.checked == true) {
															
			//alert("Item 4 is checked");
			hazards++;
															
		}
		
		if (frmObj.firefighterems.checked == true) {
															
			//alert("Item 4 is checked");
			hazards++;
															
		}
		
		if (frmObj.other.checked == true) {
															
			//alert("Item 4 is checked");
			hazards++;
															
		}
					
		if (hazards == 0) {
						
			alert("Please answer Question 2.");
			Scroller(1,1200);
			return false;
			
		}
		
		
		if  (frmObj.other.checked == true && isEmpty(frmObj.othertext)) {
		
				alert("In Question 2 you checked \"Other\", please specify.");
				frmObj.othertext.focus();
				return false;
		}
		
		return true;
	}
	else
	{
		return false;
	}


}			
	
function submitMain(button) 
{
	if (isValid(button))	{
	    frmObj = button.form;		
        frmObj.method = "post";
        frmObj.action = "/NOWApp/RequestGateway?page=DppLitSubmit";
        frmObj.submit();
	    return true;
	    }
	else {
	    return false;
	}					
}
	//  End -->
 
 