// JavaScript Document
/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/
// Declaring required variables
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;

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 trim(s) {
//   return s.replace(/^\s+|\s+|\s+$/, '');
// } 
function trim(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 validPhone(fld){
    var bracket=3
	if ((fld.value==null)||(fld.value=="")){
		fld.style.background = "yellow" ;
		fld.focus()
		return 0
	}
    strPhone=trim(fld.value)
    if(strPhone.indexOf("+")>1) { 
	    valid = 0 ;
        fld.style.background = 'yellow'; 		
	} else {
        if(strPhone.indexOf("-")!=-1) {
		    bracket=bracket+1 ;
	    }
        if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket) {
	    	valid = 0 ;
            fld.style.background = 'yellow'; 
			fld.focus() ;
	    } else {
            var brchr=strPhone.indexOf("(")
            if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")") {
	    	    valid = 0 ;
                fld.style.background = 'yellow'; 

	        } else if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1) {
	        	valid = 0 ;
                fld.style.background = 'yellow'; 

	        } else {
                s=stripCharsInBag(strPhone,validWorldPhoneChars);
	            if (isInteger(s) && s.length >= minDigitsInIPhoneNumber) {
		            valid = 1 ;
                    fld.style.background = 'white'; 
	            } else {
	                valid = 0 ;
                    fld.style.background = 'yellow'; 
			    }
			}
		}
	}
	if (valid == 0 ) {
	    fld.focus() ;	
	}
    return (valid);
}

function validString(fld) {
	if ((fld.value==null) || (fld.value=="")){
		fld.style.background = "yellow" ;
		fld.focus()
		return 0 ;
	}
    var valid = 0 ;
    var illegalChars = /[\!\<\>\?\~\!\@\#\$\^\&\*'\}\{\[\]\(\)\|]/ ; // allow letters, numbers, and underscores
    var tfld = trim(fld.value) ;
	if (tfld.length < 1) {
        fld.style.background = 'yellow'; 
    } else if (illegalChars.test(tfld)) {
        fld.style.background = 'yellow'; 
    } else {
        fld.style.background = 'white';
		valid = 1 ;
    }
	if (valid == 0) {
	    fld.focus() ;	
	}
    return valid;
}
function validEmail(fld) {
	if ((fld.value==null)||(fld.value=="")){
		fld.style.background = "yellow" ;
		fld.focus()
		return 0
	}    
	var valid = 0;
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
		valid = 1 ;
    }
	if ( valid == 0) {
		fld.focus() ;
	}
    return valid ;
}