// JavaScript Document  

//######
//## This work is licensed under the Creative Commons Attribution-Share Alike 3.0 
//## United States License. To view a copy of this license, 
//## visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter 
//## to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
//######
(function($){
 $.fn.idleTimeout = function(options) {
    var defaults = {
			inactivity: 1200000, //20 Minutes
			noconfirm: 10000, //10 Seconds
			sessionAlive: 30000, //10 Minutes
			redirect_url: '/js_sandbox/',
			click_reset: true,
			alive_url: '/js_sandbox/',
			logout_url: '/js_sandbox/'
		}
    
    //##############################
    //## Private Variables
    //##############################
    var opts = $.extend(defaults, options);
    var liveTimeout, confTimeout, sessionTimeout;
    var modal = "<div id='modal_pop'><p>You are about to be signed out due to inactivity.</p></div>";
    //##############################
    //## Private Functions
    //##############################
    var start_liveTimeout = function()
    {
      clearTimeout(liveTimeout);
      clearTimeout(confTimeout);
      liveTimeout = setTimeout(logout, opts.inactivity);
      
      if(opts.sessionAlive)
      {
        clearTimeout(sessionTimeout);
        sessionTimeout = setTimeout(keep_session, opts.sessionAlive);
      }
    }
    
    var logout = function()
    {
      
      confTimeout = setTimeout(redirect, opts.noconfirm);
      $(modal).dialog({
        buttons: {"Stay Logged In":  function(){
          $(this).dialog('close');
          stay_logged_in();
        }},
        modal: true,
        title: 'Auto Logout'
      });
      
    }
    
    var redirect = function()
    {
      if(opts.logout_url)
      {
        $.get(opts.logout_url);
      }
      window.location.href = opts.redirect_url;
    }
    
    var stay_logged_in = function(el)
    {
      start_liveTimeout();
      if(opts.alive_url)
      {
        $.get(opts.alive_url);
      }
    }
    
    var keep_session = function()
    {
      $.get(opts.alive_url);
      clearTimeout(sessionTimeout);
      sessionTimeout = setTimeout(keep_session, opts.sessionAlive);
    } 
    
    //###############################
    //Build & Return the instance of the item as a plugin
    // This is basically your construct.
    //###############################
    return this.each(function() {
      obj = $(this);
      start_liveTimeout();
      if(opts.click_reset)
      {
        $(document).bind('click', start_liveTimeout);
      }
      if(opts.sessionAlive)
      {
        keep_session();
      }
    });
    
 };
})(jQuery);


