// LTH.js
var g_req = null;
var g_req_array = new Array(REQ_ARRAY_MAX);
var g_temp = 0;
var g_req_at = 0;
for (g_temp=0; g_temp<REQ_ARRAY_MAX; g_temp++)
{
	g_req_array[g_temp] = null;
}


function fnConfigureDaySelect()
{
	var str_temp = "";
	str_temp += "<select id='select_day' onchange='fnConfigureTimeSelect();'>";
	var n_month = fnSafeParseInt(fnGetElemValue("select_month"),0);
	switch (n_month)
	{
		case 7:
			str_temp += "<option value='1' selected>1";
			str_temp += "<option value='2' >2";
			str_temp += "<option value='3' >3";
			str_temp += "<option value='4' >4";
			str_temp += "<option value='6' >6";
			str_temp += "<option value='7' >7";
			str_temp += "<option value='8' >8";
			str_temp += "<option value='9' >9";
			str_temp += "<option value='10' >10";
			str_temp += "<option value='11' >11";
			str_temp += "<option value='13' >13";
			str_temp += "<option value='14' >14";
			str_temp += "<option value='15' >15";
			str_temp += "<option value='16' >16";
			str_temp += "<option value='17' >17";
			str_temp += "<option value='18' >18";
			str_temp += "<option value='20' >20";
			str_temp += "<option value='21' >21";
			str_temp += "<option value='22' >22";
			str_temp += "<option value='23' >23";
			str_temp += "<option value='24' >24";
			str_temp += "<option value='25' >25";
			str_temp += "<option value='27' >27";
			str_temp += "<option value='28' >28";
			str_temp += "<option value='29' >29";
			str_temp += "<option value='30' >30";
			str_temp += "<option value='31' >31";
			break;
			
		case 8:
			str_temp += "<option value='1' selected>1";
			str_temp += "<option value='3' >3";
			str_temp += "<option value='4' >4";
			str_temp += "<option value='5' >5";
			str_temp += "<option value='6' >6";
			str_temp += "<option value='7' >7";
			str_temp += "<option value='8' >8";
			str_temp += "<option value='10' >10";
			str_temp += "<option value='11' >11";
			str_temp += "<option value='12' >12";
			str_temp += "<option value='13' >13";
			str_temp += "<option value='14' >14";
			str_temp += "<option value='15' >15";
			str_temp += "<option value='17' >17";
			str_temp += "<option value='18' >18";
			str_temp += "<option value='19' >19";
			str_temp += "<option value='20' >20";
			str_temp += "<option value='21' >21";
			str_temp += "<option value='22' >22";
			str_temp += "<option value='24' >24";
			str_temp += "<option value='25' >25";
			str_temp += "<option value='26' >26";
			str_temp += "<option value='27' >27";
			str_temp += "<option value='28' >28";
			str_temp += "<option value='29' >29";
			str_temp += "<option value='31' >31";
			break;

		case 9:
			str_temp += "<option value='1' selected>1";
			str_temp += "<option value='2' >2";
			str_temp += "<option value='3' >3";
			str_temp += "<option value='4' >4";
			str_temp += "<option value='5' >5";
			str_temp += "<option value='7' >7";
			str_temp += "<option value='8' >8";
			str_temp += "<option value='9' >9";
			str_temp += "<option value='10' >10";
			str_temp += "<option value='11' >11";
			str_temp += "<option value='12' >12";
			str_temp += "<option value='14' >14";
			str_temp += "<option value='15' >15";
			str_temp += "<option value='16' >16";
			str_temp += "<option value='17' >17";
			str_temp += "<option value='18' >18";
			str_temp += "<option value='19' >19";
			str_temp += "<option value='21' >21";
			str_temp += "<option value='22' >22";
			str_temp += "<option value='23' >23";
			str_temp += "<option value='24' >24";
			str_temp += "<option value='25' >25";
			str_temp += "<option value='26' >26";
			str_temp += "<option value='28' >28";
			str_temp += "<option value='29' >29";
			str_temp += "<option value='30' >30";
			break;

		/* template	
		case :
			str_temp += "<option value='1' selected>1";
			str_temp += "<option value='2' >2";
			str_temp += "<option value='3' >3";
			str_temp += "<option value='4' >4";
			str_temp += "<option value='5' >5";
			str_temp += "<option value='6' >6";
			str_temp += "<option value='7' >7";
			str_temp += "<option value='8' >8";
			str_temp += "<option value='9' >9";
			str_temp += "<option value='10' >10";
			str_temp += "<option value='11' >11";
			str_temp += "<option value='12' >12";
			str_temp += "<option value='13' >13";
			str_temp += "<option value='14' >14";
			str_temp += "<option value='15' >15";
			str_temp += "<option value='16' >16";
			str_temp += "<option value='17' >17";
			str_temp += "<option value='18' >18";
			str_temp += "<option value='19' >19";
			str_temp += "<option value='20' >20";
			str_temp += "<option value='21' >21";
			str_temp += "<option value='22' >22";
			str_temp += "<option value='23' >23";
			str_temp += "<option value='24' >24";
			str_temp += "<option value='25' >25";
			str_temp += "<option value='26' >26";
			str_temp += "<option value='27' >27";
			str_temp += "<option value='28' >28";
			str_temp += "<option value='29' >29";
			str_temp += "<option value='30' >30";
			str_temp += "<option value='31' >31";
			break;
			*/
	}
	str_temp += "</select>";
	fnSetInnerhtml("span_select_day", str_temp);
}

