// ****************************   Form Validator *********************************
// summery:			validate input values accoriding to parameters in:
//					arrInputs() - Containing all fieldValidate objects.
//					fieldValidate.inputName - the name of the inputs to be checked.
//					fieldValidate.type - the type of the input to be checked.
//					fieldValidate.userName - the user name of the input to be checked.
//					fieldValidate.isMust - a flag inticating whether the field is a must field.
// created by:	 	boaz bibas - Daronet
// created/:	 	05/01/2001
// last updated:	05/02/2001
// *******************************************************************************


function fieldValidate(strInputName, intType, strUserName, bolIsMust) {
	this.inputName = strInputName;
	this.type = intType;
	this.userName = strUserName;
	this.isMust = bolIsMust;
}

function cleanString(str) {
	for (; str.charAt(str.length-1) == " ";) {
		str = str.slice(0,(str.length-2));
	}
	for (; str.charAt(0) == " ";) {
		str = str.slice(1,(str.length-1));
	}
	return str;
}

function cleanSpace(str) {
	var arrTemp = str.split(" ");
	str = arrTemp.join("");
	arrTemp = str.split("-");
	str = arrTemp.join("");
	return str;
}

function isValidCharacter(str, strType) {
	var isMail = false;
	var isURL = false;
	var isFile = false;
	if (strType == "eMail") 
		isMail = true;
	else if (strType == "URL")
		isURL = true;
	else if (strType == "file")
		isFile = true;
	for (i=0; i<str.length; i++) {
		if (!isMail && str.charCodeAt(i) == 64)
			return false;
		if (!isURL && !isFile && (str.charCodeAt(i) == 58 || str.charCodeAt(i) == 41 || str.charCodeAt(i) == 40))
			return false;
		if (!isURL && str.charCodeAt(i) == 47)
			return false;
		if (!isFile && str.charCodeAt(i) == 92)
			return false;
		if (str.charCodeAt(i)<45 || str.charCodeAt(i)>122 || str.charCodeAt(i)==96 || (str.charCodeAt(i)>58 && str.charCodeAt(i)<63) || (str.charCodeAt(i)>90 && str.charCodeAt(i)<92) || (str.charCodeAt(i)>92 && str.charCodeAt(i)<95)) {
			return false;
		}
	}
	return true;
}

function isValidDate(DateStr) {
	var DateStrArr = DateStr.split("/");
	for (i=0; i<DateStrArr.length-1; i++) {
		if (DateStrArr[i].charAt(0) == " " || DateStrArr[i].charAt(0) == "0") {
			DateStrArr[i] = DateStrArr[i].substring(1,DateStrArr[i].length)
		}
	}
	var tempDateStr = DateStrArr[1]+"/"+DateStrArr[0]+"/"+DateStrArr[2];
	var dateToCheckObj = new Date(tempDateStr);
	return ((dateToCheckObj.getMonth()+1)+"/"+dateToCheckObj.getDate()+"/"+dateToCheckObj.getFullYear() == tempDateStr);
}

