////////////////////////////////////////////////////////////////////////////////
//
// Field Checking Javascript Functions
//
// Revision: $Id: fieldCheck.js,v 1.10.6.1 2007/05/23 17:07:01 matt Exp $
//
////////////////////////////////////////////////////////////////////////////////
//
// Copyright @ 2007 Casale Media
// All Rights Reserved.
//
// ---------------------------------------------------------------------------
//
// Legal Notice
//
// The information contained within this document is confidential,
// coyprighted and or trade secret. No part of this document may be
// reproduced or distributed in any form or by any means, in whole
// or in part, without the prior written permission of Casale Media.
//
////////////////////////////////////////////////////////////////////////////////

function fieldCheck(form, fields, error) {

	var IE4 = (document.all && !document.getElementById) ? true : false;
	var NS4 = (document.layers) ? true : false;
	var IE5 = (document.all && document.getElementById) ? true : false;
	var N6 = (document.getElementById && !document.all) ? true : false;

        // make fields data accessible
        var fieldAry    = fields.split(',');
        var formObj     = eval('document.' + form);
        var count       = 0;
        var output      = error;

        // check each field
        while(fieldAry[count]) {

                // give vars friendly names
                var field       = eval('document.' + form + '.' + fieldAry[count]);
                var fieldName	= fieldAry[count];
                count++;
                var type        = fieldAry[count];
                count++;
                var max         = fieldAry[count];
                count++;
                var name        = fieldAry[count];
                count++;
                var required    = fieldAry[count];
                count++;

                // based on the field definition, figure out if there is an error present
                var errString = "";

                errString = checkFieldItem( field, type, max, name, required );

                if ( errString != "" ) {
                	if (errString=="field value required\n") {
                		output = output + " - " + name + "\n";
                	} else {
                		if (errString=="please enter a valid email address\n" && error=="") {
                			output = output + "Please enter a valid email address.\n";
                		} else {
                        		output = output + " - " + name + ", " + errString;
                        	}
                        }
                        if (IE5 || N6) {
				document.getElementById(fieldName).style.color='red';
			}
		} else {
			if (IE5 || N6) {
				document.getElementById(fieldName).style.color='#404040';
			}
		}
        }

        // figure out whether there was an error or not && submit
        if(output != error) {
                alert(output);
                return false;
        } else {
                return true;
        }
}

function checkFieldItem(field, type, max, name, required) {

	var freeEmail = /hotmail\.com|aol\.com|yahoo\.com|msn\.com|cs\.com|earthlink\.net|yahoo\.co\.uk|juno\.com|attbi\.com|netscape\.net|bellsouth\.net|webtv\.net|rter\.net|cox\.net|excite\.com|rediffmail\.com|yahoo\.ca|sbcglobal\.net|sympatico\.ca|yahoo\.co\.in|adelphia\.net|ntlworld\.com|hot\.ee|verizon\.net|wmconnect\.com|yahoo\.fr|prodigy\.net|att\.net|optonline\.net|rogers\.com|worldnet\.att\.net|web\.de|yahoo\.com\.au|netzero\.net|lycos\.com|alltel\.net|mindspring\.com|caramail\.com|bigpond\.com|telus\.net|btopenworld\.com|shaw\.ca|mchsi\.com|yahoo\.com\.sg|eudoramail\.com|abv\.bg|interia\.pl|blueyonder\.co\.uk|mail\.ee|btinternet\.com|tampabay\.rr\.com|earthlink\.com|frontiernet\.net|tds\.net|ameritech\.net|bright\.net|netzero\.com|latinmail\.com|indiatimes\.com|cogeco\.ca|yahoo\.com\.br|cfl\.rr\.com|telusplanet\.net|yahoo\.com\.mx|pandora\.be|insightbb\.com|chartermi\.net|mail\.com|email\.com|jippii\.fi|optusnet\.com\.au|yahoo\.com\.hk|ig\.com\.br|libero\.it|centurytel\.net|webmail\.co\.za|yahoo\.de|blackplanet\.com|plasa\.com|freemail\.hu|yahoo\.es|pacbell\.net|wanadoo\.fr|rochester\.rr\.com|yandex\.ru|gaggle\.net|cableone\.net|citlink\.net|swbell\.net|ev1\.net|cox-internet\.com|iwon\.com|talk21\.com|one\.lt|t-online\.de|neo\.rr\.com|videotron\.ca|mynet\.com|ns\.sympatico\.ca|twcny\.rr\.com|lycos\.co\.uk/;

	
        // check required fields
        if ( required == "1" && field.value == "" ) {
                return "field value required\n";
        }

        // if this field is not required, && the value is "", then just return "", not need to do the field check
        if ( required == "0" && field.value== "" ) {
                return "";
        }

        // check text filed's length
        if ( (type  == "text" || type == "password" || type == "select" || type == "file" )
                && field.value.length > max && max != 0 ) {
                return "exceeds length\n";
        }

        // check email field format
        if ( (type == "email" || type == "emailNotFree") && (!field.value.match(/^([a-zA-Z0-9_\.\-\+\&\=])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,9})$/))) {
                return "please enter a valid email address\n";
        }
	
	// check emailNotFree field format
	if ( type == "emailNotFree" && (freeEmail.test(field.value))) {
                return "please enter a non-free email address\n";
        }

        // check url field format
        if ( type == "url" && (!field.value.match(/^http:\/\/.*\.([a-zA-Z0-9]{2,9})([\/]{0,1}).*$/)) ) {
                return "invalid url\n";
        }

        // check int field format
        if ( type == "int" ) {
                var intValue = field.value.replace(/,/g, "");
                if (!intValue.match(/^\d+$/) ) {
                        return "invalid integer value\n";
                }
                // check if the value of int field exceed max
                if ( parseInt(intValue) > max && max !=0 ) {
                        return "exceeds maximum\n";
                }
        }

        // all the integers must be positive integers
        if ( type == "int" && field.value< 0 ) {
                return "needs positive integer\n";
        }

        // check rate(field type: text) format
        //if ( $field = "rate" && !(field.value =~ m/^\d+\.\d*$|^\d*\.\d+$|^\d+$/) ) {
        //      return "viper_fieldcheck_bad_rate";
        //}

        if ( type == "radio" ) {
                // handler for multiple radio boxes or check boxes
                var boxCount    = 0;
                var isError     = 1;
                while (boxCount < field.length) {
                        if (field[boxCount].checked == true) {
                                isError = 0;
                        }
                        boxCount++;
                }
                if ( isError == 1 ) return "select at least one value\n";
        }

        if (type == "checkbox") {
                // handler for single check box
		var checkedCount = 0;
		for (var i=0; i<field.length; i++) {
			if (field[i].checked) checkedCount++;
		}
		if (!checkedCount) {
			if (field.length>2) {
	                        return "check at least one box\n";
			} else {
				return "field value required\n";
			}
                }
        }

        return "";
}

