/* Copyright (c) 2000 Affno (Pvt) Ltd, . All rights reserved.

 This software is the confidential and proprietary information of        
 Affno  ("Confidential Information").  You shall not disclose such 
 Confidential Information and shall use it only in accordance with
 the terms of the license agreement you entered into with Affno.

Module Name		: QTMWEB1
File Name		: validation.js
Description		: Acceptable characters for fields v3.0 (Send by Rangika Wed 9/21/2005 10:03 AM)
Created By		: Roshani de Silva
Created Date	: 23-October-2005
Modified By		: 
Modified Date	: 
Version			: 1.00.000

*/

// Empty or Null
function isEmpty(inputStr) {
	if ((inputStr == null) || (inputStr=="")) return true
	else return false
}

// Space
function isSpace(str) {
	pattern = /^[\s]+$/
        if (str.match(pattern)) return true
           else return false;
}

// Numbers only - (No of children, adults, passengers, rooms)
function isNumber(str) {
	pattern= /^[0-9]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Letters with Space
function isLetters(str) {
	pattern = /^[a-zA-Z\s]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Numbers, Letters and Space - (Search, Identity card number, Passport number)
function isAlphaNu(str) {
	pattern = /^[a-zA-Z0-9\s]+$/
		if (!str.match(pattern)) return false
			else return true;
}

/* 
---- all cractors allowed --------	
	Address(postal, resident, mailing, shipping)
 	Zip code, postal code
 	Organization, Company
 	Message, remarks, comments, qualifications
*/	 

// Name - (first, last, full name)
function isName(str) {
	pattern = /^[a-zA-Z\s\-\.\']+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Age
function isAge(str) {
	pattern = /^[a-zA-Z\s\.]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Email
function isEmail(str) {
     pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/
        if (str.match(pattern)) return true
           else return false;
}
// URL
function isUrl(str) {
     pattern = /^[a-zA-Z\s\:\.\/]+$/
        if (str.match(pattern)) return true
           else return false;
}

// Telephone
function isTelephone(str) {
	pattern =/^[0-9a-zA-Z\s\-\/\,\(\)\+\.\:]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Fax
function isFax(str) {
	pattern =/^[0-9a-zA-Z\s\-\/\(\)\+\.]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Mobile Number
function isMobile(str) {
	pattern =/^[0-9\s\-\/\+]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// SMS No
function isSmsNo(str) {
	pattern =/^[0-9\+]+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Country
function isCountry(str) {
	pattern =/^[a-zA-Z\s\-\.\']+$/
		if (!str.match(pattern)) return false
			else return true;
}

// City, State, Province, Region
function isCity(str) {
	pattern = /^[0-9a-zA-Z\s\-\.\']+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Designation, Position, occupation, post of applied for
function isDesignation(str) {
	pattern = /^[0-9a-zA-Z\s\-\/\.\,\"\(\)\']+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Post applied for
function isPostApp(str) {
	pattern = /^[0-9a-zA-Z\s\-\/\.\:\?\!\;\,\"\(\)\']+$/
		if (!str.match(pattern)) return false
			else return true;
}

// Date (birth, arrival, departure, delivery)
function isSortDate(str) {
	pattern = /^[0-9\/\s]+$/
		if (!str.match(pattern)) return false
			else return true;
}

function isUName(str) {
	pattern = /^[a-zA-Z0-9\_]+$/
		if (!str.match(pattern)) return false
			else return true;
}


function isValidPwd(str) {
	if (str.lastIndexOf(" ") == -1) {
				return true;
	} else {
				return false;
	}
}

//Decimal number
function isDecNumber(str, len) {
	//pattern=/^[0-9]+[\.]{0,1}[0-9]{0,2}$/
	pattern=/^[0-9]+[\.]{0,1}[0-9]{0,9}$/
	if (((str.indexOf("."))!=-1) && (str.indexOf(".") > 10)){
			return false
		}
	else if (((str.indexOf("."))==-1) && (str.length > 10)) {
	return false;
	}
	else return(str.match(pattern));
}


//Key word
function isKeyWord(str) {
	pattern = /^[a-zA-Z0-9\s\-\']+$/
		if (!str.match(pattern)) return false
			else return true;
}
/*
==================================================================
isDate(string) : check whether date is in proper format
==================================================================
*/

var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	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;
	}

	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 );
	}
	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
	}

	function isDate(dtStr){
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strDay=dtStr.substring(0,pos1)
		var strMonth=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 (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			alert("Please enter a valid day")
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			alert("Please enter a valid Month")
			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 the Date of Birth \n E.g. dd/mm/yyyy")
			return false
		}
	return true
}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