function createCookie(name,value,hours) {
	
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){ 
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

function enforceDateRange() {
	var theForm = document.getElementById("TransactionSearch");
	
	setTransactionSearchDates();
	
	if(theForm.p5.value == -3){
		theForm.p7.disabled = false;
		theForm.p8.disabled = false;
		theForm.p9.disabled = false;
		theForm.p10.disabled = false;
		theForm.p11.disabled = false;
		theForm.p12.disabled = false;
	}else{
		theForm.p7.disabled = true;
		theForm.p8.disabled = true;
		theForm.p9.disabled = true;
		theForm.p10.disabled = true;
		theForm.p11.disabled = true;
		theForm.p12.disabled = true;
		
	}
}

function enforceTypeLogic(){
		var theForm = document.getElementById("TransactionSearch");
		var theSelect = document.getElementById("p3");
		var theSelector = document.getElementById("p2").value;
		var j;
		if(theSelector == 22 || theSelector == 103 || theSelector == 32){
			for(j=theSelect.options.length-1;j>0;j--){
				if(theSelect.options[j].value == 2 || theSelect.options[j].value == 3){
					// nothing
				}
				else{
					theSelect.remove(j);
				}
			}
		}
}
function loopQuotesOnLoad(user) {
	var spans;
	if (document.all) {
		spans = document.all.tags('span');
	}
	else if (document.getElementsByTagName) {
		spans = document.getElementsByTagName('span');
	}
	if (spans) {
		for (var i=0;i<spans.length;i++) {
			
			if(spans[i].id.toString().indexOf('PLACEDBY')==0){
				var agrid=spans[i].id.toString().substring(8, spans[i].id.toString().length);
				enforceQuoteRules(agrid,spans[i].innerHTML,user);
			}// end if
		}// end for
	}// end if
} // end function

function enforceQuoteRules(agrid, chkfld, placedby) { 
	if(placedby!=chkfld){
		var d = document.getElementById("AMEND"+agrid);
		d.parentNode.removeChild( d ); 
	}// end if
}// end function

function loopPickListsOnLoad(user) {
	var spans;
	if (document.all) {
		spans = document.all.tags('span');
	}
	else if (document.getElementsByTagName) {
		spans = document.getElementsByTagName('span');
	}
	if (spans) {
		for (var i=0;i<spans.length;i++) {
			// alert(spans[i].id.toString());
			if(spans[i].id.toString().indexOf(user)!=0){
				enforcePicklistRules(spans[i].id.toString());
			}// end if
		}// end for
	}// end if
} // end function

function enforcePicklistRules(user) { 
		//alert("About to remove: "+user);
		var a = document.getElementById(user+"_PRINT");
		a.parentNode.removeChild( a ); 
		a = document.getElementById(user+"_APPROVE");
		a.parentNode.removeChild( a ); 
}// end function

// Date Functions
function setTransactionSearchDates(){
	var theForm = document.getElementById("TransactionSearch");
	
	// Set p7
	theForm.p7.value = getDateDayDigit();
	// Set p8
	theForm.p8.value = getDateMonthDigit();
	// Set p9
	theForm.p9.value = getDateYearDigit();
	
	// Set p10
	theForm.p10.value = getDateDayDigit();
	// Set p11
	theForm.p11.value = getDateMonthDigit();
	// Set p12
	theForm.p12.value = getDateYearDigit();
	
}

function getDateDayDigit(){
	var curdate = new Date()
	return curdate.getDate()
}

function getDateMonthDigit(){
	var curdate = new Date()
	return curdate.getMonth()+1;
}

function getDateYearDigit(){
	var curdate = new Date()
	return curdate.getYear()
}

function printpr()
{
var OLECMDID = 7;
/* OLECMDID values:
* 6 � print
* 7 � print preview
* 1 � open window
* 4 � Save As
*/

var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);

WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";
}

String.prototype.stripHTML = function()
{
        // What a tag looks like
        var matchTag = /<(?:.|\s)*?>/g;
        // Replace the tag
        return this.replace(matchTag, "");
};

function menuPainter(menuString, subMenuString){
			document.write(menuString);
}	

function menuPainterSubexists(menuString, subMenuString, groupName){
	// Check that the submenu is not in the main menu 
	if((menuString.indexOf(subMenuString) < 0 || menuString.indexOf("</li></ul>") <= 0) && subMenuString.length > 0) {
			var createDiv="<div style=\"padding-top:10px;\"><div class=\"subwrapper\"><div class=\"subhead\">Categories in "+groupName+" </div><ul>";
			subMenuString=subMenuString.replace(/<ul><\/ul>/g, "");
			
			// Now check for nexted lists and remove
			if(subMenuString.indexOf("<ul>") > 0){
				var newMenuString = subMenuString.substring(0, subMenuString.indexOf("<ul>"));
				newMenuString += subMenuString.substring(subMenuString.indexOf("</ul>")+5, subMenuString.length);
				subMenuString=newMenuString;
			}
			
			subMenuString=subMenuString.replace(/h4/g, "li");
			createDiv+=subMenuString+"</ul></div></div>";
			document.write(createDiv);
		}
}	

function searchPlaceHolderTextClear(){
	var theForm = document.getElementById("search_form");
	if(theForm.swap_value.value=="Search")
		theForm.swap_value.value="";
}

function searchForm(){
	var theForm = document.getElementById("search_form");
	var subString = theForm.swap_value.value;
	var searchType="";
	if(subString.length==0){
		alert("Please enter a search term first. ");
		return false;
	}
	searchType = theForm.searchtype.value;
	
	if(searchType == "all")
		subString=subString.replace(/ /g, "+");
	else if(searchType == "any")
		subString=subString.replace(/ /g, "\n\r");
	else{
		alert("An error occured with your search. Search canceled. ");	
		return false;
	}
	theForm.SEARCHVALUE.value=subString;
	return true;
}