function IsValidData(objForm) {
	var arInputs;
	if (arguments.length > 1) {
		arInputs = arguments[1];
	}
	else {
		arInputs = arrInputs;
	}
	var strCheck = "";
	var isValid = true;
	for (count=0; count<arInputs.length; count++) {
		if (objForm[arInputs[count].inputName]) {
			strCheck = cleanString(""+objForm[arInputs[count].inputName].value).toLowerCase();
			switch (arInputs[count].type) {
				case 0: {						// Text
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 1: {						// Number
					var arrTempValue = strCheck.split(".");
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (arrTempValue.length > 2) {
						alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					else {
						for (iCounter=0; iCounter < arrTempValue.length; iCounter++) {
							if (isNaN(arrTempValue[iCounter])) {
								alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
								objForm[arInputs[count].inputName].focus();
								objForm[arInputs[count].inputName].select();
								isValid = false;
								return isValid;
							}
						}
					}
					break;
				}
				case 2: {						// TextArea
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 3: {						// Radio Button
					if (arInputs[count].isMust) {
						var bSelected = false;
						var oControl = objForm[arInputs[count].inputName];
						if (oControl.length) {
							for (nIndex = 0; nIndex < oControl.length; nIndex++) {
								if (oControl[nIndex].checked) {
									bSelected = true;
									break;
								}
							}
						}
						else {
							bSelected = oControl.checked;
						}
						
						if (!bSelected) {
							alert("אנא בחר "+arInputs[count].userName);
							return bSelected;
						}
					}
					break;
				}
				case 4: {						// Checkbox
					if (arInputs[count].isMust) {
						var bSelected = false;
						var oControl = objForm[arInputs[count].inputName];
						if (oControl.length) {
							for (nIndex = 0; nIndex < oControl.length; nIndex++) {
								if (oControl[nIndex].checked) {
									bSelected = true;
									break;
								}
							}
						}
						else {
							bSelected = oControl.checked;
						}
						
						if (!bSelected) {
							alert("אנא בחר "+arInputs[count].userName);
							return bSelected;
						}
					}
					break;
				}
				case 5: {						// Select
					if (arInputs[count].isMust && (objForm[arInputs[count].inputName].selectedIndex == -1 || objForm[arInputs[count].inputName].value == "")) {
						alert("! אנא בחר " + arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 6: {						// Hidden
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						isValid = false;
						return isValid;
					}
					break;
				}
				case 7: {						// File
					if (arInputs[count].isMust && (strCheck == "" && !(objForm["Exist_"+arInputs[count].inputName]))){
						alert("! לא נבחר קובץ בשדה " + arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					var arrFilePath = strCheck.split("\\");
					var strFileName = arrFilePath[arrFilePath.length-1];
					if (!isValidCharacter(strFileName, "file")) {
						alert("שם הקובץ שנבחר אינו חוקי!");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 8: {						// Date
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (!isValidDate(strCheck) && strCheck != "") {
						alert("! התאריך שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 9: {						// eMail
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (strCheck != "" && (((strCheck.lastIndexOf('@')<1)||(strCheck.lastIndexOf('.')<1)||(strCheck.lastIndexOf('.')<2+strCheck.lastIndexOf('@'))||(strCheck.lastIndexOf('.')>strCheck.length-3)||(strCheck.lastIndexOf('.')<strCheck.length-5)||(strCheck.lastIndexOf(' ')!=-1)||(strCheck.lastIndexOf('.@')!=-1)||(strCheck.lastIndexOf('@.')!=-1)))) {
						alert("! הכתובת שהוזנה בשדה " + arInputs[count].userName + " אינה תקפה");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 10: {						// Currency
					var arrTempValue = strCheck.split(".");
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (arrTempValue.length > 2) {
						alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					else {
						if (arrTempValue.length > 1) {
							if (isNaN(arrTempValue[arrTempValue.length-1]) || arrTempValue[arrTempValue.length-1].length > 2) {
								alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
								objForm[arInputs[count].inputName].focus();
								objForm[arInputs[count].inputName].select();
								isValid = false;
								return isValid;
							}
						}
						var arrTempValue = arrTempValue[0].split(",");
						if (isNaN(arrTempValue[0])) {
							alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
							objForm[arInputs[count].inputName].focus();
							objForm[arInputs[count].inputName].select();
							isValid = false;
							return isValid;
						}
						for (iCounter=1; iCounter < arrTempValue.length; iCounter++) {
							if (isNaN(arrTempValue[iCounter]) || arrTempValue[iCounter].length != 3) {
								alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
								objForm[arInputs[count].inputName].focus();
								objForm[arInputs[count].inputName].select();
								isValid = false;
								return isValid;
							}
						}
					}
					break;
				}
				case 11: {						// URL
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if ((arInputs[count].isMust || strCheck != "") && (strCheck.indexOf("http://") != 0 || !isValidCharacter(strCheck, "URL") || strCheck=="http://") || strCheck=="http://www.") {
					//if ((arInputs[count].isMust || strCheck != "") && ((strCheck.indexOf("www.") != 0 && strCheck.indexOf("http://www.") != 0) || strCheck.lastIndexOf(".")<strCheck.indexOf("www.")+6 || !isValidCharacter(strCheck, "URL"))) {
						alert("! ה-URL שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 12: {						// Select Multiple
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						isValid = false;
						return isValid;
					}
					break;
				}
				case 13: {						// Counter
					break;
				}
				case 14: {						// DHTML control
					objForm[arInputs[count].inputName].value = document.all[arInputs[count].inputName+"Control"].Dom.body.innerHTML;
					strCheck = objForm[arInputs[count].inputName].value
					if (arInputs[count].isMust && strCheck == "") {
						alert("! לא הוזן תוכן ב" + arInputs[count].userName);
						document.all[arInputs[count].inputName+"Control"].focus();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 15: {						// Phone - Pax
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (isNaN(cleanSpace(strCheck)) || (strCheck.length < 7 && strCheck.length > 0)) {
						alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 16: {						// Password
					if (strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (strCheck.length < 2 || strCheck.length > 10 || strCheck.indexOf(" ") != -1) {
						alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					break;
				}
				case 17: {						// Credit Card
					var arrTempValue = strCheck.split(".");
					if (arInputs[count].isMust && strCheck == "") {
						alert("אנא הזן "+arInputs[count].userName);
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					if (arrTempValue.length > 2) {
						alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
						objForm[arInputs[count].inputName].focus();
						objForm[arInputs[count].inputName].select();
						isValid = false;
						return isValid;
					}
					else {
						for (iCounter=0; iCounter < arrTempValue.length; iCounter++) {
							if (isNaN(arrTempValue[iCounter])) {
								alert("! המידע שהוזן בשדה " + arInputs[count].userName + " אינו תקף");
								objForm[arInputs[count].inputName].focus();
								objForm[arInputs[count].inputName].select();
								isValid = false;
								return isValid;
							}
						}
					}
					break;
				}
				default: {						// Custom
					if (!customCheck(objForm, arInputs[count], strCheck))
						return false;
					else
						continue;
				}
			}
		}
	}
	return isValid;
}



function dbsHTTPGetRequest(sURL) {
	try {
		var xmlHTTP;
		// code for IE
		if (window.ActiveXObject) {
			xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
			xmlHTTP.open("GET",sURL, false);
			xmlHTTP.send();
			return xmlHTTP;
		}		
		// code for Mozilla, etc.

		else if (window.XMLHttpRequest) {
			xmlHTTP = new XMLHttpRequest();
			xmlHTTP.open("GET",sURL, false);
			xmlHTTP.send(null);
			return xmlHTTP;
		}		
		
	}
	 catch (e) {
	 	return null;
	}
}

function dbsPlaceMailData(url) {
	try {
		var xmlHTTP = dbsHTTPGetRequest(url);
		if (xmlHTTP != null) {
			var xmlDoc = xmlHTTP.responseXML.documentElement;
			for (var i=0; i < xmlDoc.childNodes.length; i++) {
				if (window.ActiveXObject)
					document.write("<option value=\""+xmlDoc.childNodes[i].childNodes[1].text+"\">"+xmlDoc.childNodes[i].childNodes[0].text+"</option>");
				else
					document.write("<option value=\""+xmlDoc.childNodes[i].childNodes[1].textContent+"\">"+xmlDoc.childNodes[i].childNodes[0].textContent+"</option>");
		}
	}
	} catch (e) {
	}
}
