﻿/* Location functions */

function strloc_submit()
{
    locCombo = document.getElementById("LOC");
	loc = locCombo.options[locCombo.selectedIndex].value
	if(loc  == "NONE")
	{
	    alert("To continue, please select a location.");
	}else{
	    document.forms['LOCFRM'].elements['STORE'].value = loc;
	    document.forms['LOCFRM'].submit();
	}
}

function strlocp_submit()
{
    document.forms['STRFRM'].submit();
}

function new_location()
{
    var answer = confirm("Your current order will be deleted. Are you sure you want to start over?")
	if (answer)
		document.location = "StoreNavigator.aspx?E=CLEAR";
}


/* Menu Functions */

function menu_addItem(dept,itm,qty,cnt)
{
	if(cnt != 0)
	{
		// change to redirect to a new page
		var url = "OrderNavigator.aspx?E=ADDITEM";
		url = url + "&DEPT=" + dept;
		url = url + "&ITEM=" + itm;
		url = url + "&QTY=" + qty;
		parent.document.location = url;
	}else{
		document.MENUITEMS.target = "myorder";
		document.MENUITEMS.DEPT.value = dept;
		document.MENUITEMS.ITEM.value = itm;
		document.MENUITEMS.QTY.value = qty;
		document.MENUITEMS.submit();
	}
}




/* Order Function */ 

function order_removeItem(itm)
{
    document.IORDER.E.value = "DELETEITEM";
    document.IORDER.ITEM.value = itm;
    document.IORDER.submit();
}

function order_removeAddOn(itm,addOn)
{
    document.IORDER.E.value = "DELETEADDON";
    document.IORDER.ITEM.value = itm;
    document.IORDER.ADDON.value = addOn;
    document.IORDER.submit();
}

function order_getInfo()
{
	if(window.frames['myorder'].document.MYORDER.COUNT.value != "0"){
		document.COORDER.E.value="GETINFO";
		document.COORDER.submit();
	}else{
		alert("You have NOT ordered any items, please add items before checking out.");
	}
}

function order_modifyItem(itm)
{
   var url = "OrderNavigator.aspx?E=MODIFY";
   url = url + "&ITEM=" + itm;
   parent.parent.document.location = url;
}

/* Check Out Functions */


function checkForHTML(field)
{
	var text = field.value;
	var ret = false;
	if(text.lastIndexOf("<") > 0 || text.lastIndexOf(">") > 0)
	{
		ret = true;
	}
	return ret;
}

function co_validatePageForm()
{
	var errors="";
	if (trim(document.CUSTFORM.FIRSTNAME.value,'both')=="")
	{
		document.CUSTFORM.FIRSTNAME.style.backgroundColor = "#FF9999";
		errors+= "- Your First Name is REQUIRED.\n";
	}else{
		if(checkForHTML(document.CUSTFORM.FIRSTNAME))
		{
			document.CUSTFORM.FIRSTNAME.style.backgroundColor = "#FF9999";
			errors+= "- Your First Name contains invalid characters.\n";
		}else{
			document.CUSTFORM.FIRSTNAME.style.backgroundColor = "#FFFFFF";
		}
	}
	document.CUSTFORM.CALLBACKNUM.value = document.CUSTFORM.cn1.value + "-" + document.CUSTFORM.cn2.value + "-" + document.CUSTFORM.cn3.value;
	if (trim(document.CUSTFORM.CALLBACKNUM.value,'both')=="")
	{
		document.CUSTFORM.cn1.style.backgroundColor = "#FF9999";
		document.CUSTFORM.cn2.style.backgroundColor = "#FF9999";
		document.CUSTFORM.cn3.style.backgroundColor = "#FF9999";
		errors+= "- Your Call Back Number is REQUIRED.\n";
	}else{
		if (document.CUSTFORM.CALLBACKNUM.value.length != 12)
		{
			document.CUSTFORM.cn1.style.backgroundColor = "#FF9999";
			document.CUSTFORM.cn2.style.backgroundColor = "#FF9999";
			document.CUSTFORM.cn3.style.backgroundColor = "#FF9999";
			errors+= "- Your Call Back Number is REQUIRED in the ###-###-#### format.\n";
		}else{
			if(document.CUSTFORM.CALLBACKNUM.value.lastIndexOf("-") == -1)
			{
					document.CUSTFORM.cn1.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn2.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn3.style.backgroundColor = "#FF9999";
					errors+= "- Your Call Back Number is REQUIRED in the ###-###-#### format.\n";
			}else{
				var phonesplit = document.CUSTFORM.CALLBACKNUM.value.split("-");
				if(phonesplit.length != 3)
				{
						document.CUSTFORM.cn1.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn2.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn3.style.backgroundColor = "#FF9999";
						errors+= "- Your Call Back Number is REQUIRED in the ###-###-#### format.\n";
				}else{
					if(phonesplit[0].length != 3 || phonesplit[1].length != 3 || phonesplit[2].length != 4)
					{
						document.CUSTFORM.cn1.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn2.style.backgroundColor = "#FF9999";
						document.CUSTFORM.cn3.style.backgroundColor = "#FF9999";
						errors+= "- Your Call Back Number is REQUIRED in the ###-###-#### format.\n";
					}else{			
						document.CUSTFORM.cn1.style.backgroundColor = "#FFFFFF";
						document.CUSTFORM.cn2.style.backgroundColor = "#FFFFFF";
						document.CUSTFORM.cn3.style.backgroundColor = "#FFFFFF";
					}
				}
			}
		}
	}
	if(document.CUSTFORM.EMAIL.value != "")
		if(!IsEmail(document.CUSTFORM.EMAIL.value)){
			document.CUSTFORM.EMAIL.style.backgroundColor = "#FF9999";
			errors+= "- Your email address is not valid. If you do not want to include your email address, please leave the field blank.\n";
		}else{
			document.CUSTFORM.EMAIL.style.backgroundColor = "#FFFFFF";
		}
	if(checkForHTML(document.CUSTFORM.COMMENTS))
	{
		document.CUSTFORM.COMMENTS.style.backgroundColor = "#FF9999";
		errors+= "- Your comments contain invalid characters.\n";
	}else{
		document.CUSTFORM.COMMENTS.style.backgroundColor = "#FFFFFF";
	}
	if (errors=="")
	{
		var timesplit = document.CUSTFORM.PICKUPTIME.value.split(":");
		document.CUSTFORM.PICKUPHOUR.value = timesplit[0];
		document.CUSTFORM.PICKUPMINUTE.value = timesplit[1].substring(0,2);
		minParts = timesplit[1].split(" ");
		document.CUSTFORM.PICKUPAMPM.value = minParts[1];
		document.CUSTFORM.submit();
	}else{
		alert(errors);
	}
}

