// JavaScript Document
/* Add method trim() into string object*/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


function jsEmail(prefix,domain,attr){
	document.write('<a '+attr+' href="mailto:' + prefix + '@' + domain + '">');
	document.write(prefix + '@' + domain + '<\/a>');
}
// If the element's string matches the regular expression it is all numbers
function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{

		return false;
	}
}

// If the element's string matches the regular expression it is all letters
function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

// If the element's string matches the regular expression it is numbers and letters
function isAlphanumeric(elem){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

// If the element's string matches the regular expression for zip code 
//it is numbers and letters and space
function isZip(elem){
	var alphaExp = /^[0-9a-zA-Z-+\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

// If the element's string matches the regular expression for telephone number
//it is numbers and letters and space
function isTel(elem){
	var alphaExp = /^[0-9-+\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}
//check email pattern
function isEmail(elem){
	var emailExp = /^[a-zA-Z_]+[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	elem.value=elem.value.trim();
	if(elem.value.match(emailExp)){
		return true;
	}else{

		return false;
	}
}
//check empty field  . if empty return true
function isEmpty(elem) {
	elem.value=elem.value.trim();
   	if ((elem.value.length==0) || (elem.value=="") || (elem.value==null))  
		return true;  //empty field return true
   	else { 

   	  	return false; //not empty return false
   	}
}	

// check length of element value
function chkLength(elem, lmin, lmax){
	var lmin=lmin|| 0;
	var lmax=lmax||10;
	var uInput = elem.value;
	if(uInput.length >= lmin && uInput.length <= lmax){
		return true;
	}else{

		return false;
	}
}
function dateCompare(d1,opt,d2){
	//date format : dd-mm-YYYY
	var rs=false;
		Date1=new Date(); 
		var temp = new Array(); // create array object
		temp = d1.split('-'); 
		Date1.setFullYear(temp[2],(temp[1]-1),temp[0]);//set value to myDate object
		
		Date2=new Date(); 
		var temp = new Array(); // create array object
		temp = d2.split('-'); 
		Date2.setFullYear(temp[2],(temp[1]-1),temp[0]);
		
		if(opt==null) opt='=';
		switch(opt){
			case '=' : 
					if(Date1==Date2) rs=true;
					break;
			case '>' :  
					if(Date1>Date2)	rs=true;
					break;
			case '<' : 
					if(Date1<Date2)	rs=true;
					break;
			default : rs=false;
					break;
		}
	return rs;

}
var roomLabelAct = '';
function disableTwinbed(label_id,elem_id,target_id,boolean_val){
	var label  = document.getElementById(label_id);
	var elem   = document.getElementById(elem_id);
	var target = document.getElementById(target_id);
	
	if(label!=null){
		if(roomLabelAct != ''){
			var oldLabel = document.getElementById(roomLabelAct);
			oldLabel.className = 'roomLabel';
			//alert(oldLabel.getAttribute('class'));
		}
		label.className= 'roomLabelAct';
		roomLabelAct = label.id;
		
	}
	
	if(elem!=null && target!=null){
		if(elem.checked && boolean_val==true){
			target.checked=true;
			elem.disabled=true;
		}else{
			elem.disabled= boolean_val;			
		}
	}
	
}
function chkPaymentFrm(frm){
	//var err_color="#FFB3B3";
	var cntError=0;
	var Msg="----- Please Check -----\n";
	
/* Personal Information */
	if(isEmpty(frm.fname)){
		cntError++;
		Msg+=cntError+". Invalid First Name. \n";
		//chgTDcolor("td_name",err_color);
		
	}
	if(isEmpty(frm.lname)){
		cntError++;
		Msg+=cntError+". Invalid Last Name. \n";
		//chgTDcolor("td_add1",err_color);
		
	}
	if(isEmpty(frm.address)){
		cntError++;
		Msg+=cntError+". Invalid Address. \n";
		//chgTDcolor("td_city",err_color);

	}
	if(isEmpty(frm.city)){
		cntError++;
		Msg+=cntError+". Invalid City. \n";
		//chgTDcolor("td_city",err_color);
	}
	if(!isZip(frm.zip)){
		cntError++;
		Msg+=cntError+". Invalid Postal Code\n";
	}
	if(frm.country.value =="0"){
		cntError++;
		Msg+=cntError+". Invalid Country. \n";
	//	chgTDcolor("td_country",err_color);
	}
	if(!isEmpty(frm.tel) && !isTel(frm.tel)){
		cntError++;
		Msg+=cntError+". Invalid Phone. \n";
	}
	if(!isEmail(frm.email)){
		cntError++;
		Msg+=cntError+". Invalid Email Address. \n";
	}

	
	if(cntError!=0){
		alert(Msg);
		return false;
	}
	else{
		//alert(cntError);
	//	document.getElementById("form1").submit();
		//frm.submit();
		return true;
	}
	
}
