/***********************************************
* Delete Confirmation Pop Up Window
***********************************************/
function performdelete(DestURL) { 
var ok = confirm("Are you sure you want to delete this item? There is no going back."); 
if (ok) {location.href = DestURL;} 
return ok; 
}


/***********************************************
* Warning On Using Invite Friend Coupon
***********************************************/
function invfri(DestURL) { 
var ok = confirm("Are you sure you want use this invite a friend coupon? The use of this coupon cannot be cancelled once you continue, even if you change your order. There is no going back."); 
if (ok) {location.href = DestURL;} 
return ok; 
}


/***********************************************
* Shows DIVS
***********************************************/
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}



/***********************************************
* Hide/Show DIVS
***********************************************/
function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
document.getElementById(d).style.display = "block";
}
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}



/***********************************************
* Hide/Show Images
***********************************************/
function hideImage()
{
document.getElementById("im1").style.visibility="hidden";
}
function repImage()
{
document.getElementById("im1").style.visibility="";
}



/***********************************************
* Hide TIP buttons when paying my cash
***********************************************/
function hideTipCash()
{
document.getElementById("radiotipcc").disabled="disabled";
document.getElementById("radiotipcash").checked="checked";
}
function showTipCash()
{
document.getElementById("radiotipcc").disabled="";
}


/***********************************************
* Pop Up Window
***********************************************/
 
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=700,height=700,left=226,top=62');");
}


/***********************************************
* Print Window Command
***********************************************/
function printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}


/***********************************************
* FORM Validation
***********************************************/
function formValidator(theFormData){
	// Make quick references to our fields
	var quantity = document.getElementById('quantity');
	
	if(document.getElementById('size')) {
	   var size = document.getElementById('size');
	   if(!madeSelection(size, "Please choose a size")) {
    	   return false;
       }
	}
	
	// Automatically get all elements in a field grouped by name
	var formGroup = createFormElementsArray(theFormData);
			
	// Check each input in the order that it appears in the form!
	if(!isNumeric(quantity, "Please enter a valid quantity")){
	   return false;
	}
	
	/*
	//.Don't use this, it fails where elements don't exist!
	var delivery1 = document.getElementById('delivery1');
	var delivery2 = document.getElementById('delivery2');
	
	if(!checkRadio(delivery1, "Please select delivery or pick up.")){
	   return false;
	}
	if(!checkRadio(delivery2, "Please select delivery or pick up.")){
	   return false;
	}
	*/
	//.Check to see if we need to validate
    for(var a=0; a<formGroup.length; a++) {
      for(var b=0; b<formGroup[a].length; b++) {
        if(formGroup[a][b].type == "checkbox") {
          if(!validateCheckbox(formGroup[a])) {return false;}
          break;
        }
        else if(formGroup[a][b].type == "radio") {
          if(!validateRadio(formGroup[a])) {return false;}
          break;
        }
      }
    }
	
	return true;
	
	/*
	
			if(isNumeric(quantity, "Please enter a valid quantity")){
				if(madeSelection(size, "Please choose a size")){
					if(checkRadio(delivery1, "Please select delivery or pick up.")){
						if(checkRadio(delivery2, "Please select delivery or pick up.")){
				
					return true;
					
						}
					}
				}
			}
			
	
	return false;
	*/
}


function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function checkRadio(elem, helperMsg){ 
    if(delivery1 == undefined || delivery2 == undefined) {
        return true;
    }
	if((delivery1.checked == false) && (delivery2.checked == false)){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
} 


function madeSelection(elem, helperMsg){
	if(elem.value == "0"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function createFormElementsArray(theForm){
    var nameExists = false;
    var autoNames = new Array();
    var formGroup = new Array();
    
    //.Put element names in an array (no duplicates)
    for(var a=0; a<theForm.elements.length; a++) {
      nameExists = false;
      for(var b=0; b<autoNames.length; b++) {
        if(autoNames[b] == theForm.elements[a].name) {
          nameExists = true;
          break;
        }
      }
      if(nameExists == false) {autoNames.push(theForm.elements[a].name);}
    }
    
    //.Put elements with common names into their own array
    var formGroup = new Array();
    for(var a=0; a<autoNames.length; a++) {
        formGroup.push(theForm.elements[autoNames[a]]);
    }
    
    return formGroup;
}

function validateSize(groupItem)
{
  //.Loop through the entire group
  if(groupItem.value == "0") {
    alert("Please choose a size.");
    return false;
  }
  
  return true;
}

function validateCheckbox(groupItems)
{
  var groupTitle = "None";
  var maxOptions = 0;
  var isRequired = 0;
  var selOptions = 0;
  
  //.Check what has been selected
  for(var c=0; c<groupItems.length; c++) {
    if(groupItems[c].id == "max") {
      maxOptions = groupItems[c].value;
    }
    else if(groupItems[c].id == "required") {
      isRequired = groupItems[c].value;
    }
    else if(groupItems[c].type == "checkbox") {
      groupTitle = groupItems[c].id;
      if(groupItems[c].checked) {
        selOptions++
      }
    }
  }
  groupTitle = groupTitle.replace(/[0-9]/g, '');
  //.Do the validation if maxOptions has been set
  if(maxOptions != 0 && selOptions > maxOptions) {
    alert("Max selection of "+groupTitle+" is: "+maxOptions);
    return false;
  }
  
  if(isRequired == "1" && selOptions == 0) {
    alert(""+groupTitle+" is required!");
    return false;
  }
  
  return true;
}

function validateRadio(groupItems)
{
  var groupTitle = "None";
  var selOptions = 0;
  
  //.Check what has been selected
  for(var c=0; c<groupItems.length; c++) {
    if(groupItems[c].type == "radio") {
      groupTitle = groupItems[c].id;
      if(groupItems[c].checked) {
        selOptions++;
      }
    }
  }
  groupTitle = groupTitle.replace(/[^a-zA-Z]/g, ' ');
  //.Do the validation
  if(selOptions <= 0 && (groupTitle == 'delivery1' || groupTitle == 'delivery2') ) {
    alert("Please select delivery or pick up.");
    return false;
  }
  else if(selOptions <= 0) {
    alert(""+groupTitle+" is required!");
    return false;
  }
  
  return true;
}

/***********************************************
* GROUP Ordere Validation
***********************************************/
function formGroup(){
	// Make quick references to our fields
	var ordertimeh = document.getElementById('ordertimeh');
	var ordertimem = document.getElementById('ordertimem');
	var ordertimeampm = document.getElementById('ordertimeampm');
	var invites = document.getElementById('invites');
	var delivery1 = document.getElementById('delivery1');
	var delivery2 = document.getElementById('delivery2');
	var emails = document.getElementById('emails');	
			
	// Check each input in the order that it appears in the form!
		if(madeSelection(ordertimeh, "Please choose an order by hour")){
			if(madeSelection(ordertimem, "Please choose an order by minutes")){
				if(madeSelection(ordertimeampm, "Please choose an order by time of day")){
					if(isNumeric(invites, "Please enter a valid number of invites")){
						if(textArea(emails, "Please enter the email addresses for your invites.")){
							if(checkRadio(delivery1, "Please select delivery or pick up.")){
								if(checkRadio(delivery2, "Please select delivery or pick up.")){
		
								return true;
		
								}
		
							}
						}
		
					}
				}
		
			}
		}
			
	
	return false;
	
}


function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function madeSelection(elem, helperMsg){
	if(elem.value == "0"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function checkRadio(elem, helperMsg){ 
	if((delivery1.checked == false) && (delivery2.checked == false)){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function textArea(elem, helperMsg){
	if(elem.value == ""){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}
