/*
	File: validate.js
	Author: Joe Grass and Tuyen
	Date Created: 05/31/2005
	Modified By: George Gutin
	Date Modified: 05/31/2005
*/

/* Open window */
function openWindow(url, name, option) {
	var newWindow = window.open(url, name, option);

	if (!newWindow) alert("The pop-up window has been blocked. Please check your pop-up blocker setting."); 
	else newWindow.focus(); 
}

/* Alert message */
function alertMessage(message, obj) {
	alert(message);
	if ( typeof ( obj ) == "object" ) obj.focus();
	return false;
}

/* Confirm message */
function confirmMessage(message) {
	return (confirm(message)) ? true : false;
}

/* Confirm Password */
function confirmPassword(objPassword, objConfirmpassword) {
	if ( typeof ( objPassword ) == "object" && typeof ( objConfirmpassword ) == "object" )
		return (objPassword.value == objConfirmpassword.value) ? true : false;
	else return false;
}

/* Check form control existance: 
if (typeof myDocument.myForm.elements['myFieldName'] != 'undefined') */
function isExist(obj) {
	return (obj && typeof (obj) == "object") ? true : false;
}

/* Form control has value */
function hasValue(obj) {
	var type;
	
	if (obj.type != null) type = obj.type.toUpperCase();
	else type = "RADIO";
	
	if (type == "TEXT" || type == "PASSWORD" || type == "HIDDEN" || type == "TEXTAREA" || type == "FILE") {	
		if (obj.value.length == 0) { return false; } else { return true; }
	} else if (type == "SELECT-ONE") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[i].selected) if (obj.options[i].value.length != 0) return true;
		}
		return false;  
	} else if (type == "SELECT-MULTIPLE") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[i].selected) return true;
		}
		return false;  
	} else if (type == "SINGLE_VALUE_RADIO" || type == "SINGLE_VALUE_CHECKBOX") {
		if (obj.checked) { return true; } else { return false; }
	} else if (type == "RADIO" || type == "CHECKBOX") {
		if (obj.length == null) { 
			if (obj.checked) return true;
		} else {
			for (i=0; i < obj.length; i++) {
				if (obj[i].checked) return true;
			}
		}
		return false;  
	}
}

/* Integer value */
function isInteger(obj) {
	if ( typeof ( obj ) == "object" ) { integer = obj.value; }
	else return false;
	return (/^[1-9][0-9]*$/.test(integer)) ? true : false;
}

/* Numeric value */
function isNumber(obj) {
	if ( typeof ( obj ) == "object" ) { number = obj.value; }
	else return false;
	return (/^([\+\-])?\d+(\.\d*)?$/.test(number)) ? true : false;
}

/* Numeric decimal value */
function isDecimalNumber(obj) {
	if ( typeof ( obj ) == "object" ) { number = obj.value; }
	else return false;
	return (/^([\+\-])?\d+\.\d*$/.test(number)) ? true : false;
}

/* Alphanumeric value */
function isAlphanumeric(obj) {
	if ( typeof ( obj ) == "object" ) { myString = obj.value; }
	else return false;
	return (/^[a-zA-Z0-9]+$/.test(myString)) ? true : false;
}

/* Date format: mm-dd-yyyy OR m-d-yy */
function isDate1(obj, separator) {
	if ( typeof ( obj ) == "object" ) { date = obj.value; }
	else return false;
	
	if (!(/^\d{1,2}[\-\/]\d{1,2}[\-\/]\d{2,4}$/.test(date))) { return false; }

	var a = date.split(separator);
    a[2] = (a[2].length == 2 ? "20" : "") + a[2];
    a[0] = (a[0].length == 1 ? "0" : "") + a[0];
    a[1] = (a[1].length == 1 ? "0" : "") + a[1];
    
    var d = new Date(a[2], Number(a[0])-1, a[1]);
    d = [d.getFullYear().toString(), (d.getMonth()+1).toString(), d.getDate().toString()];
    if (!d[0].length || !d[1].length || !d[2].length) { return false; }
    if (d[1].length == 1) { d[1] = "0" + d[1]; }
    if (d[2].length == 1) { d[2] = "0" + d[2]; }

    return a[2] == d[0] && a[0] == d[1] && a[1] == d[2];
}