//function checkAll(delimiter,formName,fieldNames, checkboxFields) {
//	var fields=fieldNames.split(delimiter);
//	var empty=0;
//	var first=1;
//	var warningStr='';
//	var cond ='';
//	var checkFieldResult='';
////Enters here
//	for (var i=0; i<fields.length;i++) {
//		cond='';
//		var obj = eval('document.'+ formName + '.' + fields[i]);i
//		checkFieldResult = checkField(fields[i], checkboxFields,delimiter,fields[i+1],formName);
//		
//		if (checkFieldResult!='0' && checkFieldResult!='1') {
//			if (document.getElementById) {
//                                // bold the field name
//                                document.getElementById(fields[i]).style.color="red";
//                                // bold the warning message
//                                //document.getElementById('message').style.color="red";
//                                //document.getElementById('message').style.fontWeight="bold";
//                                //document.getElementById('message').style.display="block";
//                                warningStr = warningStr + checkFieldResult;
//
//                        }
//                        else {
//                                warningStr = warningStr + "   -"+ fields[i+1] + '\n';
//                        }
//                        if (first) {
//                                // if this is not a checkbox, focus on the obj
//                                if (obj.focus) {
//                                        obj.focus();
//                                }
//                                else {
//                                        if (document.getElementById) {
//                                                document.getElementById(fields[i]).focus();
//                                        }
//                                }
//                                first=0;
//                        }
//                        empty="1";
//		}
//		if (checkFieldResult=='0') {
//			if (document.getElementById) {
//				// bold the field name
//				document.getElementById(fields[i]).style.color="red";
//				// bold the warning message
//				//document.getElementById('message').style.color="red";
//				//document.getElementById('message').style.fontWeight="bold";
//				//document.getElementById('message').style.display="block";
//				warningStr = warningStr + "   -"+ fields[i+1] + '\n';
//			}
//			else {
//				warningStr = warningStr + "   -"+ fields[i+1] + '\n';
//			}
//			if (first) {
//				// if this is not a checkbox, focus on the obj
//				if (obj.focus) {
//					obj.focus();
//				}
//				else {
//					if (document.getElementById) {
//						document.getElementById(fields[i]).focus();
//					}
//				}
//				first=0;
//			}
//			empty="1";
//		}
//		if (checkFieldResult=='1') {
//			if (document.getElementById) {
//				document.getElementById(fields[i]).style.color='black';
//			}  
//		}  	
//		i++;
//	}
//	if (empty=='1') {
//		if (warningStr!='') {
//			alert("Please recheck your form for the following required field\(s\) :\n\n" + warningStr);
//		}
//		return false;
//	}
//	else {
//		return true;
//	}
//}
//
//function checkField(fld,checkboxFields,delimiter,fieldName,formName) {
//	// check for a valid email address
//	if (fld == "email" || fld=="recipientEmail" || fld=="senderEmail") {
//		var objValue=eval('document.'+formName+'.'+fld+'.value');
//		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//		var freeEmail = /hotmail\.com|aol\.com|yahoo\.com|msn\.com|cs\.com|earthlink\.net|yahoo\.co\.uk|juno\.com|attbi\.com|netscape\.net|bellsouth\.net|webtv\.net|rter\.net|cox\.net|excite\.com|rediffmail\.com|yahoo\.ca|sbcglobal\.net|sympatico\.ca|yahoo\.co\.in|adelphia\.net|ntlworld\.com|hot\.ee|verizon\.net|wmconnect\.com|yahoo\.fr|prodigy\.net|att\.net|optonline\.net|rogers\.com|worldnet\.att\.net|web\.de|yahoo\.com\.au|netzero\.net|lycos\.com|alltel\.net|mindspring\.com|caramail\.com|bigpond\.com|telus\.net|btopenworld\.com|shaw\.ca|mchsi\.com|yahoo\.com\.sg|eudoramail\.com|abv\.bg|interia\.pl|blueyonder\.co\.uk|mail\.ee|btinternet\.com|tampabay\.rr\.com|earthlink\.com|frontiernet\.net|tds\.net|ameritech\.net|bright\.net|netzero\.com|latinmail\.com|indiatimes\.com|cogeco\.ca|yahoo\.com\.br|cfl\.rr\.com|telusplanet\.net|yahoo\.com\.mx|pandora\.be|insightbb\.com|chartermi\.net|mail\.com|email\.com|jippii\.fi|optusnet\.com\.au|yahoo\.com\.hk|ig\.com\.br|libero\.it|centurytel\.net|webmail\.co\.za|yahoo\.de|blackplanet\.com|plasa\.com|freemail\.hu|yahoo\.es|pacbell\.net|wanadoo\.fr|rochester\.rr\.com|yandex\.ru|gaggle\.net|cableone\.net|citlink\.net|swbell\.net|ev1\.net|cox-internet\.com|iwon\.com|talk21\.com|one\.lt|t-online\.de|neo\.rr\.com|videotron\.ca|mynet\.com|ns\.sympatico\.ca|twcny\.rr\.com|lycos\.co\.uk/;
//
//		if (freeEmail.test(objValue)){
//			return "   -Please enter a non-free email address\n"; 
//		} 
//		else{
//			if (filter.test(objValue)) {
//				return 1;
//			}
//			else {
//				if (objValue != '') {
//					return "   -Please enter a valid email address\n";
//				}
//				else {
//					return "   -" + fieldName + "\n";
//				}
//			}
//		}
//	}
//	else {
//		// at least one checkboxes must be checked
//		if (fld.match(checkboxFields) != "") {
//			alart('5');
//			// get all the checkboxes
//			var checkboxes= eval('document.'+formName+'.' + fld);
//			var checked=false;
//			for (var i=0; i<checkboxes.length;i++) {
//				if ((checked = checkboxes[i].checked)) {
//					break;
//				}
//			}
//			if (checked) {
//				return 1;
//			}
//			else {
//				return 0;
//			}
//		}
//		else {
//			var objValue=eval('document.'+formName+'.'+ fld +'.value'); 
//		
//			if (NS4) {
//				// this is a select drop down list
//				if(eval('document.'+formName+'.'+fld+'.type.toString()') == "select-one") {
//					if (eval('document.'+formName+'.'+fld+".options\[document.form."+fld+".selectedIndex\].value")=='') {
//						return 0;}
//					else {	
//						return 1; 
//					}
//				}
//			}
//			else {
//
//
//				if (eval('document.'+formName+'.'+fld+".options")) {
//					if (eval('document.'+formName+'.'+fld+".options\[document."+formName+"."+fld+".selectedIndex\].value")=='') {
//						return 0;
//					}
//					else {	return 1; }
//				}
//			}
//			if (objValue == '') {
//
//
//				return 0;
//			}
//			else {
//				return 1;
//			}
//		}
//	}
//}

