
//====================================================================================================
//	File Name		:	functions.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Javascript Utility functions
//====================================================================================================
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1;
var ns6=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1;
var ns4=document.layers;

//====================================================================================================
//	Function Name	:	popupWindowURL
//	Purpose			:	Whenever you wanna open a link into a new window just call this function
//								you need to pass some arguemnts as described below.
//	Parameters		:
//								url  = url to be open in the new window
//								winname = winname is the window name for the reference of that window
//								w is the width
//								h is the height
//								menu is the parameter, if you want menubar to be enabled on the window
//								resize if you wanna resize the window
//								scroll i fyou needed
//	Return			:	true or false
//----------------------------------------------------------------------------------------------------

function popupWindowURL(url, winname,  w, h, menu, resize, scroll) {

    var x = (screen.width-w)/2;
    var y = (screen.height-h)/3;

	if (winname == null) winname = "newWindow";
	if (w == null) w = 800;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," +"titlebar=no"+ menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);

	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return true;
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

//====================================================================================================
//	Function Name	:	Save_Cart_Order()
//----------------------------------------------------------------------------------------------------
/*function Save_Cart_Order(update_cart_url,cart_referrer)
{//alert(document.referrer)
//	if(document.referrer==cart_referrer)
//	{
		if(document.getElementById("total_rows").value!='')
		{
			var total_rows=document.getElementById("total_rows").value;
			var subtotal=0;
			
			var cartid_arr=document.getElementsByName("cartid[]");
			
			for(i=0;i < total_rows;i++)
			{
				var qty=document.getElementById("qty_"+cartid_arr[i].value).innerHTML;	
				var cart_url = update_cart_url+cartid_arr[i].value+"/"+qty;
				//alert(cart_url)
				
				var update_cart_req;
			
				if (typeof XMLHttpRequest != "undefined") 
				{
					update_cart_req = new XMLHttpRequest();
				} 
				else if (window.ActiveXObject) 
				{
					update_cart_req = new ActiveXObject("Microsoft.XMLHTTP");
				}
									
				update_cart_req.open("POST", cart_url, true);
				 
				update_cart_req.onreadystatechange = function()
				{alert(update_cart_req.responseText);
					
				}
				
				update_cart_req.send(null);
			}
		}
//	}
}*/

//====================================================================================================
//	Function Name	:	Save_Cart_Order()
//----------------------------------------------------------------------------------------------------
function Save_Cart_Order(update_cart_url)
{//alert(document.getElementById("total_rows").value)
	if(document.getElementById("total_rows").value!='')
	{
		var total_rows=document.getElementById("total_rows").value;
		var subtotal=0;
		var qty;
		var cartid_arr=document.getElementsByName("cartid[]");
		var cartid_qty_str='';
		
		for(i=0;i < total_rows;i++)
		{
			qty=document.getElementById("qty_"+cartid_arr[i].value).innerHTML;	
			
			if(i==0)
				cartid_qty_str=cartid_arr[i].value+"_"+qty;
			else
				cartid_qty_str=cartid_qty_str+"-"+cartid_arr[i].value+"_"+qty;
		}
		
		var cart_url = update_cart_url+cartid_qty_str;
			//alert(cart_url)
			
		var update_cart_req;
	
		if (typeof XMLHttpRequest != "undefined") 
		{
			update_cart_req = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) 
		{
			update_cart_req = new ActiveXObject("Microsoft.XMLHTTP");
		}
							
		update_cart_req.open("POST", cart_url, true);
		 
		update_cart_req.onreadystatechange = function()
		{//alert(update_cart_req.responseText);
			
		}
		
		update_cart_req.send(null);
	}
}

//====================================================================================================
//	Function Name	:	Logout_Click()
//----------------------------------------------------------------------------------------------------
var logout_req;
var cartlogout_req;
function Logout_Click(urlpath,delcarturl,logouturl)
{
	/*if(document.getElementById('mycart').innerHTML=="0")
	{
		window.location=logouturl;	
	}
	else
	{*/
	var url = urlpath;
	//alert(url);
	if (typeof XMLHttpRequest != "undefined") 
	{
	 logout_req = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		logout_req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	logout_req.open("POST", url, true);
	logout_req.onreadystatechange = function()
	{
		if (logout_req.readyState == 4) 
		{
			if(logout_req.responseText!='')
			{
				$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-help.png"></div><div id="contentstyle" style="height:60px;">'+logout_req.responseText+'</div>',{ 
			buttons:{Ok:true, Cancel:false},
			callback: function(v,m,f){
				
					if(v){
							var url = delcarturl;
							
							if (typeof XMLHttpRequest != "undefined") 
							{
							 cartlogout_req = new XMLHttpRequest();
							} 
							else if (window.ActiveXObject) 
							{
								cartlogout_req = new ActiveXObject("Microsoft.XMLHTTP");
							}
							
							cartlogout_req.open("POST", url, true);
							
							cartlogout_req.onreadystatechange = function()
							{
								if (cartlogout_req.readyState == 4) 
								{
									window.location=logouturl;
								}
							}
							
							cartlogout_req.send(null);
						}
						else
						{							
							return false;
						}
					}
				});
			}//End responseText
			else
				window.location=logouturl;	
		}//end readyState
	}
	
	logout_req.send(null);
	//}
}