/* Date format: yyyy-mm-dd OR yy-m-d*/
function isDate2(obj) {
	if ( typeof ( obj ) == "object" ) { date = obj.value; }
	else return false;
	
	if (!(/^\d{2,4}[\-\/]\d{1,2}[\-\/]\d{1,2}$/.test(date))) { return false; }

	var a = date.split("-");
    a[0] = (a[0].length == 2 ? "20" : "") + a[0];
    a[1] = (a[1].length == 1 ? "0" : "") + a[1];
    a[2] = (a[2].length == 1 ? "0" : "") + a[2];
    
    var d = new Date(a[0], Number(a[1])-1, a[2]);
    d = [d.getFullYear().toString(), (d.getMonth()+1).toString(), d.getDate().toString()];
    if (!d[0].length || !d[1].length || !d[2].length) { return false; }
    if (d[1].length == 1) { d[1] = "0" + d[1]; }
    if (d[2].length == 1) { d[2] = "0" + d[2]; }

    return a[0] == d[0] && a[1] == d[1] && a[2] == d[2];
}

/* Date format: YYYYDDMM */
function isDate3(obj) {
	if ( typeof ( obj ) == "object" ) { date = obj.value; }
	else return false;
	
	if (!(/^\d{8}$/.test(date))) { return false; }
	
	var d = new Date(date.substring(0, 4),
					date.substring(4, 6) - 1,
					date.substring(6, 8),
					0, 0, 0);
	
	if(isNaN(d) == true) return false;
	
	var sd = d.getFullYear().toString();
	var n = d.getMonth() + 1;
	sd += (n < 10 ? "0" : "") + n;
	n = d.getDate();
	sd += (n < 10 ? "0" : "") + n;
	
	if (date != sd) return false;

	return true;
}

/* DateTime format: yyyy-mm-dd hh:mm:ss */
function isDateISO(obj) {
	if ( typeof ( obj ) == "object" ) { DateISO = obj.value; }
	else return false;
	
	if (!(/^\d{4,4}[\-\/]\d{2,2}[\-\/]\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$/.test(DateISO))) { return false; }
    var a = DateISO.split(" ");
    return isDate2(a[0]) && isHour(a[1]);
}

/* Compare dates (format mm-dd-yyyy) */
function compareDate(objDate1, objDate2, separator) {
	if ( typeof ( objDate1 ) == "object" && typeof ( objDate2 ) == "object") {
		inputDate1 = objDate1.value;
		inputDate2 = objDate2.value;
	} else return false;
	
	splitter = separator;
	
	tempDate1 = inputDate1.split(separator);
	tempDate2 = inputDate2.split(separator);
	
	tempDate1[0] *= 1;
	tempDate1[1] *= 1;
	tempDate1[2] *= 1;

	tempDate2[0] *= 1;
	tempDate2[1] *= 1;
	tempDate2[2] *= 1;
	
	yearResult  = 0; // -1, 0, 1
	monthResult = 0; // -1, 0, 1
	dayResult   = 0; // -1, 0, 1
	
	//-1 date2 less than    date1
	// 0 date2 equals       date1
	// 1 date2 greater than date1
	
	if (tempDate1[2] == tempDate2[2]) { // yyyy
		yearResult = 0;
	} else if (tempDate1[2] < tempDate2[2]) {
		yearResult = 1;
	} else {
		yearResult = -1;
	}
	
	if (tempDate1[0] == tempDate2[0]) { // mm
		monthResult = 0;
	} else if(tempDate1[0] < tempDate2[0]) {
		monthResult = 1;
	} else {
		monthResult = -1;
	}

	if (tempDate1[1] == tempDate2[1]) { // dd
		dayResult = 0;
	} else if(tempDate1[1] < tempDate2[1]) {
		dayResult = 1;
	} else {
		dayResult = -1;
	}

	if (yearResult == 0) {
		if(monthResult == 0) {
			if(dayResult == 0) {
				return 0;
			} else {
				return dayResult;
			}
		} else {
			return monthResult;
		}
	} else {
		return yearResult;
	}
}