function searchFormB2C(){
	var theForm = document.getElementById("search_form");
	var subString = theForm.swap_value.value;
	var searchType="";
	if(subString.length==0){
		alert("Please enter a search term first. ");
		return false;
	}
	subString=subString.replace(/ /g, "\n\r");
	theForm.SEARCHVALUE.value=subString;
	return true;
}

function hideMiniBasket(salesorderacc, pageserver) {
	// Filter Pageserver and specific pages
	if(document.location.toString().indexOf(pageserver) > 0 || pageserver == "<!ABB_INC name=AAT-PAGESERVER;>"
		|| document.location.toString().indexOf("AbbShippingMethodExtras.html") >0 ){
		document.poppedLayer =  eval('document.getElementById("minibasket")');
		if( document.poppedLayer != null)
			document.poppedLayer.style.display = "none";
	}else if(salesorderacc == undefined || salesorderacc == "<!ABB_INC name=SALESORDERCUSTOMERACCOUNTCODE;>" || salesorderacc.length == 0){
		document.poppedLayer =  eval('document.getElementById("minibasket")');
		if( document.poppedLayer != null)
			document.poppedLayer.style.display = "none";
	}

}

function analyseOrderMessage(companyname, OrdCompanyname, SecureServer, host, agrid){
	if(OrdCompanyname==null || OrdCompanyname.length==0)
		document.write("<div align=\"center\"><p>No Order was found that matches the supplied References. <br />Resetting tracking. </p></div><div align=\"center\"><a href='#' class=\"orderdetailstable\" style=\"border:thick; border-color:#999999; padding:5px;\" onclick=\"window.opener.document.location.href='"+SecureServer+"?action=1&p1=TrackOrder.html'\"; window.parent.close(); return false;\"> Continue </a></div>");
	else{
		if(companyname == OrdCompanyname)
			document.write("<div align=\"center\"><p>Your Order has been found. <br />Forwarding on your request. </p></div><div align=\"center\"><a class=\"orderdetailstable\" style=\"border:thick; border-color:#999999; padding:5px;\" href='#' onclick=\"self.moveTo(0,0); self.resizeTo(960,800);window.location.href='"+SecureServer+"?action=110&p1="+agrid+"'\"; '> Continue </a></div>");
		else
			if(!host)
				document.write("<div align=\"center\"><p>No Order for your account was found that matches the supplied References. <br />Resetting tracking. </p></p></div><div align=\"center\"><a class=\"orderdetailstable\" style=\"border:thick; border-color:#999999; padding:5px;\" href='#' onclick=\"window.opener.document.location.href='"+SecureServer+"?action=1&p1=TrackOrder.html'\"; window.parent.close(); return false;\"> Continue </a></div>");  
			else
				document.write("<div align=\"center\"><p>An Order has been found. <br />Forwarding on your request. </p></div><div align=\"center\"><a class=\"orderdetailstable\" style=\"border:thick; border-color:#999999; padding:5px;\" href='#' onclick=\"self.moveTo(0,0); self.resizeTo(960,800);window.location.href='"+SecureServer+"?action=110&p1="+agrid+"'\"; '> Continue </a></div>");
	
	}				

}

function setDeliveryCharges(DeliveryCharges){
	var index = DeliveryCharges.lastIndexOf("value='")+7;
	var lastIndex = DeliveryCharges.indexOf(">Shipping Charge")-1;
	document.write("<input type=hidden name=\"DELIVERYCHARGE\" value="+DeliveryCharges.substring(index, lastIndex)+">");
}

function getVATInclusiveDeliveryCharges(VAT, deliveryEXVAT, currency){
	VAT=VAT.replace(currency, "");
	deliveryEXVAT=deliveryEXVAT.replace(currency, "");
	newVal = Number(VAT)+Number(deliveryEXVAT);
	document.write(currency+newVal);
}

