//okiinra technology v20050829
//form validation script for generic usage

//global setting variables
var ptnContactNumber = /^([0-9]{8})$/;
var ptnNumber = /^([0-9]+)$/;
var ptnCurrency = /^([0-9]+)(.[0-9]{2})?$/;
var ptnDate = /^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/;
var ptnEmailAddress = /^([0-9,a-z,A-Z,_,.]+)\@([0-9,a-z,A-Z,-]+)\.([0-9,a-z,A-Z]+)(.[0-9,a-z,A-Z]+)?$/;
var ptnTime = /^([0-9]{1,2}):([0-9]{2})(:([0-9]{2}))?$/;
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

//used for checking the correct length of a string
//return: only true when string is check
function ValidateLength(string, mn, mx) {
	var len = string.length;
	
	// check for the correct length of the string
	if(len < mn || len > mx) { return false; }
	
	//the string is of the correct length
	return true;
}

//check whether string is an valid one, which contains only characters found in the validchars list
//return: only true when the string is valid
function ValidateString(string, validchars) {
	//check for null and zero string
	if(string == null || string == '') { return false; }
	
	//check every character of the string for invalid characters
	for(i=0;i<string.length;i++) {
           if(validchars.indexOf(string.substr(i, 1)) == -1) { return false; }
	}
	
	//all characters of the string are valid
	return true;
}

//check for valid date and date format (DD/MM/YYYY)
//return: only true when it is a valid date
function ValidateDate2(date) {
	var matchArray = date.match(ptnDate);
	
	//check whether the date is of the correct format
	if(matchArray == null) { return false; }
	month = parseInt(matchArray[1]);
	day = parseInt(matchArray[2]);
	year = parseInt(matchArray[3]);
		
	//check for valid month
	if(month < 1 || month > 12) { return false; }	
	//check for valid day
	daysInMonth = DaysArray(12);
	if(day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) { return false; }
	
	//return true if date is correct and of correct format
	return true;
}

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;
}

//used to calculate the number of days in february of the given year
//return: number of days in february of the given year
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 );
}

//used to tabulate the n array of days for the given number of month
//return: the n array of days
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
}

//check for valid date and date format (DD/MM/YYYY)
//return: only true when it is a valid date
function ValidateDate(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){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	return true
}

//check for valid time and time format (HH:MM)
//arguments: type - 'world' for 24hr clock, else standard 12hr clock
//return: only true when it is a valid time
function ValidateTime(time, type) {
	var matchArray = time.match(ptnTime);
	
	//check whether the time is of the correct format
	if(matchArray == null) { return false; }
	hour = parseInt(matchArray[1]);
	mintues = parseInt(matchArray[2]);
	optional = matchArray[3];
	second = 0;
	//second = (optional.length != null) ? parseInt(optional) : 0;
	
	//check for valid hour
	if(hour < 1 || hour > ((type == 'world') ? 23 : 12)) { return false; }
	//check for valid mintue
	if(mintues < 0 || mintues > 59) { return false; }
	//check for valid second (optional)
	if(second < 0 || second > 59) { return false; }
	
	//return true if time is correct and of correct format
	return true;
}

//check for valid dollar format
//return: only true when it is a valid dollar format
function ValidateCurrency(currency) {
	if(currency.match(ptnCurrency) == null) { return false; }
	else { return true; }
}

//check for valid contact number
//return: only true when it is a valid contact number
function ValidateContactNumber(number) {
	if(number.match(ptnContactNumber) == null) { return false; }
	else { return true; }
}

//check for valid number
//return: only true when it is a valid number
function ValidateNumber(number) {
	if(number.match(ptnNumber) == null) { return false; }
	else { return true; }
}

//check for valid email address
//return: only true when it is a valid email address
function ValidateEmailAddress(email) {
	if(email.match(ptnEmailAddress) == null) { return false; }
	else { return true; }
}