//====================================================================================================
//	Function Name	:	CheckUncheck_Click
//	Purpose			:	Provide the functionality Check OR UnCheck the Check Box
//	Return			:	None
//----------------------------------------------------------------------------------------------------
function CheckUncheck_Click(fld, status)
{
	if(fld)
	{
		if(fld.length)
			for(i=0; i < fld.length; i++)
				fld[i].checked = status;
		else
				fld.checked = status;
	}
}
//====================================================================================================
//	Function Name	:	CheckUncheck_Click1
//	Purpose			:	Provide the functionality Check OR UnCheck the Check Box on the basis of length
//	Return			:	None
//----------------------------------------------------------------------------------------------------
function CheckUncheck_Click1(fld, len, status)
{
	if(fld)
	{
		if(len)
			for(i=0; i < len; i++)
				fld[i].checked = status;
		else
			fld.checked = status;
	}
}

//====================================================================================================
//	Function Name	:	validate
//	Purpose			:	Provide the functionality for shopping cart
//	Return			:	shopping cart data
//----------------------------------------------------------------------------------------------------
var req;

//function validate(frm,carturl,hasmodifierflag,modifierurl,itempref) 
function validate(frm,carturl,hasmodifierflag,modifierurl,itempref,baseprefurl,preference_combo,menuitem_pref_arr,openmodifierflag) 
{
	if(itempref==1)
	{		
		$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+document.getElementById('js_not_exists').value+'</div>');
		return false;
	}
	else
	{
		var product=frm.item_id.value;
		
		//if(hasmodifierflag=='1')
		if(hasmodifierflag=='1' && openmodifierflag=='1')
		{
			popupWindowURL(modifierurl+product, 'list', 600, 550, true, true, true);
			return false;
		}
		else
		{
			 var product=frm.item_id.value;
			
			 var url = carturl+product;
			
			if (typeof XMLHttpRequest != "undefined") 
			{
			 req = new XMLHttpRequest();
			} 
			else if (window.ActiveXObject) 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			req.open("POST", url, true);
			req.onreadystatechange = callback;
			req.send(null);
			
			item_added();
		}
	}
 }
 
