var whitespace = " \t\n\r";
var digitsInUSPhoneNumber = 10;
var phoneNumberDelimiters = "()- ";

CityCode = new Array("010","020","030","040","050","060","070","080","090","036","0341");


function warnEmpty (theField, s)
{   theField.focus()
	alert(mPrefix + s + mSuffix);
    return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(mPrefix2 + s + mSuffix2)
    return false
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

//check fieldvalue IF filled, if filled check
// If not filled return true
function checkIfFilled(theField,s,fieldType){
	if(isWhitespace(theField.value) == true){
	 	theField.value = "";
       		return true;
       }
    else{
	    switch(fieldType){
	    	case "email":
	    		if(checkEmail(theField,s)){return true}
	    		else{return false}
	    		break;
	    	case "phone":
	    		if(checkPhone(theField,s)){return true}
	    		else{return false}
	    		break;
	    	case "zipcode":
	    		if(checkZipCode(theField,s)){return true}
	    		else{return false}
	    		break;
   		}
   	}
}

function checkEmail (theField, s)
{   if (isEmpty(theField.value)) return warnEmpty (theField, s) ;
    else if (!isEmail(theField.value, false))
       return warnInvalid (theField, s);
    else return true;
}

function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;

    var i = 1;
    var sLength = s.length;

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

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

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

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checkString (theField, s){
	if (isWhitespace(theField.value))
       return warnEmpty (theField, s);
    else return true;
}

function checkBoxChecked(theField,s){

	for (i=0; i<theField.length; i++) {
		if (theField[i].checked) {
			return true;
		}
	}
	alert(mPrefix + s + mSuffix);
	return false;
}

function checkZipCode(theField, s) {
	if (isEmpty(theField.value)){
		return warnEmpty (theField, s);
	}
	else{
		var Str=theField.value
		if (Str.length < 6 || Str.length > 7){
			return warnInvalid (theField, s);
		}
		else if(Str.length == 7){
			if (whitespace.indexOf(Str.charAt(4)) == -1){
				return warnInvalid (theField, s);
			}
		}
		for (i = 0 ; i < 4; i++){
			var c = Str.charAt(i);
			if (!isDigit(c)) return warnInvalid (theField, s);
		}

		for (i = Str.length-2; i < Str.length; i++){
			var c = Str.charAt(i);
			if (!isLetter(c)) return warnInvalid (theField, s);
		}
		//theField.value = reformatPostCode(Str)
		return true
	}
}



function isLetter (c){
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}


function isCityNum (PhoneNum){
	c=PhoneNum.substr(0,3);
	for (i = 0; i < CityCode.length ; i++){
		if (c == CityCode[i]) return true;
	}
	return false;
}

function reformatPhoneNum (PhoneNum){
	if (isCityNum(PhoneNum)){
		return (reformat (PhoneNum, "(", 3, ") ", 3, " ", 2, " ", 2))
	}
	else{
		return (reformat (PhoneNum, "(", 4, ") ", 2, " ", 2, " ", 2))
	}
}


function checkPhone(theField, s){
	if (isEmpty(theField.value)){
		return warnEmpty(theField,s);
	}
    else{
    var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
       if (!isPhoneNumber(normalizedPhone))
          return warnInvalid (theField, s);
       else {  // if you don't want to reformat as (123) 456-789, comment next line out
          //theField.value = reformatPhoneNum(normalizedPhone);
          return true;
       }
    }
}

function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}


function isPhoneNumber (s){
	if (s.charAt(0) != 0) return false;
	return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function isInteger(s){
	for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function changeToUpperCase(theField){
	c=theField.value ;
	c=c.toUpperCase();
	theField.value=c;
}

function checkZipCodeBelgium(theField,s)
{
	myRegExp = new RegExp()
	re = /[Bb-]/;
	re = /^[B|b](|-|\s)[1-9]{1}[0-9]{3}$/;
	//re = /^[1-9]{1}[0-9]{3}\s{0,1}?[a-zA-Z]{2}$/;
	//alert(re.test(theField.value))
	if (isEmpty(theField.value)) return warnEmpty (theField, s) ;
    else if (!re.test(theField.value))
       return warnInvalid (theField, s);
    else return true;
}

function checkOnlyLetters(theField,s)
{   if (isEmpty(theField.value)) return warnEmpty (theField, s) ;
    else if (!isLetter(theField.value, false))
       return warnInvalid (theField, s);
    else return true;
}

function checkOnlyDigits(theField,s)
{   if (isEmpty(theField.value)) return warnEmpty (theField, s) ;
    else if (!isDigit(theField.value, false))
       return warnInvalid (theField, s);
    else return true;
}

function checkDBZipStandard(theField,s,n,tarField)
{
	if(tarField.selectedIndex == 1)	{return checkZipCodeBelgium(theField,s,n)}
	if(tarField.selectedIndex == 0)	{return checkZipCode(theField,s,n)}
	else{return false}
}