function fnConfigureTimeSelect()
{
	var str_temp = "";
	str_temp += "<select id='select_time'>";
	var n_month = fnSafeParseInt(fnGetElemValue("select_month"),0)-1;
	var n_year = 2010;
	var n_day = fnSafeParseInt(fnGetElemValue("select_day"),1);
	var js_date = new Date(n_year, n_month, n_day);
	
	if (js_date.getDay()==0)
	{
		str_temp += "<option value='1230' selected>12:30 pm";
	}
	else if (js_date.getDay()!==1)
	{
		str_temp += "<option value='-1' selected>Select a time";
		str_temp += "<option value='1000' selected>10:00 am";
		str_temp += "<option value='1230'>12:30 pm";
		str_temp += "<option value='1500' selected>3:00 pm";
	}
	str_temp += "</select>";
	fnSetInnerhtml("span_select_time", str_temp);
	fnSetSelectedIndex("select_time",0);
}

function fnReserve_SetDefaults()
{
	var i=0;
	fnHide("div_reserve_success");
	fnHide("div_reserve_fail");
	fnHide_PleaseWait();
	fnReserve_HideErr();
	fnSetElemValue("input_name","");
	fnSetElemValue("input_phone","");
	fnSetSelectedIndex("select_num_in_party",0);
	fnSetElemValue("input_email","");
	fnSetChecked("checkbox_join_tea_club");
	//fnSetUnchecked("checkbox_silver_tea_service");
	fnOnCheckSilverTeaService();
	fnSetElemValue("textarea_notes","");
	fnShow("div_reserve");		
	fnEnable("button_req_reservation");

		// Configure date selects
	fnSetSelectedIndex("select_month", 0);
	fnConfigureDaySelect();
	fnConfigureTimeSelect();
	
	/*
	// Configure silver tea service deposit CC info
	fnSetElemValue("input_cc_num","");
	fnSetElemValue("input_cc_ccv","");
	fnSetSelectedIndex("select_cc_type",0);
	fnSetElemValue("input_cc_person_name","");
	fnSetSelectedIndex("select_cc_expiration_date_month",0);
	fnSetSelectedIndex("select_cc_expiration_date_year",0);
	*/
}

function fnValidateCCNum(str_cc_num)
{	
	var cc_num = str_cc_num.replace(/\D/gi,"");
	fnSetElemValue("input_cc_num", cc_num);
	if (cc_num.length!=16)
		return 0;		
	return 1;
}