//====================================================================================================
//	Function Name	:	validate1 - not in use right now
//	Purpose			:	Provide the functionality for shopping cart
//	Return			:	shopping cart data
//----------------------------------------------------------------------------------------------------
function validate1(frm,carturl,hasmodifierflag,modifierurl,preference_flag) 
{	
	if(preference_flag==1)
	{
			if(confirm('Preference Changed!'))
            {
              	var product=frm.item_id.value;
	
				if(hasmodifierflag=='1')
				{					
					popupWindowURL(modifierurl+product+"/"+preference_flag, 'list', 600, 550, true, true, true);		
				}
				else
				{
					var product=frm.item_id.value;
					
					var url = carturl+product+"/"+preference_flag;
					
					if (typeof XMLHttpRequest != "undefined") 
					{
					 req = new XMLHttpRequest();
					} 
					else if (window.ActiveXObject) 
					{
						req = new ActiveXObject("Microsoft.XMLHTTP");
					}
					req.open("POST", url, true);
					req.onreadystatechange = callback;
					req.send(null);
					
					item_added();
				}
            }
            else
            {
				return false;
            }       
	}
	else
	{
		var product=frm.item_id.value;
	
		if(hasmodifierflag=='1')
		{
			popupWindowURL(modifierurl+product, 'list', 600, 550, true, true, true);		
		}
		else
		{
			 var product=frm.item_id.value;
			
			 var url = carturl+product;
			
			if (typeof XMLHttpRequest != "undefined") 
			{
			 req = new XMLHttpRequest();
			} 
			else if (window.ActiveXObject) 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			req.open("POST", url, true);
			req.onreadystatechange = callback;
			req.send(null);
			
			item_added();
		}
	}		
 }
//====================================================================================================
//	Function Name	:	callback
//	Purpose			:	return the shopping cart content
//	Return			:	return the shopping cart content
//----------------------------------------------------------------------------------------------------
function callback() 
{ 
	if (req.readyState == 4) 
     {
         var mdiv = document.getElementById("mycart"); 		
		 mdiv.innerHTML = req.responseText;
	}
}     

//====================================================================================================
//	Function Name	:	item_added
//	Purpose			:	Item Addede Message
//	Return			:	prompt message
//----------------------------------------------------------------------------------------------------
function item_added()
{
	var message=document.getElementById('js_message').value;
		
	$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-information.png"></div><div id="contentstyle">'+message+'</div>');	
}

//====================================================================================================
//	Function Name	:	change_preference
//	Purpose			:	Changing Preference dropdown
//	Return			:	return the changed preference on true/false
//----------------------------------------------------------------------------------------------------
function change_preference(frm,prefval,cart_count,old_pref_val,$pref_change_flag)
{
	with(frm)
	{
		//if(cart_count>0)
		/*if($pref_change_flag==1)
		{
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-help.png"></div><div id="contentstyle">'+document.getElementById('jm_preference_cnt').value+'</div>',{ 
					buttons:{Ok:true, Cancel:false},
					callback: function(v,m,f){
						
						if(v){
							document.getElementById('set_preference').value=prefval;					
							submit();							
						}	
						else{
							sel_preference.value=old_pref_val;
							return false;
							}
					}
				});			
		}
		else
		{*/
			if(document.getElementById('mycart').innerHTML>0)
			{
				$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-help.png"></div><div id="contentstyle">'+document.getElementById('jm_preference').value+'</div>',{ 
						buttons:{Ok:true, Cancel:false},
						callback: function(v,m,f){
							
							if(v){
								document.getElementById('set_preference').value=prefval;					
								submit();							
							}
							else{
								sel_preference.value=old_pref_val;
								return false;
								}
						}
					});			
			}
			else
			{
				document.getElementById('set_preference').value=prefval;
				submit();
			}
		//}
	}
}

//====================================================================================================
//	Function Name	:	change_settings
//	Purpose			:	Changing Settings
//	Return			:	return true/false
//----------------------------------------------------------------------------------------------------
function change_settings(frm,pref_set_val)
{
	with(frm)
	{
		if(pref_set_val==1)
		{
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-information.png"></div><div id="contentstyle">'+document.getElementById('jm_preference_cnt').value+'</div>',{ 
						buttons:{Ok:true},
						callback: function(v,m,f){
							
							if(v){
								document.getElementById('set_pref_val').value=pref_set_val;
								frm.action=location.href;
								submit();		
							}							
						}
					});				
			
		}
	}
}

//====================================================================================================
//	Function Name	:	btnaction
//	Purpose			:	-
//	Return			:	return true/false
//----------------------------------------------------------------------------------------------------
function btnaction(val)
{
	if (val==true)
		return true;
	else
	{
		return false;
		window.close();
	}
}