function activatePwd(checked){
	if(checked){
		document.UpdateUser.PASSWORD.disabled=false;
		document.UpdateUser.CONFIRMPASSWORD.disabled=false;
		document.UpdateUser.PASSWORD.style.background="#ececce";
		document.UpdateUser.CONFIRMPASSWORD.style.background = "#ececce";
	}else{
		document.UpdateUser.PASSWORD.disabled=true;
		document.UpdateUser.CONFIRMPASSWORD.disabled=true;
		document.UpdateUser.PASSWORD.style.background = "#CCCCCC";
		document.UpdateUser.CONFIRMPASSWORD.style.background = "#CCCCCC";
		document.UpdateUser.PASSWORD.value="";
		document.UpdateUser.CONFIRMPASSWORD.value=""
		
	}
}

function validateAlertGroup(alertGroups, supervisor){
	var alertGroupString;
	var replaceString;
	var startString;
	var endString;
	var posStart;
	var posEnd;
	<!-- Filter out Create Note -->
	startString="<!--Activate User-->";
	endString="<!--END_Activate User-->";
	posStart = alertGroups.indexOf(startString);
	posEnd = alertGroups.indexOf(endString)+endString.length;
	if(posStart>0 && posEnd>0){
		replaceString = alertGroups.substring(posStart, posEnd);
		alertGroupString = alertGroups.replace(replaceString, "");
		alertGroups = alertGroupString;	
	}
	
	<!-- Filter out Delivery Date Change -->
	startString="<!--Delivery Date Change-->";
	endString="<!--END_Delivery Date Change-->";
	posStart = alertGroups.indexOf(startString);
	posEnd = alertGroups.indexOf(endString)+endString.length;
	if(posStart>0 && posEnd>0){
		replaceString = alertGroups.substring(posStart, posEnd);
		alertGroupString = alertGroups.replace(replaceString, "");
		alertGroups = alertGroupString;	
	}
	
	<!-- Filter out Service Call -->
	startString="<!--Service Call-->";
	endString="<!--END_Service Call-->";
	posStart = alertGroups.indexOf(startString);
	posEnd = alertGroups.indexOf(endString)+endString.length;
	if(posStart>0 && posEnd>0){
		replaceString = alertGroups.substring(posStart, posEnd);
		alertGroupString = alertGroups.replace(replaceString, "");
		alertGroups = alertGroupString;	
	}

	<!-- Apply Security to Purchase Order Approval -->
	if(!supervisor){
		startString="<!--Purchase Order Approval-->";
		endString="<!--END_Purchase Order Approval-->";
		posStart = alertGroups.indexOf(startString);
		posEnd = alertGroups.indexOf(endString)+endString.length;
		if(posStart>0 && posEnd>0){
			replaceString = alertGroups.substring(posStart, posEnd);
			alertGroupString = alertGroups.replace(replaceString, "");
			alertGroups = alertGroupString;	
		}
	}

	document.write(alertGroups);

}

function valueExists(value){
	if(value == undefined || value.length==0)
		document.write("&nbsp;");
	else
		document.write(value);
}

function valueExistsHTML(value, html){
	if(value != undefined && value.length>0)
		document.write(html);
}

function valueExistsBool(value){
	if(value == undefined || value.length==0)
		return false;
	else
		return true;
}

function P11Shuffle (value){
	if(value != undefined || value.length!=0)
		document.write("<input type=\"hidden\" name=\"P11\" value=\""+value+"\" />");
}

function checkDateKPIGoods(){
	var theForm = document.getElementById("form328");
	
	if(theForm.dateGroup[0].checked){
		theForm.P25.disabled=false;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		theForm.P22.disabled=true;
		theForm.P23.disabled=true;
	}else if(theForm.dateGroup[1].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=false;
		theForm.P26.disabled=true;
		theForm.P22.disabled=true;
		theForm.P23.disabled=true;
	}else if(theForm.dateGroup[2].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=false;
		theForm.P22.disabled=true;
		theForm.P23.disabled=true;
	}else if(theForm.dateGroup[3].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		theForm.P22.disabled=false;
		theForm.P23.disabled=false;
	}
}

function checkDateReceiptsSummary(){
	var theForm = document.getElementById("form336");
	
	if(theForm.dateGroup[0].checked){
		theForm.P25.disabled=false;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[1].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=false;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[2].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=false;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[3].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=false;
		document.getElementById("P23").disabled=false;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}
}