function fnReserveItemClick(item_index)
{
	var str_input_quantity = "input_quantity_"+item_index;
	var elem = document.getElementById(str_input_quantity);
	if (elem)			
		elem.select();
}
function fnReserve_ShowErr(str_err)
{
	fnSetInnerhtml("div_order_err", str_err);
	fnShow("div_order_err");
}
function fnReserve_HideErr()
{
	fnHide("div_order_err");
	fnSetInnerhtml("div_order_err", "");
}
function fnIsValidEmail(str_email)
{
	// These two lines below are from http://www.quirksmode.org/js/mailcheck.html
	// Permission to use given by this page: http://www.quirksmode.org/about/copyright.html
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(str_email)) 
		return 1;
	return 0;
}

function fnValidateReservation()
{
	var b_ok=1;
	var str_err="";
	var str_temp = "";
	
	if (fnGetSelectedValue("select_time")=="-1")
	{
		b_ok=0;
		str_err +="<br>- Please select a seating time.";
		fnChangeClass("span_reserve_label_time","style_form_col1_red");
	}
	else
		fnChangeClass("span_reserve_label_time","style_form_col1");
	
	if ((!fnGetElemValue("input_name")) || ("" == fnGetElemValue("input_name")))
	{
		b_ok=0;
		str_err +="<br>- Name is required.";
		fnChangeClass("span_reserve_label_name","style_form_col1_red");
	}				
	else
		fnChangeClass("span_reserve_label_name","style_form_col1");

	if ((!fnGetElemValue("input_phone")) || ("" == fnGetElemValue("input_phone")))
	{
		b_ok=0;
		str_err +="<br>- Phone Number is required.";
		fnChangeClass("span_reserve_label_phone","style_form_col1_red");
	}				
	else
		fnChangeClass("span_reserve_label_phone","style_form_col1");
		
	if ((!fnGetElemValue("input_email")) || ("" == fnGetElemValue("input_email")))
	{
		b_ok=0;
		str_err +="<br>- E-mail is required.";
		fnChangeClass("span_reserve_label_email","style_form_col1_red");
	}				
	else
	{
		if (fnIsValidEmail(fnGetElemValue("input_email")))
		{
			fnChangeClass("span_reserve_label_email","style_form_col1");
		}
		else
		{
			b_ok=0;
			str_err +="<br>- E-mail address must be a valid format (e.g. john@yahoo.com). ";
			fnChangeClass("span_reserve_label_email","style_form_col1_red");
		}
	}
	/*
	if (fnGetChecked("checkbox_silver_tea_service"))
	{
		// CC info
		str_temp = fnGetElemValue("input_cc_num");
		if ((!str_temp) || ("" == str_temp))
		{
			b_ok=0;
			str_err +="<br>- Credit Card Number is required.";
			fnChangeClass("span_cc_num","style_form_col1_red");
		}				
		else
		{
			if (fnValidateCCNum(str_temp))		
			{
				fnChangeClass("span_cc_num","style_form_col1");
			}
			else
			{
				b_ok=0;
				str_err +="<br>- Credit Card Number is not valid.  Please recheck to see if you have entered too many or too few digits.";
				fnChangeClass("span_cc_num","style_form_col1_red");
			}
		}
					
		if ((!fnGetElemValue("input_cc_ccv")) || ("" == fnGetElemValue("input_cc_ccv")))
		{
			b_ok=0;
			str_err +="<br>- Credit Card CCV Number is required.";
			fnChangeClass("span_cc_ccv","style_form_col1_red");
		}				
		else
			fnChangeClass("span_cc_ccv","style_form_col1");
			
		if ((!fnGetElemValue("select_cc_type")) || (("0" != fnGetElemValue("select_cc_type")) && ("1" != fnGetElemValue("select_cc_type"))))
		{
			b_ok=0;
			str_err +="<br>- Credit Card Type is required.";
			fnChangeClass("span_cc_type","style_form_col1_red");
		}				
		else
			fnChangeClass("span_cc_type","style_form_col1");
	
		if ((!fnGetElemValue("input_cc_person_name")) || ("" == fnGetElemValue("input_cc_person_name")))
		{
			b_ok=0;
			str_err +="<br>- Name on Card is required.";
			fnChangeClass("span_cc_person_name","style_form_col1_red");
		}				
		else
			fnChangeClass("span_cc_person_name","style_form_col1");
			
		if ((!fnGetElemValue("select_cc_expiration_date_month")) || ("0" == fnGetElemValue("select_cc_expiration_date_month"))
			|| (!fnGetElemValue("select_cc_expiration_date_year")) || ("0" == fnGetElemValue("select_cc_expiration_date_year")))
		{
			b_ok=0;
			str_err +="<br>- Expiration Date is required.";
			fnChangeClass("span_cc_exp_date","style_form_col1_red");
		}				
		else
			fnChangeClass("span_cc_exp_date","style_form_col1");
	}
	*/
	// Validate Date 
	var str_selected_date = fnSelects2SQLDate();
	var jsdate_selected = fnSQLDate2JSDate(str_selected_date);
	var selected_hour = Math.round(fnSafeParseInt(fnGetElemValue("select_time"),1000)/100);
	jsdate_selected.setHours(selected_hour);
	var jsdate_now = new Date();
	var delta_ms = jsdate_selected.getTime() - jsdate_now.getTime();
	var delta_hrs = ((delta_ms/1000)/60)/60;
	if (delta_hrs<0)
	{
		b_ok=0;
		str_err +="<br>- Online Reservations cannot be made for the past.";
		fnChangeClass("span_reservation_date","style_form_col1_red");
	}				
	else if (delta_hrs<24)
	{
		b_ok=0;
		str_err +="<br>- Online Reservations must be done 24 hours in advance.";
		fnChangeClass("span_reservation_date","style_form_col1_red");
	}				
	else
		fnChangeClass("span_reservation_date","style_form_col1");

	// Generate Error Feedback if appropriate
	fnReserve_HideErr();	
	if (!b_ok)
	{
		str_err = "Please correct the following information to continue:<br>" +str_err;
		fnReserve_ShowErr(str_err);
	}
	
	// Return Success or Error
	return b_ok;
}
function fnBodyOnload()
{
	fnHide_PleaseWait();
	fnReserve_HideErr();
	fnHide("div_reserve_success");
	fnHide("div_reserve_fail");
	fnShow("div_reserve");	
	fnHide("div_disabling_backdrop");
	fnEnable("button_req_reservation");

	var str_param = "" +INDEX_TRACKING__PROJ_ID +"=3046032983269033849";
	str_param += fnParamToURL(INDEX_TRACKING__ACTIVITY_CODE) + "15";
	fnAJAXCall_Post("addTrackingAdminEntry.do", str_param);			
}

