﻿function validateForm() {
	var res=true;
	var ctrls=new Array();
	if (typeof(createControls)!="function") {
		alert("please define createControls(ctrls) function");
		throw "function createControls(ctrls) is not defined"; 
	}
	createControls(ctrls);
	res=validateControls(ctrls);
	return res;
}

function validateControls(ctrls) {
	var ctrl;
	var str;
	var vl;
	var msg='';
	for(var i=0; i<ctrls.length; i++) {
		ctrl=ctrls[i];
		str=ctrl[1]+"(ctrl)";
		if (!eval(str)) {
			msg+=ctrl[2]+'\n\r';
			markControl(ctrl);
		}else{unMarkControl(ctrl);}
	}
	if (msg!='') {alert('The following fields need your attention:'+'\n\r\n\r'+msg+'\n\r'+"Please provide the required information, thank you!"); return false;} else {return true;}
}

function getValueOfControl(ctrl) {
	return getValueOfControlById(ctrl[0]);
}

function getValueOfControlById(id) {
	var obj=document.getElementById(id);
	if (obj.tagName=='INPUT' || obj.tagName=="TEXTAREA") {
		return obj.value;}
	else if (obj.tagName=="SELECT"){
		return obj.options[obj.selectedIndex].value;
	}
}

function markControl(ctrl) {
	var obj=document.getElementById(ctrl[0]);
	obj.style.borderColor="red";
}

function unMarkControl(ctrl){
	var obj=document.getElementById(ctrl[0]);
	obj.style.borderColor="#EEEEFF";
}

function isNotEmpty(ctrl) {
	var vl=getValueOfControl(ctrl);
	var re = /.+/;
    if(!vl.match(re)) {return false;} else {return true;}
}


function isNumber(ctrl) {
	var vl=getValueOfControl(ctrl);
    var re = /^[-]?\d*\.?\d*$/;
    ctrl = ctrl.toString( );
    if (!vl.match(re) || vl=="") {return false;} else {return true;}
}

function isEMailAddr(ctrl) {
	var vl=getValueOfControl(ctrl);
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!vl.match(re)) {return false;} else {return true;}
}

function isLen(ctrl) {
	var str=getValueOfControl(ctrl);
	var maxlength=parseInt(ctrl[3]);
	var re = new RegExp('\\b.{'+maxlength+'}\\b',"ig");
    if (!str.match(re)) {return false;} else {return true;}
}

function isIdentical(ctrl) {
	var vl1=getValueOfControl(ctrl);
	var vl2=getValueOfControlById(ctrl[3]);
	//alert(vl1+";"+vl2);
	return (vl1==vl2);
}


function isDate(ctrl) {
	var entry=getValueOfControl(ctrl);
	var dateformat=null;
	if (ctrl[3]!=null) dateformat=null;
	
	if (entry.length==0) return true;

	var dt=parsedate(entry, dateformat);
	if (!dt) {return false}
    var testDate = new Date(dt.year, dt.month-1, dt.day);
	if (testDate.getDate() == dt.day) {
		if (testDate.getMonth() + 1 == dt.month) {
			if (testDate.getFullYear() == dt.year) {
                    return true;
			} 
		} 
     } 
	 return false

}
//parse the string dt and return object with properties: day, month, year
//date format is one of the follow string: 
//
function parsedate(dt, dateformat) {
	var reg = /\b(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})\b/;
	var mat=reg.exec(dt);
	if (!mat) {return false;}
	if (mat.length!=4) {return false}

	var d=new Object();
	d.year=parseInt(mat[3]);
	
	if (d.year<100) {
		if (d.year>50) {d.year=1900+d.year} else {d.year=2000+d.year}
	}
	switch (dateformat) {
		case "MM\/DD\/YYYY" :
			d.day=mat[2]; d.month=mat[1];
			return d;
			break;
		case "MM\/DD\/YY" :
			d.day=mat[2]; d.month=mat[1];
			return d;
			break;
		case "MM-DD-YYYY" :
			d.day=mat[2]; d.month=mat[1];
			return d;
			break;
		case "MM-DD-YY" :
			d.day=mat[2]; d.month=mat[1];
			return d;
			break;
		case "DD\/MM\/YYYY" :
			d.day=mat[1]; d.month=mat[2];
			return d;
			break;
		case "DD\/MM\/YY" :
			d.day=mat[1]; d.month=mat[2];
			return d;
			break;
		case "DD-MM-YYYY" :
			d.day=mat[1]; d.month=mat[2];
			return d;
			break;
		case "DD-MM-YY" :
			d.day=mat[1]; d.month=mat[2];
			return d;
			break;
		default :
			d.day=mat[1]; d.month=mat[2];
			return d;
	}
}

//validates that the entry is in military time format
function isTime(str) {
	var reg = /^(\d{1,2}):(\d{2})?$/;
//with seconds and am/pm (optional) 
//var reg = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = str.match(reg);
	if (matchArray == null) {
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];

	if (hour < 0  || hour > 23) {
		return false;
	}
	if (minute<0 || minute > 59) {
		return false;
	}
	return true;
}

//validates that the entry is URL
function isURL(ctrl) {
	var str=getValueOfControl(ctrl);
	var re = /[http:\/\/,www]?(\w+)\.(\w+)(.*)/
	if(!str.match(re)) 
		{return false;}
	else 
		{return true;}
}
	
function RegExpValid(str, template)
{	
	var str=getValueOfControl(ctrl);
	var template=ctrl[3];
	if(!str.match(template)) {
		return false;}
	else {
		return true;}
}

