jQuery.fn.trimval = function() {
	return jQuery(this).val().replace(/^\s+|\s+$/g,"");
};
jQuery.fn.isPhone = function() {
	var patt1 = new RegExp(/^(ext|x)*[0-9 \(\)\+\-]+$/i);
	return patt1.test(jQuery(this).trimval());
};
jQuery.fn.isEmail = function() {
	function emailCheck (emailStr) {
		/* The following pattern is used to check if the entered e-mail address
		   fits the user@domain format.  It also is used to separate the username
		   from the domain. */
		var emailPat=/^(.+)@(.+)$/
		/* The following string represents the pattern for matching all special
		   characters.  We don't want to allow special characters in the address. 
		   These characters include ( ) < > @ , ; : \ " . [ ]    */
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		/* The following string represents the range of characters allowed in a 
		   username or domainname.  It really states which chars aren't allowed. */
		var validChars="\[^\\s" + specialChars + "\]"
		/* The following pattern applies if the "user" is a quoted string (in
		   which case, there are no rules about which characters are allowed
		   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		   is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")"
		/* The following pattern applies for domains that are IP addresses,
		   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		   e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		/* The following string represents an atom (basically a series of
		   non-special characters.) */
		var atom=validChars + '+'
		/* The following string represents one word in the typical username.
		   For example, in john.doe@somewhere.com, john and doe are words.
		   Basically, a word is either an atom or quoted string. */
		var word="(" + atom + "|" + quotedUser + ")"
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		/* The following pattern describes the structure of a normal symbolic
		   domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		/* Finally, let's start trying to figure out if the supplied address is
		   valid. */
		/* Begin with the coarse pattern to simply break up user@domain into
		   different pieces that are easy to analyze. */
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
		  /* Too many/few @'s or something; basically, this address doesn't
		     even fit the general mould of a valid e-mail address. */
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		// See if "user" is valid 
		if (user.match(userPat)==null) {
		    // user is not valid
		    return false
		}
		/* if the e-mail address is at an IP address (as opposed to a symbolic
		   host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
		    // this is an IP address
			  for (var i=1;i<=4;i++) {
			    if (IPArray[i]>255) {
				return false
			    }
		    }
		    return true
		}
		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
		    return false
		}
		/* domain name seems valid, but now make sure that it ends in a
		   three-letter word (like com, edu, gov) or a two-letter word,
		   representing country (uk, nl), and that there's a hostname preceding 
		   the domain or country. */
		/* Now we need to break up the domain to get a count of how many atoms
		   it consists of. */
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
		    domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
		   return false
		}
		// Make sure there's a host name preceding the domain.
		if (len<2) {
		   var errStr="This address is missing a hostname!"
		   return false
		}
		// If we've gotten this far, everything's valid!
		return true;
	}
	return emailCheck(jQuery(this).trimval());
};
jQuery.fn.isHKid = function() {
	var patt1= new RegExp(/^[a-zA-Z][\d]{4}$/i);
	return patt1.test(jQuery(this).trimval());
};
jQuery.fn.isLess = function(value) {
	return jQuery(this).val().toString.length<=value;
};
jQuery.fn.isChecked = function(value) {
	return jQuery(this).length!=0;
};
jQuery.fn.isSelected = function(value) {
	return jQuery(this).length!=0;
};
jQuery.fn.isSame = function(value) {
	return (jQuery(this).val()==jQuery('#'+value).val());
};
jQuery.fn.isRateCodeOk = function() {
	var patt1= new RegExp(/^[a-zA-Z0-9]{10}$/i);
	return patt1.test(jQuery(this).trimval());
};
jQuery.fn.isUniqueLoginName = function() {
	var result = true;
	jQuery.ajax({
		url: 'checkLoginName.asp',
		dataType: 'json',
		async: false,
		type: 'GET',
		cache: false,
		timeout: 10000,
		data: "name=" + jQuery(this).val(),
		error: function(e, e2, e3){
			alert('error'+ id);
		},
		success: function(json){
			result = json.data;
		}
	})
	return result;
};
jQuery.fn.isValueBetween = function(from_to_string){
	var temp = from_to_string.split(',');
	var from = temp[0]*1;
	var to = temp[1]*1;
	temp = jQuery(this).val();
	return (temp>=from && temp<=to);
};
jQuery.fn.isBetween = function(from_to_string, evalSuffix){
	var temp = from_to_string.split(',');
	var from = temp[0]*1;
	var to = temp[1]*1;
	temp = eval('jQuery(this).val()'+evalSuffix);
	return (temp>=from && temp<=to);
};
jQuery.fn.val2Date = function(){
	return (new Date.fromString(jQuery(this).val()));
}
jQuery.fn.isDate = function(para_str) {
	var result = true;
	var splitPara = para_str.split(',');
	var mode = splitPara[0];
	var compare1 = splitPara[1];
	var dates = {
		current: jQuery(this).val2Date(),
		_1: (new Date(compare1)).addDays(-1),
		_2: (new Date('2099-12-31'))
	}
	if(splitPara.length>=2){
		dates._2 = new Date(splitPara[2]);
	}
	switch(mode){
		case 'before':		result = dates.current<dates._1;	break;
		case 'before_and':	result = dates.current<=dates._1;	break;
		case 'after':		result = dates.current>dates._1;	break;
		case 'after_and':	result = dates.current>=dates._1;	break;
		case 'between':
			break;
	}
	return result;
};