/* Check for future date (format mm-dd-yyyy) */
function isFutureDate(obj, separator) {
	if ( typeof ( obj ) != "object" ) return false;

	today = new Date(); 
	date_today = new Object;
	if (today.getYear() < 1900) year = 1900+today.getYear(); else year = today.getYear();
	date_today.value = (today.getMonth()+1) + separator +  today.getDate() + separator + year; 
	
	return compareDate(date_today,obj,separator);
}

/* Time format: hh:mm:ss */
function isTime(obj) {
	if ( typeof ( obj ) == "object" ) { time = obj.value; }
	else return false;

	if (!(/^\d{2,2}:\d{2,2}:\d{2,2}$/.test(time))) { return false; }

	var a = time.split(":");
    a[0] = Number(a[0]);
    a[1] = Number(a[1]);
    a[2] = Number(a[2]);

	return a[0] >= 0 && a[0] <= 23 &&
           a[1] >= 0 && a[1] <= 59 &&
           a[2] >= 0 && a[2] <= 59;
}

/* Time format: hh:mm or h:mm */
function isTime1(obj) {
	if ( typeof ( obj ) == "object" ) { time = obj.value; }
	else return false;

	var a = time.split(":");
    a[0] = Number(a[0]);
    a[1] = Number(a[1]);

	return a[0] >= 0 && a[0] <= 12 &&
           a[1] >= 0 && a[1] <= 59;
}

/* Is e-mail */
function isEmail(obj) {
	if ( typeof ( obj ) == "object" ) { email = obj.value; }
	else return false;
	//var filter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return (filter.test(email));
}

/* Is e-mail */
function isEmail2 (obj) {    
    if ( typeof ( obj ) == "object" ) { email = obj.value; }
	else return false;

	var filter=/^[\w-]+@[\w-]+([.][\w-]+) {1,3}$/;    
    myArray = filter.exec(email);    

	return (myArray) ? true : false;
}

/* Is US ZIP code (Giovanni Glass, 06/16/05) */
function isZip(obj) {
	if ( typeof ( obj ) == "object" ) { zip = obj.value; }
	else return false;

	var USZip = /[0-9]/g; // check if numbers

	if (zip.length == 5 && USZip.test(zip)) return true;
	else return false;
}

/* Is Canadian ZIP code (Giovanni Glass, 06/16/05) */
function isCanZip(obj) {
	if ( typeof ( obj ) == "object" ) { zip = obj.value; }
	else return false;

	var CanZip = /[a-z0-9\s]/ig; // check if numbers, letters, and space only
	var CanZip2 = /^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$/;

	if (CanZip.test(zip) && CanZip2.test(zip)) return true;
	else return false;
}

/* Is phone number in format 800-123-4567 */
function isPhoneNumber(obj) {
	if ( typeof ( obj ) == "object" ) { phone = obj.value; }
	else return false;
	
	var error = 0;
	var count = 0;
	var mask =/(^\d+$)|(^\d+\.\d+$)/;
	var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');

	for (var i = 0; i < phone.length; i++) {
		if (phone.charAt(i)=="-") count+=1;
	}

	if (count>2) error=1;
	else if (phone.indexOf("-")!=3 || phone.indexOf("-",4)!=7) error=1;
	else if (!mask.test(stripped)) error=1; 
	else if (!(stripped.length==10)) error=1; 
	
	if (error==1) return false;
	
	return true;
}

/* Is phone number in format: field1: xxx, field2: xxx, field3: xxxx */
function isPhoneNumberSeparated(obj1, obj2, obj3) {
	if ( typeof ( obj1 ) == "object" && typeof ( obj2 ) == "object" && typeof ( obj3 ) == "object") { 
		var phone = new String;
		phone.value = phone.value = obj1.value + "-" + obj2.value + "-" + obj3.value;
		return isPhoneNumber(phone);
	} else return false;
}

/* Count characters in textarea or input form control */
function textareaCounter(obj, maxLength) {
	var length = obj.value.length;
	
	if (length > maxLength) {
		alert('You have reached the maximum number of characters allowed in this text box.\nPlease enter only '+(maxLength)+' characters.');
		obj.value = obj.value.substr(0,maxLength);
		obj.focus();
		return false;
	}
}

