function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function Is_Email(email){
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!(!reg1.test(email) && reg2.test(email))) return false;
	else return true;
}

function Is_Blank(obj, chk_val, msg){
	if(trim(obj.value) == chk_val){
		alert(msg);
		obj.focus();
		return true;
	}
	else return false;
}

function Check_Blank(obj, chk_val, msg){
	if(trim(obj.value) == chk_val){
		msg = "\n - " + msg;
	}
	else msg = '';
	return msg;
}

function Check_Country(ctry, frm){
	if(ctry == "US"){
		frm.mem_state_txt.style.display = 'none';
		frm.mem_state.style.display = 'block';
	}
	else{
		frm.mem_state_txt.style.display = 'block';
		frm.mem_state.style.display = 'none';
	}
}

function Validate_Text(obj, msg){
	if(Is_Blank(obj, '', msg)) return false;
	return true;
}

function Clear_Dates(opt){
	if(opt == "lft"){
		document.getElementById('arvl_date').value = '';
		document.getElementById('dept_date').value = '';
	}
	else{
		document.getElementById(opt + '_date').value = '';
		document.getElementById(opt + '_frmdate').value = '';
		document.getElementById(opt + '_todate').value = '';
	}
}

function Check_Extension(obj){
	fname = obj.value;
	if(trim(fname) != "" && fname.indexOf(".") != -1){
		fname_arr = fname.split(".");
		extn = fname_arr[fname_arr.length - 1];
		extn = extn.toLowerCase();
		if(extn != "jpg" && extn != "jpeg" && extn != "gif" && extn != "png"){
			alert("You can only select jpg, gif, png files.");
			obj.focus();
			return false;
		}
		else return true;
	}
	return true;
}

function Swap_Dates(){
	var frm = document.frmsearch;
	frm.arvl_date.value = frm.pop_arvl_date.value;
	frm.dept_date.value = frm.pop_dept_date.value;
}

$(document).ready(function(){ 
    $("#lft_room").change(function(){ 
		var all_rooms_detail = $("#rooms").find(".room");
		var total_rooms = $(this).val();
		var max_rooms = 9;
		var prev_sel_rooms = all_rooms_detail.length;
		if(prev_sel_rooms > total_rooms){
			for(i=total_rooms; i < prev_sel_rooms; i++){
				room = all_rooms_detail[i];
				$(room).remove();
			}
		}
		else{
			var new_rooms = "";
			for(i=(prev_sel_rooms + 1); i <= total_rooms; i++){
				new_rooms = new_rooms + '<div class="room">'
									  + '	<div class="fltlft" style="width:120px;padding-left:15px; margin-top:5px;"><strong>Room ' + i + '</strong></div>'
									  + '	<div class="fltlft" style="width:120px;">'
									  +	'	  <select name="lft_adults[]" id="lft_adults' + (i - 1) + '" class="fltlft">'
									  +	'		 <option>1</option>'
									  +	'		 <option selected="selected">2</option>'
									  +	'		 <option>3</option>'
									  +	'		 <option>4</option>'
									  +	'	  </select>'
									  +	'	</div>'
									  +	'	<div class="fltlft" style="width:120px;">'
									  +	'	  <select name="lft_children[]" id="lft_children' + (i - 1) + '" class="sel-children">'
									  +	'		 <option>0</option>'
									  +	'		 <option>1</option>'
									  +	'		 <option>2</option>'
									  +	'		 <option>3</option>'
									  +	'	  </select>'
									  +	'	</div>'
									  +	'</div>';
			}
			$("#rooms").append(new_rooms);
			
			$('select.sel-children').unbind("change");
			$(".sel-children").change(function(){
		
				var total_children = $(this).val();
				var max_children = 3;
				
				var children_id = $(this).attr("id");
				var curr_rr = children_id.substr(12);
				
				if(total_children == 0){
					$('#div_ages_all' + curr_rr).remove();
					return;
				}
				
				if(total_children < max_children ){
					for(i=total_children; i < max_children; i++){
						curr_age = "childage_" + curr_rr + "_" + i;
						if($("#" + curr_age)) $("#" + curr_age).remove();
					}
				}
				var new_ages = "";
				 
				var age_options = "";
				for(opt=0; opt <= 18; opt++){
					if(opt == 0) opt_val = "&lt;1"; else opt_val = opt;
					age_options = age_options + '<option value="' + opt + '">' + opt_val + '</option>';
				}
				
				var flag_for_label = 1;
				for(i=0; i < total_children; i++){
					new_age_id = "childage_" + curr_rr + "_" + i; //alert(new_age_id);
					if($("#" + new_age_id).attr('id') == undefined){//alert("Not Present " + new_age_id);
						if(i == 0) flag_for_label = 0;
						new_ages = new_ages + '<select name="child_age' + curr_rr + '[]" id="' + new_age_id + '" class="child-age">';
						new_ages = new_ages + age_options;
						new_ages = new_ages + '</select>';
					}
				}
				//alert(new_ages);
				if(flag_for_label == 0){
					div_content = '<div id="div_ages_all' + curr_rr + '">'
								+ '	<div class="fltlft" style="width:103px; margin-top:5px; padding-left:15px;">&nbsp;</div>'
								+ '	<div class="fltlft" style="width:257px;" id="div_ages' + curr_rr + '">'
								+ ' 	Age of Children : ' + new_ages
								+ '	</div>'
								+ '</div>';
					var parent_room = $(this).parent().parent();
					parent_room .append(div_content);
				}
				else{
					$("#div_ages" + curr_rr).append(new_ages);
				}
					
			});
		}
	});
	
	$(".sel-children").change(function(){
		
		var total_children = $(this).val();
		var max_children = 3;
		
		var children_id = $(this).attr("id");
		var curr_rr = children_id.substr(12);
		
		if(total_children == 0){
			$('#div_ages_all' + curr_rr).remove();
			return;
		}
		
		if(total_children < max_children ){
			for(i=total_children; i < max_children; i++){
				curr_age = "childage_" + curr_rr + "_" + i;
				if($("#" + curr_age)) $("#" + curr_age).remove();
			}
		}
		var new_ages = "";
		 
		var age_options = "";
		for(opt=0; opt <= 18; opt++){
			if(opt == 0) opt_val = "&lt;1"; else opt_val = opt;
			age_options = age_options + '<option value="' + opt + '">' + opt_val + '</option>';
		}
		
		var flag_for_label = 1;
		for(i=0; i < total_children; i++){
			new_age_id = "childage_" + curr_rr + "_" + i; //alert(new_age_id);
			if($("#" + new_age_id).attr('id') == undefined){//alert("Not Present " + new_age_id);
				if(i == 0) flag_for_label = 0;
				new_ages = new_ages + '<select name="child_age' + curr_rr + '[]" id="' + new_age_id + '" class="child-age">';
				new_ages = new_ages + age_options;
				new_ages = new_ages + '</select>';
			}
		}
		//alert(new_ages);
		if(flag_for_label == 0){
			div_content = '<div id="div_ages_all' + curr_rr + '">'
						+ '	<div class="fltlft" style="width:103px; margin-top:5px; padding-left:15px;">&nbsp;</div>'
						+ '	<div class="fltlft" style="width:257px;" id="div_ages' + curr_rr + '">'
						+ ' 	Age of Children : ' + new_ages
						+ '	</div>'
						+ '</div>';
			var parent_room = $(this).parent().parent();
			parent_room .append(div_content);
		}
		else{
			$("#div_ages" + curr_rr).append(new_ages);
		}
			
	});
});

