// JavaScript Document
String.prototype.trim = function() {
	return this.ltrim().rtrim();
};

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
//***********************************************************************************
var formfieldnames = new Array(0); 
var fieldtypes = new Array(0); 
var fieldtitles = new Array(0); 
var fieldvaltype = new Array(0);

function commonfieldvalidationjs(myform){
	var invalidfields = new Array(0);
	xnum=0;
	while (xnum < formfieldnames.length){
		if(fieldtypes[xnum] == "email") {
			if(fieldvaltype[xnum] == "required"){
				if(!isEmail(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push('Valid ' + fieldtitles[xnum]);
				}		
			}
			else if(!(myform.elements[formfieldnames[xnum]].value.trim() == "")){
				if(!isEmail(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push('Valid ' + fieldtitles[xnum]);
				}						
			}
		}
		//*************************************
		else if(fieldtypes[xnum] == "url") {
			if(fieldvaltype[xnum] == "required"){
				if(!validurl(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push(fieldtitles[xnum]);
				}			
			}
			else if(!(myform.elements[formfieldnames[xnum]].value.trim() == "")){
				if(!validurl(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push('Valid ' + fieldtitles[xnum]);
				}						
			}
		}
		//*************************************
		else if(fieldtypes[xnum] == "select") {
			if(myform.elements[formfieldnames[xnum]].selectedIndex == 0){
				invalidfields.push(fieldtitles[xnum]);
			}			
		}
		//*************************************
		else if(fieldtypes[xnum] == "phone") {
			myform.elements[formfieldnames[xnum]].value = myform.elements[formfieldnames[xnum]].value.replace(/-/g, ".");
			if(fieldvaltype[xnum] == "required"){				
				if(!checkInternationalPhone(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push('Valid ' + fieldtitles[xnum]);
				}			
			}
			else if(!(myform.elements[formfieldnames[xnum]].value.trim() == "")){
				if(!checkInternationalPhone(myform.elements[formfieldnames[xnum]].value.trim())){
					invalidfields.push('Valid ' + fieldtitles[xnum]);
				}						
			}
		}
		//*************************************
		else{
			if(myform.elements[formfieldnames[xnum]].value.trim() == ""){
				invalidfields.push(fieldtitles[xnum]);
			}			
		}
		xnum++;
	} 
	//*************************************
	if (invalidfields == ""){return true;}
	else {alert('Please enter values for the following:\n\r' + invalidfields.join("\n\r")); return false;}
}
//***********************************************************************************
function isEmail(s){ 
    if (s.trim() == "") return false;
    
    // there must be >= 1 character before @, so we;
    // start looking at character position 1 ;
    // (i.e. second character);
    var i = 1;
    var sLength = s.length;

    // look for @;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .;
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .;
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
//***********************************************************************************
function validurl(url) {
	return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\%"\,\{\}\\|\\\^\[\]`]+)?$/);
}
//***********************************************************************************;
// 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 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
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+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
 //***********************************************************************************;