function hrFieldCheck_DC() {
      var ret = fieldCheck('hrForm','firstName,text,255,First name,1,lastName,text,255,Last name,1,email,email,255,E-mail,1,phone,text,255,Phone number,1,address1,text,255,Address line 1,1,city,text,255,City,1,province,text,255,State/province,1,country,text,255,Country,1,postalCode,text,255,Zip/postal code,1,education,text,255,Education level,1,salary,text,255,Desired salary range,1,status,text,255,Employment status,1,areYouAFit,text,0,Paragraph in description section,1,resume,text,0,Cut and pasted resume missing,1,requirements,checkbox,0,Paragraph in requirements section,1,survey1,radio,0,Survey question 1,1,survey2,radio,0,Survey question 2,1,survey3,radio,0,Survey question 3,1,survey4,radio,0,Survey question 4,1,survey5,radio,0,Survey question 5,1,survey6,radio,0,Survey question 6,1,question1,text,0,Written question 1,1,question2,text,0,Written question 2,1,question3,text,0,Written question 3,1,question4,text,0,Written question 4,1,question5,text,0,Written question 5,1,question6,text,0,Written question 6,1,question7,text,0,Written question 7,1,question8,text,0,Written question 8,1','Please check your form for the following required field(s)\n\n');
      return ret;
}