function trim(arg,func)
{
	var trimvalue = "";
	arglen = arg.length;
	if (arglen < 1) return trimvalue;

	if (func == "left" || func== "both") 
	{
		i = 0;
		pos = -1;
		while (i < arglen) 
		{
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) 
			{
				pos = i;
				break;
			}
			i++;
		}
	}

	if (func == "right" || func== "both") 
	{
		var lastpos = -1;
		i = arglen;
		while (i >= 0) 
		{
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) 
			{
				lastpos = i;
				break;
			}
			i--;
		}
	}
	if (func == "left") {
		trimvalue = arg.substring(pos,arglen-1);
	}
	if (func == "right") {
		trimvalue = arg.substring(0,lastpos+1);
	}
	if (func == "both") {
		trimvalue = arg.substring(pos,lastpos + 1);
	}
	return trimvalue;
}

    function setOptions()
    {
	    if(document.CUSTFORM.PICKUPDATE.selectedIndex == 0){
		    var len = document.CUSTFORM.PICKUPTIME.length;
		    for(i = 0 ; i < len ;i++)
		    {
				 document.CUSTFORM.PICKUPTIME.options[0] = null;
		    }
		    for(h = 0 ; h< tdtime.length;h++)
		    {
			    var optNew = new Option();
			    optNew.value = tdtime[h];
			    optNew.text = tdtime[h];
    		    document.CUSTFORM.PICKUPTIME.options.add(optNew);
		    }
	    }else{
		    var len = document.CUSTFORM.PICKUPTIME.length;
		    for(i = 0 ; i < len;i++)
		    {
			    document.CUSTFORM.PICKUPTIME.options[0] = null;
		    }
		    for(h = 0 ; h< tmtime.length;h++)
		    {
			    var optNew = new Option();
			    optNew.value = tmtime[h];
			    optNew.text = tmtime[h];
    		    document.CUSTFORM.PICKUPTIME.options.add(optNew);
		    }
	    }
    }	
    
    function keytab(obj, whatLength, whereNext)
    {
	    currentLength = obj.value;
	    if (currentLength.length == whatLength)
	        whereNext.focus();
    }


// Clear all toppings. Needed for topping page in edit mode.
function clearItems(){
	count = document.forms['build'].elements.length;
	cur = "";
	last = "";
	for(i = 0; i < document.forms['build'].elements.length; i++){
		if(document.forms['build'].elements[i].name.substr(0,4) == "ITEM"){
			cur = document.forms['build'].elements[i].name;
			// It is the first element (no toppings)
			if(cur != last){
				document.forms['build'].elements[i].checked = true;
			}
			last = cur;	
		}
	}
}




/* Functions Yet To Be used */


function clearOrder()
{
	if(confirm("Are you sure you want to clear your order? Click \"OK\" to clear."))
	{
		document.MENUFORM.E.value="CLEARORDER";
		document.MENUFORM.submit();
	}
}

function newStore()
{
	if(confirm("NOTE: Selecting a new store will delete your order, are you sure you want to proceed?"))
		location = "index.aspx";
}
	


function onBack()
{
	if(confirm("Going back will clear your order, click \"OK\" to clear and continue back."))
	{
		document.MENUFORM.E.value= "GETSTORES";
		document.MENUFORM.action = "StoreNavigator.aspx";
		document.MENUFORM.submit();
	}
}

// Validates email
function IsEmail(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	}
	if (str.indexOf(" ")!=-1){
	  	return false
	}
 	return true					
}

// Grab form values if a user wants to go back and edit their order
function goBackAndEdit(){
	document.COORDERNAV.E.value="EDITTINGORDER";
	document.COORDERNAV.EDITFNAME.value=document.CUSTFORM.FIRSTNAME.value
	document.COORDERNAV.EDITLNAME.value=document.CUSTFORM.LASTNAME.value
	document.COORDERNAV.EDITEMAIL.value=document.CUSTFORM.EMAIL.value
	document.COORDERNAV.EDITP1.value=document.CUSTFORM.cn1.value
	document.COORDERNAV.EDITP2.value=document.CUSTFORM.cn2.value
	document.COORDERNAV.EDITP3.value=document.CUSTFORM.cn3.value
	document.COORDERNAV.EDITCOMMENT.value=document.CUSTFORM.COMMENTS.value
	document.COORDERNAV.EDITPICKUPDATE.value=document.CUSTFORM.PICKUPDATE.value
	document.COORDERNAV.EDITPICKUPTIME.value=document.CUSTFORM.PICKUPTIME.value
	document.COORDERNAV.submit();
}
