﻿var typeInt = 1;
var typeFloat = 2;
var typeString = 3;
var typeDate = 4;
var typePassword = 5;
var typeEmail = 6;
var daysofmonth = new Array();
var daysofmonthLY = new Array();

var iArrayMax = 30;

var aDropdown = new Array(iArrayMax);
//as the page loads - first thing to do is to load the dropdown array
var bOk = LoadArrays();

daysofmonth[0] = -10;
daysofmonth[1] = 31;
daysofmonth[2] = 28;
daysofmonth[3] = 31;
daysofmonth[4] = 30;
daysofmonth[5] = 31;
daysofmonth[6] = 30;
daysofmonth[7] = 31;
daysofmonth[8] = 31;
daysofmonth[9] = 30;
daysofmonth[10] = 31;
daysofmonth[11] = 30;
daysofmonth[12] = 31;
for (var i=0; i<=12; i++) {
	daysofmonthLY[i] = daysofmonth[i];
}
daysofmonthLY[2] = 29;

function LeapYear(year) {
	if ((year/4)   != Math.floor(year/4))   return false;
	if ((year/100) != Math.floor(year/100)) return true;
	if ((year/400) != Math.floor(year/400)) return false;
	return true;
}

function ValidDate(syear, smonth, sday) {
	var year = Number(syear);
	var month = Number(smonth);
	var day = Number(sday);
	if (day<=0 || day>31) {
		return false;
	}
	if (month<=0 || month>12)
		return false;
	if ( (LeapYear(year) && (day > daysofmonthLY[month])) || (!LeapYear(year) && (day > daysofmonth[month])) ) {
		return false;
 	} else {
		return true;
	}
}

function empty(str) {
	var trim = /^\s+/;
	str = str.replace(trim, "");
	if (str.length==0)
		return true;
	else return false;
}

function checkDate(obj, whichType, strLabel) {
	//whichType==1--YYYY/MM/DD
	//whichType==2--MM/DD/YYYY
	//whichType==3--DD/MM/YYYY
	if (!strLabel) strLabel="Date";
	if (!whichType)
		whichType=1;
	var str = obj.value;
	var ok = false;
	switch (whichType) {
		case 2:
		case 3:
			ok=str.match(/^\d{1,2}[.\-\/]\d{1,2}[.\-\/]\d{1,4}/);break;
		case 1:
		default:
			ok=str.match(/^\d{1,4}[.\-\/]\d{1,2}[.\-\/]\d{1,2}/);
	}
	if (ok) {
		var cutter = /[.\-\/]/;
		var datevalue = str.split(cutter);
		var isValid=false;
		switch (whichType) {
			case 2:
				isValid=ValidDate(datevalue[2],datevalue[0],datevalue[1]);break;
			case 3:
				isValid=ValidDate(datevalue[2],datevalue[1],datevalue[0]);break;
			case 1:
			default:
				isValid=ValidDate(datevalue[0],datevalue[1],datevalue[2]);
		}
		if (isValid) {
			obj.value=""+obj.value;
			return true;
		} else {
			alert("Invalid Date");
			obj.value="";
			return false;
		}
	}
	alert(strLabel+" cannot be empty.");
	obj.value="";
	return false;
}

function checkInt(obj, min, max, field_name) {
	if (empty(obj.value)) return true;

	var str = obj.value;
	var anum=/^(\-)?(\d+$)/; 
	var fn="Value";
	if (!field_name) {
		fn="Value";
	} else fn=field_name;
	
	if (anum.test(str)) {
		if (typeof(max)=="undefined") {
			if (str<min) {
				alert(fn+" must be at least "+min);
				obj.value="";
				return false;
			}
			return true;
		}
		if (typeof(min)=="undefined") {
			return true;
		}
		if (str<min || str>max) {
			alert(fn+" must be within "+min+" and "+max);
			obj.value = "";
			return false;
		}
		return true;
	} else {
		alert("Integer Required!!!");
		obj.value = "";
		return false;
	}
}

function checkFloat(obj, min, max, field_name) {
	if (empty(obj.value)) return true;

	var str = obj.value;
//		var anum=/(^\d+$)|(^\d+\.\d+$)/
	var anum=/^(\-)?((\d+$)|(\d+\.\d+$))/;
	var fn="Value";
	if (!field_name) {
		fn="Value";
	} else fn=field_name;
	if (anum.test(str)) {
		if (typeof(max)=="undefined") {
			if (str<min) {
				alert(fn+" must be at least "+min);
				obj.value="";
				return false;
			}
			return true;
		}
		if (typeof(min)=="undefined") {
			return true;
		}
		if (str<min || str>max) {
			alert(fn+" must be within "+min+" and "+max);
			obj.value = "";
			return false;
		}
		return true;
	} else {
		alert("Decimal Number Required");
		obj.value="";
		return false;
	}
}

