/**
 * site_functions_order.js - JavaScript Functions.
 * @package site_functions_order.js
 * @copyright 2005 - 2007 OSD
*/

/**
 * popUp Function
 * Launches pop up with given URL.
 * @param string URL (URL to include in the popup)
 * @param string height (height of the popup)
 * @param string width (width of the popup)
 * @param string scroll (boolean to show / hide the scrollbar)
 * @param string resize (boolean to enable / disable the resizing)
 * @access public
*/
function popUp(URL, height, width, scroll, resize) { 
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '"+id+"', 'toolbar=0,scrollbars="+scroll+ ",location=0,statusbar=0,menubar=0,resizable="+resize+",width="+width+",height="+height+",left = 10,top = 10');");
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function externalLinks() { 
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;

//**** 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


/**
 * onlyNumbers Function
 * Check key event that its a numerical character.
 * @param event e (key event)
 * @access public
*/
function onlyNumbers(e) { 
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	//alert(keyCode);
	if ((keyCode != 8) && (keyCode != 9) && (keyCode != 127) && ((keyCode < 48) || (keyCode > 57)))
		return false;
}

/**
 * onlyCurrency Function
 * Check key event that its a numerical character.
 * @param event e (key event)
 * @access public
*/
function onlyCurrency(e) { 
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	//alert(keyCode);
	if ((keyCode != 8) && (keyCode != 9) && (keyCode != 46) && (keyCode != 127) && ((keyCode < 48) || (keyCode > 57)))
		return false;
}

/**
 * onlyGoodChars Function
 * Check key event that its an allowed character.
 * @param event e (key event)
 * @access public
*/
function onlyGoodChars(e) { 
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	//alert(keyCode);
	if ((keyCode == 34) || (keyCode == 96))
		return false;
}

/**
 * GetObject Function
 * Gets the chosen object.
 * @access public
*/
function GetObject(strWindow, strObjectName, strParent) { 
	var objItem;	
	if (document.all)
		objItem = strWindow.document.all[strObjectName];
	else { 
		objItem = strWindow.document.getElementById(strObjectName);
		if (objItem==null)
			objItem=strWindow.document.getElementsByName(strObjectName)[0];
		if (strWindow.document.getElementsByName(strObjectName).length>1)
			objItem=strWindow.document.getElementsByName(strObjectName);	
	}
	return (objItem);
}

/**
 * HideDiv Function
 * Hides the chosen DIV.
 * @access public
*/
function HideDiv(strWindow, strDivID, strParent) { 
	objDIV = GetObject(strWindow, strDivID, strParent);
	if (typeof(objDIV) == 'object') { 
		objDIV.style.display = "none";
		objDIV.style.visibility = "hidden";	
	}
}

/**
 * ShowDiv Function
 * Shows the chosen DIV.
 * @access public
*/
function ShowDiv(strWindow, strDivID, strParent) { 
	objDIV = GetObject(strWindow, strDivID, strParent);
	if (typeof(objDIV) == 'object')	{ 
		objDIV.style.display = "";
		objDIV.style.visibility = "visible";
	}
}

/**
 * ToggleDiv Function
 * Toggle the chosen DIV between either hidden or visible.
 * @access public
*/
function ToggleDiv(strWindow, strDivID, strParent) { 
	objDIV = GetObject(strWindow, strDivID, strParent);
	if (typeof(objDIV) == 'object') { 
		if (objDIV.style.visibility=='visible') { 
			HideDiv (strWindow,strDivID,strParent);
			switch (strDivID) { 
   				case "form_rvr" :
   				case "list_rvr" :
   				case "form_rvr_comment" :
   					objDIVImg = GetObject(strWindow, strDivID+"_linkimg", strParent);
   					if (typeof(objDIVImg) == 'object') { 
   						objDIVImg.src = "/images/arrow-down.jpg";
   					}
      				break;
   				default :
			}
		}
		else { 
			ShowDiv (strWindow,strDivID,strParent) ;
			switch (strDivID) { 
   				case "form_rvr" :
   				case "list_rvr" :
   				case "form_rvr_comment" :
   					objDIVImg = GetObject(strWindow, strDivID+"_linkimg", strParent);
   					if (typeof(objDIVImg) == 'object') { 
   						objDIVImg.src = "/images/arrow-up.jpg";
   					}
      				break;
   				default :
			}
		}
	}
}

/**
 * updateField Function
 * Updates form field to flag changes have been made.
 * @param string field (form field that changes were made to)
 * @access public
*/
function updateField(field) { 
	obj = GetObject(window, field, '');
	//alert('Test: '+obj.name+'\nClass: '+obj.className+'\nClass: '+obj.type);
	if (typeof(obj) == 'object') { 
		obj.className = "input_order_field_edited";
		obj_update = GetObject(window, field+'_update', '');
		obj_update.value = 'true';
	}
	return true;
}

/**
 * saveCategory Function
 * Prompts user to save the current category and stay in it.
 * @access public
*/
function saveCategory(){ 
	if (!confirm('Save current order details and stay in the current category!\n\n\'OK\' to Save!\n\n')) {
		return false;
	}
	return true;
}

/**
 * saveNext Function
 * Prompts user to save the current category and proceed to next category.
 * @access public
*/
function saveNext(){ 
	if (!confirm('Save and Proceed to the Next Category!\n\n\'OK\' to Save and Proceed!\n\n')) {
		return false;
	}
	obj = GetObject(window, 'go_next', '');
	//alert(obj.value);
	obj.value = "true";
	//document.form.operation.value='clearCart';
	return true;
}

/**
 * completeOrder Function
 * Prompts user to save and complete the order.
 * @access public
*/
function completeOrder(){ 
	if (!confirm('Save and Complete Order!\n\n\'OK\' to Save and Complete!\n\n')) {
		return false;
	}
	return true;
}

/**
 * CheckRVRDetails Function
 * Check form for required input
 * @param string redirect URL (URL to redirect logout to)
 * @access public
*/
function CheckRVRDetails(){ 
	var pass = true;
	var details = 'Invalid information entered.\n\n';

	if (document.form.rvr_prod_num.value == '') { 
		details+= '- Please select a Product.\n\n';
    	if (pass) document.form.rvr_prod_num.focus();
		pass = false;
	}
	if (document.form.rvr_qty_ordered.value == '') { 
		details+= '- Please enter the Quantity Ordered.\n\n';
    	if (pass) document.form.rvr_qty_ordered.focus();
		pass = false;
	}
	if (document.form.rvr_qty_delivery_note.value == '') { 
		details+= '- Please enter the Quantity on Delivery Note.\n\n';
    	if (pass) document.form.rvr_qty_delivery_note.focus();
		pass = false;
	}
	if (document.form.rvr_qty_received.value == '') { 
		details+= '- Please enter the Quantity Received.\n\n';
    	if (pass) document.form.rvr_qty_received.focus();
		pass = false;
	}
	if (document.form.rvr_collection_req.value == '') { 
		details+= '- Please select if Collection is Required.\n\n';
    	if (pass) document.form.rvr_collection_req.focus();
		pass = false;
	}
	if (document.form.rvr_reason_code.value == '') { 
		details+= '- Please select a Reason Code.\n\n';
    	if (pass) document.form.rvr_reason_code.focus();
		pass = false;
	}
	if (pass) { 
		return true;
	}
	else { 
		details+= 'Please correct the above errors before proceeding.    \n\n';
		alert(details);
		return false;
	}
}

/**
 * confirmLogout Function
 * Asked user to save before logging out - in case they have changed quantities unsaved.
 * @param string redirect URL (URL to redirect logout to)
 * @access public
*/
function confirmLogout(logout_url){ 
	var changed_quantities = false;
	obj = GetObject(window, 'num_order_fields', '');
	if (typeof(obj) == 'object') { 
		num_fields = obj.value;
		//alert(num_fields);
		for (field_index = 1; field_index < num_fields; field_index++) { 
			obj_field = GetObject(window, 'order_' + field_index + '_update', '');
			if (obj_field.value == "true") { 
				changed_quantities = true;
			}
		}
	}
	//alert("changed_quantities : "+changed_quantities);
	if (changed_quantities) { 
		logout_msg = 'Note : \n';
		logout_msg+= ' - You have configured quantities that have not been saved!\n';
		logout_msg+= ' - This could be due to the system defaulting to last weeks quantities where available!\n\n';
		logout_msg+= 'Logout Without Saving?\n\n';
		logout_msg+= '\'OK\' to Logout without saving!\n\n';
		if (!confirm(logout_msg)) {
			return false;
		}
		else { 
			//alert(logout_url);
			window.location.href = logout_url;
			return true;
		}
	}
	else { 
		//alert(logout_url);
		window.location.href = logout_url;
		return true;
	}
}