function fnShow_PleaseWait()
{
	/*
	fnShow("div_disabling_backdrop");
	fnShow("div_popup_please_wait");
	fnCenterElem("div_popup_please_wait");
	*/
}
function fnHide_PleaseWait()
{
	/*
	fnHide("div_disabling_backdrop");
	fnHide("div_popup_please_wait");
	*/
}

// Send the order to the server
function fnReqReservation()
{
	var j=0;
	var i=0;
	
	// Validate
	if (!fnValidateReservation())
		return;
	
	fnDisable("button_req_reservation");
	// Show the please wait dialog
	//fnShow_PleaseWait();
	
	// Assemble the post param
	var str_param = AJAX_PARAM_NAME +"=" +encodeURIComponent(fnGetElemValue("input_name"));
	str_param += fnParamToURL(AJAX_PARAM_EMAIL) +encodeURIComponent(fnGetElemValue("input_email"));
	if (fnGetChecked("checkbox_join_tea_club"))
		str_param += fnParamToURL(AJAX_PARAM_JOIN_TEA_CLUB) +"1";
	//if (fnGetChecked("checkbox_silver_tea_service"))
	//	str_param += fnParamToURL(AJAX_PARAM_SILVER_TEA_SERVICE) +"1";
	str_param += fnParamToURL(AJAX_PARAM_PHONE) +encodeURIComponent(fnFormatPhone(fnGetElemValue("input_phone")));
	str_param += fnParamToURL(AJAX_PARAM_RESERVATION_DATE) +fnSelects2SQLDate();
	str_param += fnParamToURL(AJAX_PARAM_RESERVATION_TIME) +encodeURIComponent(fnGetElemValue("select_time"));
	str_param += fnParamToURL(AJAX_PARAM_NUM_IN_PARTY) +encodeURIComponent(fnGetElemValue("select_num_in_party"));
	str_param += fnParamToURL(AJAX_PARAM_NOTES) +encodeURIComponent(fnGetElemValue("textarea_notes"));
/*	
	if (fnGetChecked("checkbox_silver_tea_service"))
	{
		str_param += fnParamToURL(AJAX_PARAM_CC_NUM) +encodeURIComponent(fnGetElemValue("input_cc_num"));
		str_param += fnParamToURL(AJAX_PARAM_CC_CCV) +encodeURIComponent(fnGetElemValue("input_cc_ccv"));
		str_param += fnParamToURL(AJAX_PARAM_CC_TYPE) +encodeURIComponent(fnGetElemValue("select_cc_type"));
		str_param += fnParamToURL(AJAX_PARAM_CC_PERSON_NAME) +encodeURIComponent(fnGetElemValue("input_cc_person_name"));
		str_param += fnParamToURL(AJAX_PARAM_CC_EXP) +encodeURIComponent(fnGetElemValue("select_cc_expiration_date_month")+"/"+fnGetElemValue("select_cc_expiration_date_year"));
	}
*/	
	fnAJAXCall_Post("http://www.quasitime.com/tdt/lavenderMakeReservation.do", str_param);
}