function checkString(obj, min, field_name) {
	var fn="Text";
	if (!field_name) {
		fn="Text";
	} else fn=field_name;

	var trim = /^\s+/;
	str = obj.value;
	str = str.replace(trim, "");
	if (str.length<min) {
		alert(fn + " must be at least " + min + " characters");
		obj.value="";
		return false;
	}
	return true;
}

function checkPassword(obj, min, field_name) {
	var fn="Password";
	if (!field_name) {
		fn="Password";
	} else fn=field_name;
	if (obj.value.length<min) {
		alert(fn + " must be at least " + min + " characters");
		obj.value="";
		return false;
	}
	return true;
}

function checkEmail(obj, field_name) {
	if (empty(obj.value)) return true;
	var fn="Email";
	if (!field_name) {
		fn="Email";
	} else fn=field_name;
	var str=obj.value;

	var filter = /^(([_\.0-9A-Za-z-]+@)([0-9A-Za-z][0-9A-Za-z\-]+\.)+([A-Za-z]{2,3}))$/;
	if (filter.test(str)) {
		filter = /((\.){2,})|(\-\.)|(\-){2,}/
		if (!filter.test(str)) {
			return true;
		}
	}
	alert(fn + "'s format is invalid.");
	return false;
}

function FormValidation(obj) {
	this.fields = new Array();
	this.fields_name = new Array();
	this.fields_type = new Array();
	this.fields_attr_1 = new Array();
	this.fields_attr_2 = new Array();
	this.field_count = 0;
	this.addItem = add_item;
	this.checkFill = check_fill;
	this.form_name = eval("document."+obj);
}

function add_item(obj, field_name, field_type, attr_1, attr_2) {
	this.fields[this.field_count] = eval("this.form_name."+obj);
	eval("this.fields_name[this.field_count] = field_name");
	eval("this.fields_type[this.field_count] = field_type");
	eval("this.fields_attr_1[this.field_count] = attr_1");
	eval("this.fields_attr_2[this.field_count] = attr_2");
	eval("this.field_count++");
}

function check_fill() {
	for (var i=0; i<this.field_count; i++) {
		if (this.fields[i].disabled==true)
			continue;
		switch (this.fields[i].type) {
			case "password":
				if (checkPassword(this.fields[i],this.fields_attr_1[i],this.fields_name[i])==false)
					return 0;
				break;
			case "text":
			case "hidden":
				if (empty(this.fields[i].value)) {	//It is text box
					alert('Field "'+this.fields_name[i]+'" must be filled in');
					return 0;
				}
				switch(this.fields_type[i]) {
					case 1 : if (checkInt(this.fields[i],this.fields_attr_1[i],this.fields_attr_2[i],this.fields_name[i])==false)
								return 0;
							break;
					case 2: if (checkFloat(this.fields[i],this.fields_attr_1[i],this.fields_attr_2[i],this.fields_name[i])==false)
								return 0;
							break;
					case 3: if (checkString(this.fields[i],this.fields_attr_1[i],this.fields_name[i])==false)
								return 0;
							break;
					case 4: if (checkDate(this.fields[i],this.fields_attr_1[i])==false)
								return 0;
							break;
					case 6: if (checkEmail(this.fields[i])==false)
								return 0;
							break;
					default: break;
				}
				break;
			case "textarea" :
				if (empty(this.fields[i].value)) {	//It is text box
					alert('Field "'+this.fields_name[i]+'" must be filled in');
					return 0;
				}
				break;
			case "select-one" : 
				if (this.fields[i].options) {	//check whether it is select
					if (!(this.fields[i].selectedIndex >=1)) {
						alert('Field "'+this.fields_name[i]+'" must be filled in');
						return 0;
					}
				}
				break;
			case "radio":
			default: 	//It should be radio
				if (this.fields[i].length) {	//check whether it is radio button
					is_checked=0;
					for (var j=0; j<this.fields[i].length; j++) {
						if (this.fields[i][j].checked==true) {
							is_checked=1;
							break;
						}
					}
					if (is_checked==0) {
						alert('Field "'+this.fields_name[i]+'" must be filled in');
						return 0;
					}
				}
				break;
		}
	}
	return 1;
}

function fncEncode(str) {
	var slash=/%/g;
	str=str.replace(slash,"%%");
	var slash=/\\/g;
	str=str.replace(slash,"%/");
	return str;
}