$.fn.check_email = function() {
		value = $(this).attr("value");
			// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
			return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
		}

var Validate_Reservation1 = function(){
		var err = '';
		var tot_rooms = $("#hotel_rooms").val();
		for(r=1; r <= tot_rooms; r++){
			fname = $("#rm_fname" + r).val();
			if($.trim(fname).length < 1) err = err + "\n\nRoom " + r + " - First Name.";
			lname = $("#rm_lname" + r).val();
			if($.trim(lname).length < 1) err = err + "\n\nRoom " + r + " - Last Name.";
		}
		
		email_address = $("#rm_email").val();
		if($.trim(email_address).length < 1) err = err + "\n\n - E-mail Address.";
		else if(!$("#rm_email").check_email()) err = err + "\n\n - Invalid E-mail Address.";
		else{
			re_email = $("#re_email").val();
			if($.trim(re_email).length < 1) err = err + "\n\n - Confirm E-mail Address.";
			else if(email_address != re_email) err = err + "\n\n - Confirm E-mail Address again.";
			
		}
		
		for(r=1; r <= 4; r++){
			opt_email = $("#opt_email" + r).val();
			if($.trim(opt_email).length > 0 && !$("#opt_email" + r).check_email())
				 err = err + "\n\n - Invalid Optional E-mail Address " + r + ".";
		}
		
		if(err != ""){
			err = 'One or more of the following fields contain missing or invalid data:\n' + err;
			alert(err);
			return false;
		}
		return true;
	};