function checkDateMovementRep(){
	var theForm = document.getElementById("form914");
	
	if(theForm.dateGroup[0].checked){
		theForm.P25.disabled=false;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
	}else if(theForm.dateGroup[1].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=false;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
	}else if(theForm.dateGroup[2].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=false;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
	}else if(theForm.dateGroup[3].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=false;
		document.getElementById("P23").disabled=false;
	}
}

function checkDateQuoteManRep(){
	var theForm = document.getElementById("form334");
	
	if(theForm.dateGroup[0].checked){
		theForm.P25.disabled=false;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[1].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=false;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[2].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=false;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[3].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=false;
		document.getElementById("P23").disabled=false;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}
}

function checkDateQuoteManDetRep(){
	var theForm = document.getElementById("form335");
	
	if(theForm.dateGroup[0].checked){
		theForm.P25.disabled=false;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[1].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=false;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[2].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=false;
		document.getElementById("P22").disabled=true;
		document.getElementById("P23").disabled=true;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}else if(theForm.dateGroup[3].checked){
		theForm.P25.disabled=true;
		theForm.P24.disabled=true;
		theForm.P26.disabled=true;
		document.getElementById("P22").disabled=false;
		document.getElementById("P23").disabled=false;
		theForm.INITFROM.value="";
		document.getElementById("INITFROM").disabled=true;
		theForm.INITTO.value="";
		document.getElementById("INITTO").disabled=true;		
	}
}

function filterouttabletags(string){
	document.write(string.replace("<table width='100%' border='0'><tr valign='top'><td>", "").replace("</td></tr><tr valign='top'><td>", "").replace("</td></tr></table>", "").replace("\\n", ""));
}
function filterouttabletagsalt(string){
	document.write(string.replace("<table border=\"0\" width=\"100%\"><tbody><tr valign=\"top\"><td>", "").replace("</td></tr><tr valign=\"top\">", "").replace("</td></tr></tbody></table>", "").replace("\\n", ""));
}

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  delete el;
  return s;
}