function fncDecode(str) {
	var intLen=str.length;
	var strDecode="";
	var intEscape=0;
	for (var i=0; i<intLen;i++) {
		switch(str.charAt(i)) {
		case '%':			
			if (intEscape==1) {
				strDecode+="%";
				intEscape=0;
			} else intEscape=1;
			break;
		case '/':
			if (intEscape==1) {
				strDecode+="\\";
				intEscape=0;
			} else strDecode+="/";
			break;
		default:
			strDecode+=str.charAt(i);
		}
	}	
	return strDecode;
}

function LoadArrays() {
//this can be thought of as an object array, each element can be identified as a property of the array position
// a very powerful structure for 'client side' data caching.

Array[0] = new sElement('M',' < 156','Under 155');
Array[1] = new sElement('M','Between 156 And 162','156 - 162');
Array[2] = new sElement('M','Between 163 And 169','163 - 169');
Array[3] = new sElement('M','Between 170 And 176','170 - 176');
Array[4] = new sElement('M','Between 177 And 182','177 - 182');
Array[5] = new sElement('M',' > 182','Over 183');

Array[6] = new sElement('F',' < 156','Under 155');
Array[7] = new sElement('F','Between 156 And 162','156 - 162');
Array[8] = new sElement('F','Between 163 And 169','163 - 169');
Array[9] = new sElement('F','Between 170 And 176','170 - 176');
Array[10] = new sElement('F','Between 177 And 182','177 - 182');
Array[11] = new sElement('F',' > 182','Over 183');

Array[12] = new sElement('B',' < 76','Under 75');
Array[13] = new sElement('B','Between 76 And 85','76 - 85');
Array[14] = new sElement('B','Between 86 And 95','86 - 95');
Array[15] = new sElement('B','Between 96 And 105','96 - 105');
Array[16] = new sElement('B','Between 106 And 115','106 - 115');
Array[17] = new sElement('B','Between 116 And 125','116 - 125');
Array[18] = new sElement('B','Between 126 And 135','126 - 135');
Array[19] = new sElement('B','Between 136 And 145','136 - 145');
Array[20] = new sElement('B',' > 145','Over 146');

Array[21] = new sElement('G',' < 76','Under 75');
Array[22] = new sElement('G','Between 76 And 85','76 - 85');
Array[23] = new sElement('G','Between 86 And 95','86 - 95');
Array[24] = new sElement('G','Between 96 And 105','96 - 105');
Array[25] = new sElement('G','Between 106 And 115','106 - 115');
Array[26] = new sElement('G','Between 116 And 125','116 - 125');
Array[27] = new sElement('G','Between 126 And 135','126 - 135');
Array[28] = new sElement('G','Between 136 And 145','136 - 145');
Array[29] = new sElement('G',' > 145','Over 146');

Array[30] = new sElement('A',' > 0','ALL');

return true;
}

function sElement(sParentId,sValue,sDescription) {	
	// elements that will be loaded into the array structure and persisted
	// think of it as an object.
	this.ParentId = sParentId;
	this.Id = sValue;
	this.Description = sDescription;
}

function bCascadeDrop(oDDsource, oDDdest) {
	//function to enable cascading dropdowns
	//called as the parent dropdown changes.
	var iX=0;
	var sText="";
	var iY=0;
	var sOptionId="";
	var sOptionDesc="";
	var iStartPos="";
	var iArray=30;
	
	//find the value of the item currently selected		
	sText = oDDsource.options[oDDsource.selectedIndex].value;
	if (sText != '0')	{
		//clear down the destination list box
		oDDdest.options.length = 0;        
		
		//loop through the elements that are in the array
		// if they match the parent if then they should be displayed.
		for (iX=0; iX<=iArray; iX++) {
			if(sText == Array[iX].ParentId) {
			//grab the values out of the element (Netscape is not happy doing too many things in a function call!)
			sOptionId = Array[iX].Id;
			sOptionDesc= Array[iX].Description;
			//alert(sOptionDesc)
			
			//write the element into the dripdown box.		
			oDDdest.options[iY] = new Option (sOptionDesc,sOptionId);
			iY = iY + 1;
			}
		}
	}
}

	function showadv(advid){
//		document.form_quest.intQuestNum.value=advid;
//		var objType=eval("document.form.answertype_"+advid+".selectedIndex");
		var objType = advid;

	

				document.getElementById("adv1").style.display = "none";
				document.getElementById("adv2").style.display = "none";
				document.getElementById("adv3").style.display = "none";
				document.getElementById("adv4").style.display = "none";
				document.getElementById("adv5").style.display = "none";
				document.getElementById("adv6").style.display = "none";
				document.getElementById("adv7").style.display = "none";
				document.getElementById("adv8").style.display = "none";
		
		    document.getElementById("adv"+advid).style.display = "";
	}