$(function(){
	if($("#frmres")){
		$("#frmres").submit(function(){
			var err = '';
			var tot_rooms = $("#hotel_rooms").val();
			for(r=1; r <= tot_rooms; r++){
				fname = $("#rm_fname" + r).val();
				if($.trim(fname).length < 1) err = err + "\n\nRoom " + r + " - First Name.";
				lname = $("#rm_lname" + r).val();
				if($.trim(lname).length < 1) err = err + "\n\nRoom " + r + " - Last Name.";
			}
			
			email_address = $("#rm_email").val();
			if($.trim(email_address).length < 1) err = err + "\n\n - E-mail Address.";
			else if(!$("#rm_email").check_email()) err = err + "\n\n - Invalid E-mail Address.";
			else{
				re_email = $("#re_email").val();
				if($.trim(re_email).length < 1) err = err + "\n\n - Confirm E-mail Address.";
				else if(email_address != re_email) err = err + "\n\n - Confirm E-mail Address again.";
				
			}
			
			for(r=1; r <= 4; r++){
				opt_email = $("#opt_email" + r).val();
				if($.trim(opt_email).length > 0 && !$("#opt_email" + r).check_email())
					 err = err + "\n\n - Invalid Optional E-mail Address " + r + ".";
			}
			
			if(err != ""){
				err = 'One or more of the following fields contain missing or invalid data:\n' + err;
				alert(err);
				return false;
			}
		});
	}
	
	if($("#frmres2")){
		$("#frmres2").submit(function(){
			var err = '';
			
			credit_card = $.trim($("#credit_card").val());
			if(credit_card == "0") err = err + "\n\n - Credit Card Type.";
			
			card_number = $.trim($("#card_number").val());
			if(card_number.length < 1) err = err + "\n\n - Credit Card Number.";
			
			var cur_date = new Date();
			cur_mnth = cur_date.getMonth() + 1;
			cur_year = cur_date.getFullYear();
			
			var exp_month = $("#exp_month").val();
			exp_month = Number(exp_month);
			
			var exp_year = $("#exp_year").val();
			exp_year = Number(exp_year);
			
			if(exp_month < cur_mnth && cur_year == exp_year){
				err = err + "\n\n - Invalid Expiration Date.";
			}
			
			var cid_code = $("#cid_code").val();
			if(credit_card != "P" && cid_code.length < 1) err = err + "\n\n - CID / Card Security Code.";
			
			fname = $("#cr_fname").val();
			if($.trim(fname).length < 1) err = err + "\n\n - First Name.";
			lname = $("#cr_lname").val();
			if($.trim(lname).length < 1) err = err + "\n\n - Last Name.";
			
			cr_address = $.trim($("#cr_address").val());
			if(cr_address.length < 1) err = err + "\n\n - Billing Address.";
			
			cr_city = $.trim($("#cr_city").val());
			if(cr_city.length < 1) err = err + "\n\n - City.";
			
			cr_state = $.trim($("#cr_state").val());
			cr_country = $.trim($("#cr_country").val());
			
			if((cr_country == "US" || cr_country == "AU" || cr_country == "CA") && cr_state.length < 1)
				err = err + "\n\n - State / Province.";
			
			cr_zip = $.trim($("#cr_zip").val());
			if(cr_zip.length < 1) err = err + "\n\n - ZIP / Postal Code.";
			
			if(cr_country.length < 1) err = err + "\n\n - Country.";
			
			cr_phone = $.trim($("#cr_phone").val());
			if(cr_phone.length < 1) err = err + "\n\n - Home Phone.";
			
			cr_work_phone = $.trim($("#cr_work_phone").val());
			if(cr_work_phone.length < 1) err = err + "\n\n - Work Phone.";
			
			if(!$('input[name=terms_condn]').is(':checked')) err = err + "\n\n - Read and agree with Terms and Conditions.";
			
			if(err != ""){
				err = 'One or more of the following fields contain missing or invalid data:\n' + err;
				alert(err);
				return false;
			}
			
			$("#cmdres2").attr("disabled", "true");
		});
	}
});

function Validate_Dates(frm){
	
	var get_date = new Date();
	var cur_date = new Date(get_date.getFullYear(), get_date.getMonth(), get_date.getDate(), 0, 0, 0);
	
	arvl_date = frm.arvl_date.value;
	dept_date = frm.dept_date.value;
	if(arvl_date != "" && dept_date != ""){
		arvl_stamp = new Date(Number(arvl_date.split("/")[2]), (Number(arvl_date.split("/")[0]) - 1), Number(arvl_date.split("/")[1]), 0, 0, 0);
		dept_stamp = new Date(Number(dept_date.split("/")[2]), (Number(dept_date.split("/")[0]) - 1), Number(dept_date.split("/")[1]), 0, 0, 0);
	
		if(arvl_stamp < cur_date){
			alert("Arrival Date should be greater than or equal to Today's Date.");
			return false;
		}
		else if(dept_stamp < cur_date){
			alert("Departure Date should be greater than or equal to Today's Date.");
			return false;
		}
		else if(dept_stamp < arvl_stamp){
			alert("Departure Date should be greater than Arrival Date.");
			return false;
		}
	}
	else if((arvl_date != "" && dept_date == "") || (arvl_date == "" && dept_date != "")){
		alert("Please select both Arrival Date and Departure Date.");
			return false;
	}
}

var Validate_Themes = function(url){
	$("#frm").action = url;
	document.frm.submit(); return false;
};

function Socal(obj){
	$("#" + obj).val('Los Angeles, CA');
	document.frm.srch.value = '1';
	document.frm.submit();
};
