/*	________________________________________________________________________________	
	Copyright © 1992-2003 Comspec Communications, Inc.									
	All Rights Reserved.																
	________________________________________________________________________________	
	Legal Notice																		
	The information contained within this document is confidential, copyrighted			
	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 Comspec Communications, Inc.											
	________________________________________________________________________________	
	Version:	1.0																	
	File:		fieldCheck.js												
	Description:	Public field check client functionality										
*/

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]);
                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(field.name).style.color='red';
			}
		} else {
			if (IE5 || N6) {
				document.getElementById(field.name).style.color='black';
			}
		}
        }

        // 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 possitive integers
        if ( type == "int" && field.value< 0 ) {
                return "needs possitive 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
                if (!field.checked) {
                        return "check at least one box\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;
//			}
//		}
//	}
//}