function SCfilter(s){
	return s.replace(/"/g, "&quot;");
}

function jsTagValueTag(value, compare, string){
	if(value == compare){
		document.write(string);
	}
}
function jsTagNotValueTag(value, compare, string){
	if(value != compare){
		document.write(string);
	}
}

function setProductForm(productid){
	var theForm = document.getElementById("ProductForm");
	theForm.P1.value=productid;
	theForm.submit();
}

function getxPos(elem){
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
}
function getyPos(elem){
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
}

function idleTimeout(webhost, secureserver){
	$(document).ready(function(){
		$(document).idleTimeout({
      			inactivity: 1500000,
	      		noconfirm: 120000,
      			sessionAlive: 300000,
				redirect_url: '/',
				alive_url: webhost,
				click_reset: false,
				logout_url: secureserver+'?action=11'
    	});
	});
}

function countryLabel(){
	var country = document.getElementsByName("CONTACTDETAILS_CODEID");
	document.write(	country[0].options[country[0].selectedIndex].text );
}


/* 
	Utility function to parse system generated selects and 
	populate based on AAT with csv values of the text in the dropdown
*/
function accSelect(sel,csvOpt){
	var els; var i; var j;
	var arry = csvOpt.split(',');
	
	// Quick allow kill filter if no details passed.
	if(arry.length > 0 && arry[0].length > 0){
		els = document.getElementsByTagName("select");
		for(i=0; i<els.length; i++){
			if(els[i].name.startsWith(sel)){
				for(j=els[i].options.length-1;j>0;j--)
					if(accSelectContains(arry,els[i].options[j].text))
						els[i].remove(j);
			}// end i
		}// end if	
	}
}// end accSelect

function accSelectNotContains(arry, val){
	var i = arry.length;
	while (i--) {
    if (arry[i] === val) {
      return false;
    }
  }
  return true;
}

function accSelectContains(arry, val){
	var i = arry.length;
	while (i--) {
    if (arry[i] === val) {
      return true;
    }
  }
  return false;
}

/* 
	Utility function to parse system generated selects and remove particular option
*/
function selectFilter(sel,opt){
	var els; var i; var j;
	els = document.getElementsByTagName("select");
	for(i=0; i<els.length; i++){
		if(els[i].name.startsWith(sel)){
			for(j=els[i].options.length-1;j>0;j--)
				if(els[i].options[j].text == opt)
					els[i].remove(j);
		}// end i
	}// end if	
}// end accSelect

function confirmPostMessage(message){
	var agree=confirm(message);
	if (agree)
		return true ;
	else
		return false ;
}

function stripNonAscii(s){
        document.write(s.replace(/[^0-9\.]/g, ''));
}

function NewLineItems(formId, action){
	var theForm = document.getElementById(formId);
	// Increment MAXLINES
	if(checktransactionitems(theForm.MAXLINES.value)){
		theForm.MAXLINES.value = Number(theForm.MAXLINES.value)+1;
		theForm.submit();
	}
}

function LessLineItems(formId, action){
	var theForm = document.getElementById(formId);
	
	// Decrement MAXLINES
	if(checktransactionitems(theForm.MAXLINES.value)){
		theForm.MAXLINES.value = Number(theForm.MAXLINES.value)-1;
		theForm.submit();
	  }
}

function checktransactionitems(maxlines){
	var descr, account, mgt, amt;
	var result="";
	
	for(var i = 1; i<= maxlines; i++){
		
		descr=document.getElementById("DESCR"+i);
		if(descr.value.length == 0){
			result+="Please ensure that you fill in a description on line "+i+".\n";
			descr.style.backgroundColor="yellow";
		}
		account=document.getElementsByName("ACCTCATID"+i);		
		if(account[0].value.length == 0){
			result+="Please ensure that you select an acccount on line "+i+".\n";
			account[0].style.backgroundColor="yellow";
		}
		mgt=document.getElementsByName("MGTUNIT"+i);
		if(mgt[0].value.length == 0){
			result+="Please ensure that you select a management unit on line "+i+".\n";
			mgt[0].style.backgroundColor="yellow";
		}
		amt=document.getElementById("AMOUNT"+i);
		if(amt.value.length == 0 || amt.value == 0 ){
			result+="Please ensure that you fill in an amount on line "+i+".\n";
			amt.style.backgroundColor="yellow";
		}
	}
	if(result.length > 0){
		alert(result);
		return false;	
	}
	
	return true;
	
}

function totalisejournalamounts(){
	var amt,dr,cr;
	var result="";
	var totval=0;
	var theForm = document.getElementById("formTransEntry");	
	var maxlines = theForm.MAXLINES.value;

	for(var i = 1; i<= maxlines; i++){
		amt=document.getElementById("AMOUNT"+i);
		if(amt.value.length == 0 || amt.value == 0 ){
			result+="Please ensure that you fill in an amount on line "+i+".\n";
			amt.style.backgroundColor="yellow";
		}
		dr=document.getElementById("drcr_dr"+i);
		if(dr.checked){
			totval=totval+(amt.value*1);
		}
		cr=document.getElementById("drcr_cr"+i);
		if(cr.checked){
			totval=totval+(amt.value*-1);
		}
	}
	if(result.length > 0){
		alert(result);
		return false;	
	}
	if(totval!=0){
		alert("Please ensure that the dr and cr elements balance");	
		return false;
	}
	
	var theForm=document.getElementById("formTransEntry");
	theForm.action=window.location;
	theForm.submit();
		
	}

function drcr(radio, amount){
	var theAmt = document.getElementById(amount);
	// Assign Neg
	if(radio = 'CR'){
		theAmt.value = theAmt.value*-1;
	}
}

function createTransactionEntry(){
	var theForm = document.getElementById("formTransEntry");	

	if(theForm.TOTALTEST.value != 0){
		alert("The transaction does not balance. Don't forget to update the Total with all transactions entered below. ");	
		return false;
	}else if(theForm.MAXLINES.value == 1){
		alert("No details have been added. Please add DR and CR line items for a balanced transaction. ");	
		return false;
	}
	theForm.action="?action=300011"; 
	theForm.submit();
}

function amendTransactionEntry(){
	var theForm = document.getElementById("formTransEntry");	
	if(theForm.TOTALTEST.value != 0){
		alert("The transaction does not balance. Don't forget to update the Total with all transactions entered below. ");	
		return false;
	}else if(theForm.MAXLINES.value == 1){
		alert("No details have been added. Please add DR and CR line items for a balanced transaction. ");	
		return false;
	}
	theForm.action="?action=300013"; 
	theForm.submit();
}

function deliveryConstraint(value){
	var orderValue = parseFloat(value.replace("GB&pound;", "").replace(",", ""));
	var theForm = document.getElementById("orForm");
	if(orderValue > 300){
		var x=document.getElementsByName("DeliveryCharge");
		for(i = 0; i < x.length; i++){
			x[i].style.display = "none";
			x[i].disabled = true;
		}
			
		x=document.getElementsByName("freedelivery");
		for(i = 0; i < x.length; i++)
			x[i].style.display = "";
	}
}

function checkPOInvoiceQty(Qty, Control){
	
	var theForm = document.getElementById('PIGrnForm');
	var quantity = document.getElementsByName(Qty);
	if(quantity[0].value.length > 0 ){
		if(quantity[0].value*1 > Control*1){
			alert("Please ensure that you do not enter a value greater than the outstanding items. ");		
			quantity[0].value="";
			quantity[0].style.background="yellow";
		}
	}
}

function confirmPost(){
	var agree=confirm("Are you sure you want to cancel?");
	if (agree)
		return true;
	else
		return false;
}

function setPage(action, page){
	var theForm = document.getElementById("pageform");
	theForm.PAGENUMBER.value=page;
	theForm.submit();	
}
function clearSessionAction(stub, subaction){
	$.get(stub+"?action=200&P1=0", function(){
		window.location=subaction;
	})
}
function clearSessionForm(stub, formId){
	$.get(stub+"?action=200&P1=0", function(){
		$("#"+formId).submit();
	})
}
function requestQuote(ses, stub, action){
	createCookie(ses,'YES', '1');
	clearSessionAction(stub, action);
}
function createOrderForm(ses, stub, formId){
	eraseCookie(ses);
	clearSessionForm(stub, formId);
}


function cloud91501(socac, pageserver,secpageserver,version ){
	<!-- Create the Menu -->
	<!-- Check top level menu option --> 
	if($("#mCustomers > ul > li").length == 0)
		$("#mCustomers").fadeOut();
	if($("#mSuppliers > ul > li").length == 0)
		$("#mSuppliers").fadeOut();
	if($("#mWarehousing > ul > li").length == 0)
		$("#mWarehousing").fadeOut();
	if($("#mReports > ul > li").length == 0)
		$("#mReports").fadeOut();
	if($("#mRepCustomers > ul > li").length == 0)
		$("#mRepCustomers").hide();
	if($("#mRepSuppliers > ul > li").length == 0)
		$("#mRepSuppliers").hide();					
	if($("#mAdministration > ul > li").length == 0)
		$("#mAdministration").fadeOut();
	if($("#mWareHousing > ul > li").length == 0)
		$("#mWareHousing").fadeOut();
	if($("#mAdCustomers > ul > li").length == 0)
		$("#mAdCustomers").hide();
	if($("#mAdSuppliers > ul > li").length == 0)
		$("#mAdSuppliers").hide();

	<!-- Clear Session Actions -->
	$('.clearsess').click(function(){
		var link=$(this).attr("href");
		clearSessionAction(secpageserver,link);
		return false;	
	});
	
	<!-- Clear Session Actions -->
	$('.clearsessform').click(function(){
		var id=$(this).parent().attr("id");
		clearSessionForm(secpageserver,id);
		return false;	
	});
	
	<!-- Clear Session Actions -->
	$('.submitform').click(function(){
		$("#"+$(this).parent().attr("id")).submit();
		return false;	
	});
	<!-- Version Bar --> 
	$('#e3vs-bar').html("v"+version+".$Rev: 1006 $".replace("$Rev: ", "").replace("$","") );
	if(navigator.userAgent.match(/iPad/i) != null ||navigator.userAgent.match(/iPhone/i) != null)
	{$("#e3vs-bar").css("position", "static");};
}