function fnOnCheckSilverTeaService()
{
	/*
	var str_out="";
	
	if (fnGetChecked("checkbox_silver_tea_service"))
	{
		str_out += "<div class='style_order_line'></div>";
		str_out += "<div style='text-align:center; margin-top:7px;'><img src='images/silver_text_pink.gif'></div>";
		str_out += "<div style='margin-top:10px;'>This tea service is reserved for those who truly appreciate the \"art of tea\" and the table top. The presentation, which takes two & one half hours, is served on silver tea sets and fine china. Sparkling cider in champagne flutes is served to welcome your party, followed by a pot of tea & coffee. The next course served is a cup of our delicious soup du jour. When the soup is complete a small house salad will be served. After the salad a dainty coup of sorbet to cleanse the palate. Finally a beautiful three-tier presentation which includes our LTH specialties of scones, tea sandwiches, savories, fruit and assortment of petite desserts.</div>";
		str_out += "<div style='margin:10px 0px 15px;'>Silver Tea Service requires a $50 deposit.</div>";
		str_out += "<div class='style_form_row_small'>";
			str_out += "<span class='style_form_col1' id='span_cc_num'>Credit Card #:</span>";
			str_out += "<span class='style_form_col2'>";
				str_out += "<input type=text id='input_cc_num' style='width:250px;'></input>*";
			str_out += "</span>";
		str_out += "</div>";
		str_out += "<div class='style_form_row_small'>";
			str_out += "<span class='style_form_col1' id='span_cc_ccv'>CCV code:</span>";
			str_out += "<span class='style_form_col2'>";
				str_out += "<input type=text id='input_cc_ccv' style='width:50px;'></input>*";
			str_out += "</span>";
		str_out += "</div>";
		str_out += "<div class='style_form_row_small'>";
			str_out += "<span class='style_form_col1' id='span_cc_type'>Credit Card Type:</span>";
			str_out += "<span class='style_form_col2'>";
				str_out += "<select id='select_cc_type'>";
					str_out += "<option value='-1' selected></option>";
					str_out += "<option value='0'>Mastercard</option>";
					str_out += "<option value='1'>Visa</option>";
				str_out += "</select>*";
			str_out += "</span>";
			str_out += "<span style='position:absolute;left:250px;top:2px;'><i>Only Mastercard or Visa</i></span>";
		str_out += "</div>";
		str_out += "<div class='style_form_row_small'>";
			str_out += "<span class='style_form_col1' id='span_cc_person_name'>Name on Card:</span>";
			str_out += "<span class='style_form_col2'>";
				str_out += "<input type=text id='input_cc_person_name' style='width:250px;'></input>*";
			str_out += "</span>";
		str_out += "</div>";
		str_out += "<div class='style_form_row_small'>";
			str_out += "<span class='style_form_col1' id='span_cc_exp_date'>Expiration Date:</span>";
			str_out += "<span class='style_form_col2'>";
				str_out += "<select id='select_cc_expiration_date_month'>";
					str_out += "<option value='0' selected></option>";
					str_out += "<option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option>";
				str_out += "</select>";
				str_out += "&nbsp;/&nbsp;";
				str_out += "<select id='select_cc_expiration_date_year'>";
					str_out += "<option value='0' selected></option>";
					str_out += "<option value='2008'>2008</option><option value='2009'>2009</option><option value='2010'>2010</option><option value='2011'>2011</option><option value='2012'>2012</option><option value='2013'>2013</option><option value='2014'>2014</option><option value='2015'>2015</option><option value='2016'>2016</option><option value='2017'>2017</option><option value='2018'>2018</option><option value='2019'>2019</option><option value='2020'>2020</option><option value='2021'>2021</option><option value='2022'>2022</option><option value='2023'>2023</option><option value='2024'>2024</option><option value='2025'>2025</option>";
				str_out += "</select>*";
			str_out += "</span>";
		str_out += "</div>";
		str_out += "<div class='style_order_line' style='margin-bottom:12px;'></div>";
	}	
	fnSetInnerhtml("div_silver_tea_payment",str_out);
	*/
}