//====================================================================================================
//	Function Name	:	Contact_Click
//	Purpose			:	-
//	Return			:	return true/false
//----------------------------------------------------------------------------------------------------
function Contact_Click(frm,actionval1)
{
	with(frm)
	{	
		subaction.value = actionval1;
		var formerror="";
		if(document.frmContactUs.full_name.value=="")
		{
			if(formerror=="")
				formerror="Full Name is required!"+"<br>";
				
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');			
			return false;
		}
		
		if(document.frmContactUs.state.value=="")
		{
			if(formerror=="")
				formerror="City/State is required!"+"<br>";
			else
				formerror=formerror+"City/State is required!"+"<br>";
			
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
			return false;
		}
		
		if(document.frmContactUs.country.value=="")
		{
			if(formerror=="")
				formerror="Country is required!"+"<br>";
			else
				formerror=formerror+"Country is required!"+"<br>";
				
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
			return false;
		}
		
		if(document.frmContactUs.phone.value=="")
		{
			if(formerror=="")
				formerror="Phone is required!"+"<br>";
			else
				formerror=formerror+"Phone is required!"+"<br>";
				
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
			return false;
		}
		else
		{
			valid_phone=isPhoneNumber(document.frmContactUs.phone);
			
			if(valid_phone==false)
				return false;	
		}
		
		if(document.frmContactUs.email.value=="")
		{
			if(formerror=="")
				formerror="Email is required!"+"<br>";
			else
				formerror=formerror+"Email is required!"+"<br>";
				
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
			return false;
		}
		
		if(document.frmContactUs.email.value!="")
		{	
			var regex  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;

			if(!regex.test(document.frmContactUs.email.value))
			{
				if(formerror=="")
					formerror="Invalid Email!"+"\n";
				else
					formerror=formerror+"Invalid Email!"+"\n";
				
				$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
				return false;
			}			
		}
		
		if(document.frmContactUs.comments.value=="")
		{			
			if(formerror=="")
				formerror="Comments is required!"+"<br>";
			else
				formerror=formerror+"Comments is required!"+"<br>";
				
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');
			return false;
		}
				
		submit();		
	}
}

//====================================================================================================
//	Function Name	:	language_submit
//	Purpose			:	-
//	Return			:	return true/false
//----------------------------------------------------------------------------------------------------
function language_submit(frm,langval)
{
	with(frm)
	{
		document.getElementById('set_lang').value=langval;
		submit();
	}
}

//====================================================================================================
//	Function Name	:	isPhoneNumber()
//	Purpose			:	Provide the functionality of checking whether the phone input is alphabet or number 
//	Parameters		:	field	
//	Return			:	true/false
//----------------------------------------------------------------------------------------------------
function isPhoneNumber(field) 
{
	var Phone=trim(field.value);
	var formerror="";

	if(Phone.indexOf("+")>1)
	{
		if(formerror=="")
			formerror="(+) sign must be at the first position!"+"<br>";
		else
			formerror=formerror+"(+) sign must be at the first position!"+"<br>";
		
		$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');	
		
		//alert("(+) sign must be at the first position");
			return false;
	}
	else
	{
		var cnt=0;
		for(i=0;i<Phone.length;i++)
		{
			//alert(field.value.charAt(i));
			if("+" == Phone.charAt(i))
				cnt++;
		}
		
		if(cnt>1)
		{
			if(formerror=="")
				formerror="You cannot add more than one (+) sign in Phone No.!"+"<br>";
			else
				formerror=formerror+"You cannot add more than one (+) sign in Phone No.!"+"<br>";
		
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');	
	
			//alert("You cannot add more than one (+) sign in Phone No.");
			return false;
		}
	}
	
	var re = /^[0-9-+]*$/;
	
	if (!re.test(Phone)) 
	{
		//field.value = field.value.replace(/[^0-9-+]/g, "");
		//alert("Please enter valid Phone No.");
		
		if(formerror=="")
				formerror="Please enter valid Phone No.!"+"<br>";
		else
			formerror=formerror+"Please enter valid Phone No.!"+"<br>";
	
		$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+formerror+'</div>');	
		
		return false;
	}
	
	return true;
} 