function repeatX(str,supp,length){
	var result="";
	for(var counter=0;counter<=length;counter++){
		result += supp;
	}
	result += str;
	return result.substr(result.length-length,length);
}

function checkSample(){
	var checkListSample = [
		{id:'f-name',checker:'isNull'},
		{id:'f-contact',checker:'isPhone'},
		{id:'f-email',checker:'isEmail'},
		{id:'f-message',checker:'isNull,isLess',isLess:'500'}
	];
	return checker(checkListSample);
}

function checkerSwitch(myChecker, me, $me, myPass){
	switch(myChecker){
		case 'isNull':
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= ($me.trimval()!=\'\')');
			break;
		case 'isSame':
		case 'isLess':
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'("\'+eval(\'me.\'+myChecker+\'\')+\'")\');');
			break;
		case 'isBetween':
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'("\'+eval(\'me.\'+myChecker+\'\')+\'",".length")\');');
			break;
		case 'isValueBetween':
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'("\'+eval(\'me.\'+myChecker+\'\')+\'")\');');
			break;
		case 'isChecked':
			$me = jQuery('#'+me.id+':checked');
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'()\');');
			break;
		case 'isSelected':
			$me = jQuery('#'+me.id+':selected');
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'()\');');
			break;
		case 'isDate':
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'("\'+eval(\'me.\'+myChecker+\'\')+\'")\');');
			break;
		default:
			eval('myPass '+((me.andOr)?me.andOr:'&')+'= eval(\'$me.\'+myChecker+\'()\');');
			break;
	}
	return trueFalse10(myPass);
}
function trueFalse10(thePass){
	return (thePass==1 || thePass==true)?true:false;
}

function checker(checkList){
	var me,myPass,$me,tempId;
	var myChecker = '';
	jQuery('#errMsg').remove();
	for(x in checkList){
		me = checkList[x];
		tempId = me.id.split(',');
		for(x in tempId){
			jQuery('#'+tempId[x]).removeClass('invalid');
		}
	}
	var allPassed = true;
	var msg2user = '';
	try{
		for(x in checkList){
			me = checkList[x];
			myChecker = me.checker.split(',');
			myId = me.id.split(',');
			if(myId=="special"){
				switch(me.checker){
					case "roomsAverage":
						if(jQuery('#no_of_adults_input').val() / jQuery('#no_of_rooms_input').val()>3){
							myPass = false;
						}
						break;
					case "paymentMethod":
						myPass = (jQuery('.btn_bullet[src*="_over"]').length==1);
						break;
				}
			}else if(me.checker.search("&")!=-1){
				myChecker = me.checker.split('&');
				if(myChecker.length != myId.length){
					alert('Wrong No. of Arguments.');
				}
				myPass = !(me.andOr=="|");
				for(y in myChecker){
					if(me.andOr=="|" && y!=0){
						myPass |= false;
					}
					myPass = trueFalse10(myPass);
					$me = jQuery('#'+myId[y]);
					myPass = checkerSwitch(myChecker[y], me, $me, myPass)
				}
			}else{
				for(z in myId){
					$me = jQuery('#'+myId[z]);
					for(y in myChecker){
						if(me.andOr=="|" && y!=0){
							myPass |= false;
						}else{
							myPass = true;
						}
						myPass = trueFalse10(myPass);
						myPass = checkerSwitch(myChecker[y], me, $me, myPass)
					}
				}
			}
			if(! myPass){
				allPassed = false;
				msg2user += me.msg + '\r\n';
				jQuery('#'+tempId[x]).addClass('invalid');
			}
		}
	}catch(e){
		alert(checkList[x].id+'<br />'+e);
		return false;
	}
	if(! allPassed){
		alert(msg2user);
	}
	return allPassed;
}

function getKeyFilter(name){
	var numcheck;
	switch(name){
		case 'email':
		case 'area':
		case 'number':
			numcheck = /([^0-9a-zA-Z@\._])/g;
			break;
		case 'rate_code_code':
			numcheck = /([^0-9A-Z])/g;
			break;
		case 'country':
		case 'area':
		case 'number':
			numcheck = /([^0-9])/g;
			break;
		case 'travel_agent':
		default:
			numcheck = /([^0-9A-Za-z])/g;
			break;
	}
	return numcheck;
}
function keyCheck(me, e, mode){
	var keynum, keychar, numcheck, result;
	if(window.event){	keynum = e.keyCode;
	}else if(e.which){	keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	numcheck = getKeyFilter($(me).attr('name').toString().toLowerCase().replace(/(\[|\])/gi, ''));
	switch(mode){
		case 'press':	return !numcheck.test(keychar); 	break;
		case 'up'	:	($(me).val($(me).val().replace(numcheck,''))); break;
	}
}