/* Limit characters input - NOT for Firefox */
function limitEnter(obj, maxLength) {
	if ( typeof ( obj ) == "object" ) { objValue = obj.value; }
	else return false;

	if (objValue.length>= maxLength) return false;
}

/* Is string empty - ignore whitespace */
function isEmpty(string) {
    return !Boolean(string.replace(/^\s*|\s*$/g, "").length);
}

/* Is string NOT empty - ignore whitespace */
function isNonEmpty(string) {
    return Boolean(string.replace(/^\s*|\s*$/g, "").length);
}

/* Check all radiobuttons/checkboxes in a group */
function checkAll(formName,checkName) {
	var checkCollection = eval("document.forms." + formName + "." + checkName);
	for (i=0; i<checkCollection.length; i++) checkCollection[i].checked = true;
}

/* Uncheck all radiobuttons/checkboxes in a group */
function uncheckAll(formName,checkName) {
	var checkCollection = eval("document.forms." + formName + "." + checkName);
	for (i=0; i<checkCollection.length; i++) checkCollection[i].checked = false;
}

/* Autotabbing */
function autoTab(prevObj, nextObj) {
	if (prevObj.getAttribute && prevObj.value.length == prevObj.getAttribute("maxlength")) nextObj.focus();
}

/* Replace characters  */
function replaceEntity(string, char1, char2) {
	var newString = "";
	
	for (var i = 0; i < string.length; i++) {
		myChar = string.charAt(i);
		if (myChar == char1) { 
			newString = newString + char2; 
		} else newString = newString + myChar;
	}
    return newString;
}

/* Remove any HTML tags */
function removeHTML() {
	var filter=/<\S[^><]*>/g

	for (i=0; i<arguments.length; i++)
	arguments[i].value=arguments[i].value.replace(filter, "")
}

/* Add single qoute */
function sqlEntity(obj) {
	if ( typeof ( obj ) == "object" ) { objValue = obj.value; }
	else return false;
	var newString = "";
	
	for (var i = 0; i < objValue.length; i++) {
		myChar = objValue.charAt(i);
		if (myChar == "'") { 
			newString = newString + myChar + "'";
		} else newString = newString + myChar;
	}
    return newString;
}

/* Double-submit form prevention */
function singleSubmit(formObj){
	if (document.all||document.getElementById) {
		for (i=0;i<formObj.length;i++) {
			var tempObj=formObj.elements[i];
			if(tempObj.type.toLowerCase()   =="submit"||
				tempObj.name.toLowerCase()  =="submit"||
				tempObj.value.toLowerCase() =="submit"||
				tempObj.id.toLowerCase()    =="submit")
			{
				tempObj.disabled=true;
				break;
			}
		}
	}
}

/* Convert special HTML characters to their entities version  */
function htmlEntity(obj) {

	if ( typeof ( obj ) == "object" ) {
		var originalString = obj.value;
		var newString = "";

		arrayChar = new Array();
		arrayChar[0,0] = "<";
		arrayChar[0,1] = "%3C;";
		arrayChar[1,0] = ">";
		arrayChar[1,1] = "%3E;";
		arrayChar[2,0] = '"';
		arrayChar[2,1] = "%22";
		arrayChar[3,0] = "'";
		arrayChar[3,1] = "%27;";
		arrayChar[4,0] = " ";
		arrayChar[4,1] = "%20";
		arrayChar[5,0] = "#";
		arrayChar[5,1] = "%23";

		originalString = originalString.replace(/[\n]/gi,"<br>");
		originalString = originalString.replace(/[&]/gi,"%26");
		originalString = originalString.replace(/[%]/gi,"%25");

		for (var i = 0; i < originalString.length; i++) {
			counter = 0;
			myChar = originalString.charAt(i);
			for (var j = 0; j < 6; j++) {
				if (myChar == arrayChar[j,0]) { 
					newString = newString + arrayChar[j,1]; 
					counter = 1; 
					break; 
				}
			}
			if (counter==0) newString = newString + myChar;
		}
	} else return false;
    return newString;
}