//====================================================================================================
//	Function Name	:	TableBook_Click()
//----------------------------------------------------------------------------------------------------
function TableBook_Click(frm,actionval,searchaction)
{
	with(frm)
	{
		if(bookingtime.value == '0' || bookingtime.value == '1' || bookingtime.value == '2' || bookingtime.value == '3' || bookingtime.value == '4')
		{
			$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+document.getElementById('jm_select_table_time').value+'</div>');
			
			return false;
		}	
		
		
		  var todaysdate = new Date();
	  
		  if(todaysdate.getMonth()<"9")
		  {
			  var monthval="0"+parseInt(todaysdate.getMonth()+1);
		  }
		  else
			var monthval=parseInt(todaysdate.getMonth()+1);
		  
		  if(todaysdate.getDate()<"10")
		  {
			  var dateval="0"+parseInt(todaysdate.getDate());
		  }
		  else
		  {
			  var dateval=todaysdate.getDate();
		  }
		  
		  //alert(todaysdate.getFullYear()+"-"+monthval+"-"+todaysdate.getDate());
		  if(bookingdate.value < todaysdate.getFullYear()+"-"+monthval+"-"+dateval)
		  {
			  $.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+document.getElementById('jm_select_table_date').value+'</div>');
			  
			  return false;
		  }
		  
		  Check_Valid_Time();
		  
		  var check_time=Check_Booking_Time();
		  //alert(check_time)
		  if(check_time==false)
		  	return false;
		  
		frm.action=actionval;
		SearchAction.value=searchaction;	
		submit();
	}
}

//====================================================================================================
//	Function Name	:	Check_Valid_Time()
//----------------------------------------------------------------------------------------------------
function Check_Valid_Time()
{
	var req_gmt;
	
	var url = document.getElementById('gmt_url').value;
					
	if (typeof XMLHttpRequest != "undefined") 
	{
		req_gmt = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		req_gmt = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	req_gmt.open("POST", url, true);
	req_gmt.onreadystatechange = function()
	{
	  if (req_gmt.readyState == 4)
	  {
		  document.getElementById('gmt_time').value=req_gmt.responseText;
		}
	}

	req_gmt.send(null);
	
	/* var check_time=Check_Booking_Time();
	 // alert(check_time)
	  if(check_time==false)
		return false;*/
	
	return true;
}
	
//====================================================================================================
//	Function Name	:	Check_Booking_Time()
//----------------------------------------------------------------------------------------------------
function Check_Booking_Time()
{
	var gmt=document.getElementById('gmt_time').value;
	//alert(gmt);
	var tabletime=document.getElementById('bookingtime').value;
	
	var todaysdate = new Date();
	
	 var dateval=todaysdate.getDate();
	 var monthval=todaysdate.getMonth()+1;
	 var yearval=todaysdate.getFullYear();
	 
	 var bookingdate=document.getElementById('bookingdate').value;
	 
	 var booking_date=bookingdate.split("-");
	 
	 var booking_year=booking_date[0];
	 var booking_month=booking_date[1];
	 var booking_dateval=booking_date[2];
	 
	//alert(tabletime+"val");
	//alert(gmt+"gmt")
	/*alert(booking_year+" -- book year-- "+yearval+"  curr year");
	alert(booking_month+" -- book mon-- "+monthval+"  curr mon");
	alert(booking_dateval+" -- book date-- "+dateval+"  curr date");
	*/
	
	if(booking_year <= yearval)
	{
		if(booking_month <= monthval)
		{
			if(booking_dateval <= dateval)
			{
				if(tabletime < gmt)
				{
					
					$.prompt('<div id="imgstyle"><img src="'+document.getElementById('alertimagepath').value+'alertimages/dialog-error.png"></div><div id="contentstyle">'+document.getElementById('jm_gmt_time').value+'</div>');
					
					//alert('Please select future time');
					document.getElementById('main_order_table').style.display='none';
					return false;
				}
			}
		}
	}
	
	return true;
}
