function submitForm() {
	if ( window.validateForm ) {
		hideNormMsg();
		var isValid = window.validateForm();
		if ( !isValid ) {
			//window.document.all.validationError.style.display = 'block';
			//window.document.all.error_message.innerHTML = error_msg;
			getElement('validationError').style.display = 'block';
			getElement('error_message').innerHTML = error_msg;
		} else {
			//window.document.all.validationError.style.display = 'none';
			//window.document.all.error_message.innerHTML = error_msg;
			getElement('validationError').style.display = 'none';
			getElement('error_message').innerHTML = error_msg;
		}
		return isValid;
	}
	return true;
}

function resetForm() {
	if (window.clearForm) {
		hideAllMsg();
		return window.clearForm();
	} else if (document.forms[0]) {
		document.forms[0].reset();
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) { this[i] = 30; }
		if (i==2) { this[i] = 29; }
   } 
   return this;
}

function isValidDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	return true;
}

function isSelected( fieldObj ) {
	if ( fieldObj.value == '' ) {
		return false;
	}
	return true;
}

// Checks for valid phone number
function phoneCheck( fieldObj, fieldName ) {
	var isValid = true;
	var phoneValue = strip( fieldObj );
	if ( phoneValue.length != 0 ) {
		if ( !strCheck( phoneValue, 'numeric' ) ) {
			isValid = false;
		}
		if ( phoneValue.length != 10 ) {
			isValid = false;
		}
	}
	if ( !isValid ) {
		return false;
	}
	return true;
}

// Checks postal code validity
function postalCheck( fieldObj, fieldName )
{
	var isValid = true;
	var postalValue = strip( fieldObj );
	if ( postalValue.length != 0 ) {
		if ( !strCheck( postalValue, 'numeric' ) ) {
			isValid = false;
		}
		if ( postalValue.length != 5 && postalValue.length != 9 ) {
			isValid = false;
		}
	}
	if ( !isValid ) {
		return false;
	}
	return true;
}

// Check field values
function strCheck( fieldValue, type )
{
	var letters;
	var LETTERS;
	if ( type == 'alphabetic' ) {
		var chars="abcdefghijklmnopqrstuvwxyz- ";
		var CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZ- ";
	}
	else if ( type == 'alphabetic_nospace' ) {
		var chars="abcdefghijklmnopqrstuvwxyz-";
		var CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
	}
	else if ( type == 'numeric' ) {
		var chars="0123456789 ";
		var CHARS="0123456789 ";
	}
	else if ( type == 'numeric_nospace' ) {
		var chars="0123456789";
		var CHARS="0123456789";
	}
	else if ( type == 'alphanumeric' ) {
		var chars="01234567890abcdefghijklmnopqrstuvwxyz- ";
		var CHARS="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ- ";
	}
	else if ( type == 'alphanumeric_nospace' ) {
		var chars="01234567890abcdefghijklmnopqrstuvwxyz-";
		var CHARS="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
	}
	else {
		return true;
	}
	var i;
	if ( fieldValue.length != 0 ) {
		if ( fieldValue.length > 1 ) {
			for( i = 0; i < fieldValue.length; i++ ) {
				alpha = strCheck( fieldValue.substring( i, i+1 ), type );
				if( !alpha ) {
					return alpha;
				}
			}
			return alpha;
		}
		else {
			if( chars.indexOf( fieldValue ) >= 0 || CHARS.indexOf( fieldValue ) >= 0 ) {
				return true;
			}
			return false;
		}
	}
	return true;
}

// Check password field length and matching
function passwordCheck( passwdObj, verifyObj )
{
	if ( passwdObj.value != verifyObj.value ) {
		return false;
	}
	return true;
}

// Email address validation
function emailCheck( emailObj ) {
	var emailStr = emailObj.value;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) {
    	return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	  	for (var i=1;i<=4;i++) {
	    	if (IPArray[i]>255) {
				return false;
	    	}
    	}
    	return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {	
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {		
		return false;
	}
	if (len<2) {
   		var errStr = "This address is missing a hostname!";		
		return false;
	}
	return true;
}

// Form field validation
function fieldCheck( fieldObj, minSize, fieldName, fieldType )
{
	var isValid = true;
	var fieldStr = fieldObj.value;
	if ( !strCheck( fieldObj.value, fieldType ) ) {
		isValid = false;
	}
	if ( fieldStr.length<minSize ) {
		isValid = false;
	}
	if ( !isValid ) {
		return false;
	}
	return true;
}

// Formats postal code
function formatPostal( fieldObj )
{
	var postalValue = strip(fieldObj);
	if (postalValue.length > 5) {
		fieldObj.value = postalValue.substring(0,5)+'-'+postalValue.substring(5,9);
	}
	else {
		fieldObj.value = postalValue;
	}
}

function setBirthDate( srcDate, toDate ) {
	var preformat = srcDate.value;
	if ( preformat != '' && preformat.length == 10 ) {
		var month = preformat.substring(0,2);
		var day = preformat.substring(3,5);
		var year = preformat.substring(6,10);
		toDate.value = year + '-' + month + '-' + day;
	}
}