function fnSelects2SQLDate()
{
	var n_month = fnSafeParseInt(fnGetElemValue("select_month"),0);
	var n_day = fnSafeParseInt(fnGetElemValue("select_day"),0);
	var n_year = 2010;
	return ("" +n_year +"-" +n_month +"-" +n_day);
}

function fnConfirmGoBack()
{
	fnReserve_HideErr();
	fnHide("div_confirm_order");
	fnShow("div_order");			
	fnHide("div_order_success");
	fnHide("div_order_fail");
	fnEnable("button_confirm_place_order");
}

function fnStatusHandler(n_code, str_extra)
{
	switch(n_code)
	{
		case STATUS__RESERVE_SUCCESS:
			// Fill in the details
			fnSetInnertext("span_reserve_complete_email",fnGetElemValue("input_email"));
			fnHide("div_reserve");			
			fnShow("div_reserve_success");
			fnHide("div_reserve_fail");
			fnHide_PleaseWait();
			break;
		case STATUS__RESERVE_FAIL:
			fnHide("div_reserve");			
			fnHide("div_reserve_success");
			fnShow("div_reserve_fail");
			fnHide_PleaseWait();
			break;
	}
}

function fnAdjust_Box_Sizes()
{
	var elem = null;
	var client_width = xClientWidth();
	var client_height = xClientHeight();
	var n_temp=0;

	fnCenterElem("div_popup_please_wait");
}

function fnAJAXError(err_code)
{
	alert("We're sorry, an error has occurred.  The order could not be \n sent to the Lavender Tea House server. Our server \n may be temporarily down, or there may be trouble with an \n internet connection. Please wait a few minutes, then try again. Error code="+err_code);
	fnHide_PleaseWait();
}