function hrFieldCheck_AC() {
      var ret = fieldCheck('hrForm','firstName,text,255,First name,1,lastName,text,255,Last name,1,email,email,255,E-mail,1,phone,text,255,Phone number,1,address1,text,255,Address line 1,1,city,text,255,City,1,province,text,255,State/province,1,country,text,255,Country,1,postalCode,text,255,Zip/postal code,1,education,text,255,Education level,1,salary,text,255,Desired salary range,1,status,text,255,Employment status,1,areYouAFit,text,0,Paragraph in description section,1,resume,text,0,Cut and pasted resume missing,1,requirements,checkbox,0,Paragraph in requirements section,1,survey1,radio,0,Survey question 1,1,survey2,radio,0,Survey question 2,1,survey3,radio,0,Survey question 3,1,survey4,radio,0,Survey question 4,1,survey5,radio,0,Survey question 5,1,question1,text,0,Written question 1,1,question2,text,0,Written question 2,1,question3,text,0,Written question 3,1,question4,text,0,Written question 4,1,question5,text,0,Written question 5,1,question6,text,0,Written question 6,1,question7,text,0,Written question 7,1,question8,text,0,Written question 8,1,question9,text,0,Written question 9,1,question10,text,0,Written question 10,1','Please check your form for the following required field(s)\n\n');
      return ret;
}

function hrFieldCheck_QA() {
      var ret = fieldCheck('hrForm','firstName,text,255,First name,1,lastName,text,255,Last name,1,email,email,255,E-mail,1,phone,text,255,Phone number,1,address1,text,255,Address line 1,1,city,text,255,City,1,province,text,255,State/province,1,country,text,255,Country,1,postalCode,text,255,Zip/postal code,1,education,text,255,Education level,1,salary,text,255,Desired salary range,1,status,text,255,Employment status,1,areYouAFit,text,0,Paragraph in description section,1,resume,text,0,Cut and pasted resume missing,1,requirements,checkbox,0,Paragraph in requirements section,1,survey1,radio,0,Survey question 1,1,survey2,radio,0,Survey question 2,1,survey3,radio,0,Survey question 3,1,survey4,radio,0,Survey question 4,1,survey5,radio,0,Survey question 5,1,question1,text,0,Written question 1,1,question2,text,0,Written question 2,1,question3,text,0,Written question 3,1,question4,text,0,Written question 4,1,question5,text,0,Written question 5,1,question6,text,0,Written question 6,1,question7,text,0,Written question 7,1','Please check your form for the following required field(s)\n\n');
      return ret;
}

function hrFieldCheck_CS() {

      var ret = fieldCheck('hrForm','firstName,text,255,First name,1,lastName,text,255,Last name,1,email,email,255,E-mail,1,phone,text,255,Phone number,1,address1,text,255,Address line 1,1,city,text,255,City,1,province,text,255,State/province,1,country,text,255,Country,1,postalCode,text,255,Zip/postal code,1,education,text,255,Education level,1,salary,text,255,Desired salary range,1,status,text,255,Employment status,1,areYouAFit,text,0,Paragraph in description section,1,resume,text,0,Cut and pasted resume missing,1,requirements,checkbox,0,Paragraph in requirements section,1,survey1,radio,0,Survey question 1,1,survey2,radio,0,Survey question 2,1,survey3,radio,0,Survey question 3,1,survey4,radio,0,Survey question 4,1,survey5,radio,0,Survey question 5,1,question1,text,0,Written question 1,1,question2,text,0,Written question 2,1,question3,text,0,Written question 3,1,question4,text,0,Written question 4,1,question5,text,0,Written question 5,1,question6,text,0,Written question 6,1,question7,text,0,Written question 7,1','Please check your form for the following required field(s)\n\n');

      return ret;

}

