var arraySize;
var gblWindowHandle = null;

function pageNav(strTarget) {
	var bitsSize = 0;
	var pageSize = 0;
	var paramSize = 0;

	// Perform a Javascript Split if available
	if (navigator.appVersion.substring(0, 3) != '2.0') {
		var windowURL = window.self.document.URL;
	}
	else {
		var windowURL = window.location;
	}

	if (windowURL.indexOf("Price_Active_Sub.asp") != -1 && !ValidatePriceHistoryPage(strTarget)) {
		//do nothing		
	}
	else {
		// Get the Current URL
		var currentPage = new String(windowURL);
		// Set the Parameter Flag
		var bParam = false;

		// Split off the page parameters
		var pageBit = doSplit(currentPage, '?');
		if (navigator.appVersion.substring(0, 3) != '2.0') {
			pageSize = pageBit.length;
		}
		else {
			pageSize = arraySize;
		}

		// Split to Just the local page name
		var urlBits = doSplit(pageBit[0], '/');
		if (navigator.appVersion.substring(0, 3) != '2.0') {
			bitsSize = urlBits.length;
		}
		else {
			bitsSize = arraySize;
		}

		// Build the Basic URL
		var url = doSplit(urlBits[bitsSize - 1], '#');
		var strURL = url[0];

		// Process if extra bits
		if (pageSize != 1) {

			// Have we naved before
			if (pageBit[1].indexOf('PageNav=') == -1) {
				// No
				strURL += '?PageNav=' + strTarget;
				bParam = true;
			}
			else {
				// Yes, are we going to the same place
				if (pageBit[1].indexOf(strTarget) == -1) {
					// No
					var paramBit = doSplit(pageBit[1], '&');
					if (navigator.appVersion.substring(0, 3) != '2.0') {
						paramSize = paramBit.length;
					}
					else {
						paramSize = arraySize;
					}
					for (var lLoop = 0; lLoop < paramSize; lLoop++) {
						if (paramBit[lLoop].indexOf('PageNav=') != -1) {
							paramBit[lLoop] = 'PageNav=' + strTarget;
						}

						if (lLoop > 0) {
							pageBit[1] += '&' + paramBit[lLoop];
						}
						else {
							pageBit[1] = paramBit[lLoop];
						}
					}
				}
			}

			// Add the Parameters if supplied
			if (bParam) {
				strURL += '&' + pageBit[1];
			}
			else if (pageBit.length != 1) {
				strURL += '?' + pageBit[1];
			}

		}
		else {
			// Build our Target URL
			strURL += '?PageNav=' + strTarget;
		}

		// Remove Category if Changing Category 'tracker 7811 added 'fertilizers'
		if ((strTarget == 'Chemicals') || (strTarget == 'Fertilizers') || (strTarget == 'Crude') || (strTarget == 'Shipping') || (strTarget == 'BaseOils') || (strTarget == 'Conductors')) {
			if (strURL.indexOf('Category=') != -1) {
				var strFront = strURL.slice(0, strURL.indexOf('Category=') - 1);
				var strEnd = strURL.slice(strURL.indexOf('Category=') + 10);
				strURL = strFront + strEnd;
			}
		}

		// Goto to the page
		//alert('the strURL = ' + strURL);
		window.document.frmGeneral.action = strURL;
		window.document.frmGeneral.submit();
	}
}

function showNews(strURL) {

	window.name = "Icismainzz1";
	// If there are any subscriber messages to show 												
	if (document.frmGeneral.Messages.value > 0) {
		// show the popup window with the news - size is dependant on browser type
		if (navigator.appName.indexOf('Netscape') != -1) {
			newswin = open(strURL, "News", "width=475,height=300,scrollbars=1,location=0,status=0");
		}
		else {
			newswin = open(strURL, "News", "width=490,height=300,scrollbars=1,location=0,status=0");
		}
	}

	// Generic Functionality to Get the Browser Type if 
	// Not already available and add to the BrowserType Hidden
	var browser = document.frmGeneral.BrowserType.value;
	if (browser.length == 0) {
		document.frmGeneral.BrowserType.value = navigator.appName;
	}
}

function SubmitAndRedirect(strURL) {
	document.frmGeneral.action = strURL;
	document.frmGeneral.submit();
}

function listSelect() {
	// Get the Value
	var indexValue = document.frmGeneral.navList.selectedIndex;
	var navID = document.frmGeneral.navList.options[indexValue].value;

	// Call Page Navigation
	pageNav(navID);
}

function getCheckedUnCheckedIDs(FormName, CheckBoxName) {
	var sCheckedItemIDs = '';
	var sUnCheckedItemIDs = '';

	if (document.forms == null) {
		// Retrieve Previous Information
		sCheckedItemIDs = eval('document.' + FormName + '.txtCheckedIDs.value');
		sUnCheckedItemIDs = eval('document.' + FormName + '.txtUnCheckedIDs.Value');
	}
	else {
		// Retrieve Previous Information
		sCheckedItemIDs = eval('document.' + FormName + '.txtCheckedIDs.value');
		sUnCheckedItemIDs = eval('document.' + FormName + '.txtUnCheckedIDs.Value');

		with (eval('document.' + FormName)) {
			// Loop through the Criteria's Form Elements
			for (var i = 0; i < elements.length; i++) {
				if (elements[i].name == CheckBoxName) {
					if (elements[i].checked) {
						// Add them to the list of Checked ItemIds
						sCheckedItemIDs += elements[i].value + ',';
					}
					else {
						// This check box is NOT checked.
						sUnCheckedItemIDs += elements[i].value + ',';
					}
				}
			}
		}  //End of the for loop. 
	}
	// Fill in the hidden field values.
	//NOTE: These are default values for the Hidden fields.
	eval('document.' + FormName + '.txtCheckedIDs.value = sCheckedItemIDs');
	eval('document.' + FormName + '.txtUnCheckedIDs.value = sUnCheckedItemIDs');
}

function GetTickedItems() {
	var sItemIds = '';
	var s = 0;
	var sname = '';

	// DJR 26/09/00
	// Amended following line to force else statement code
	// to be run. 
	if (document.form_null) {
		//if (document.form_nav) {	
		sItemIds = document.form_nav.CheckedItemIds.value;
		//window.alert('sItemIds = ' + sItemIds);
	}
	else {
		with (document.frmGeneral) {
			// Loop through the Criteria's Form Elements
			for (var i = 0; i < elements.length; i++) {
				// Look at the Item element checkboxes
				//window.alert((elements[i].name));
				//sname = elements[i].name;
				// s = sname.indexOf('chk');  
				if (elements[i].name == 'CheckedItemIds') {
					//if s.valueOf() > 0 {
					// Only interested in the Checked Items
					if (elements[i].checked) {
						// Add them to the list of Checked ItemIds
						sItemIds += elements[i].value + ',';
					}
				}
			}
		}
	}
	//alert 'Itemids ' + sItemIds;
	return (sItemIds);
}

function launch() {
	var newfile = window.open("graph_options.htm", "options", "resizable=yes,height=150,width=350,scrollbars=auto,location=0,status=0");
	newfile.focus();
}

function launchPA(w) {

	//	var newfile = window.open("../../il_secure/il_price_alert/il_home.asp","options","resizable=yes,height=450,width=660,scrollbars=auto,location=0,status=0")

	var newfile = window.open("../../il_secure/il_price_alert/il_home.asp", "options", "resizable=yes,height=450,width=" + w + ",scrollbars=auto,location=0,status=0");
	// put code here to detect browser type.
	// if IE4 is found do not try to set focus
	// now put back as per JS 24/09/01
	newfile.focus();
}

function launchPAprompt(w) {

	var newfile = window.open("../../il_secure/il_price_alert/il_message.htm", "options", "resizable=yes,height=450,width=" + w + ",scrollbars=auto,location=0,status=0");
	newfile.focus();
}

function launchPAPopup(w) {
	//alert('Oops');
	if (gblWindowHandle && gblWindowHandle.closed) {
		// Already open so set focus and exit
		if (gblWindowHandle.name = 'options') {
			gblWindowHandle.focus();
			return;
		}
	}
	// If here then we need to open the window and set focus (May need a time delay here)
	var gblWindowHandle = window.open("../../il_secure/il_price_alert/il_popup/pu_nonsubscriber.htm", "options", "resizable=no,height=450,width=" + w + ",scrollbars=yes,location=0,status=0");
	gblWindowHandle.focus();
}

function doSplit(strString, splitter) {

	// Perform a Javascript Split if available
	if (navigator.appVersion.substring(0, 3) != '2.0') {
		return strString.split(splitter);
	} else {
		return splitIt(strString, splitter);
	}

}

function splitIt(strString, splitter) {
	var strData = new String(strString);
	var strBits = new String();
	var lenSub = 0;
	var cutBits;
	var nCount = 0;

	// Process through the data to find the bits
	while (strData.length != 0) {

		// Append to the array
		strBits.length = nCount + 1;

		if (strData.indexOf(splitter) != -1) {
			lenSub = strData.indexOf(splitter);

			strBits[nCount] = strData.substring(0, lenSub);
			strData = strData.substring((lenSub + 1), strData.length);
		}
		else {
			strBits[nCount] = strData;
			strData = '';
		}
		nCount++;
	}

	arraySize = nCount;
	return strBits;
}

var bUnloadInCode;

function SaveSubDetails() {
	// redirect to the same page and save the details 
	// needs to differentiate between a new and an existing user		
	document.frmGeneral.AlreadySaved.value = 'true';
	bUnloadInCode = true;
	if (window.confirm('Save changes?')) {
		document.frmGeneral.GoneThroughConfirm.value = 'true';
	} else {
		document.frmGeneral.GoneThroughConfirm.value = 'false';
	}
	document.frmGeneral.action = document.frmGeneral.HidURL.value;
	//alert('URL = ' + document.frmGeneral.HidURL.value);
	document.frmGeneral.submit();
}

function SavePADetails() {
	var sItems = '';
	// redirect to the same page and save the details 
	// needs to differentiate between a new and an existing user		
	document.frmGeneral.AlreadySaved.value = 'true';
	bUnloadInCode = true;
	if (window.confirm('Save changes?')) {
		document.frmGeneral.GoneThroughConfirm.value = 'true';
	} else {
		document.frmGeneral.GoneThroughConfirm.value = 'false';
	}
	//sItems=GetTickedItems();
	//alert('items '+ sItems);
	//document.frmGeneral.action = "adm_pa_product_list.asp?action=save";
	document.frmGeneral.action = document.frmGeneral.HidURL.value;
	//alert('URL = ' + document.frmGeneral.HidURL.value);
	document.frmGeneral.submit();
}

function SaveDetails() {
	// redirect to the same page and save the details 
	// needs to differentiate between a new and an existing user		
	document.frmGeneral.AlreadySaved.value = 'true';
	bUnloadInCode = true;
	if (window.confirm('Save changes?')) {
		document.frmGeneral.GoneThroughConfirm.value = 'true';
	} else {
		document.frmGeneral.GoneThroughConfirm.value = 'false';
	}
	document.frmGeneral.action = document.frmGeneral.HidURL.value;
	//alert('URL = ' + document.frmGeneral.HidURL.value);
	document.frmGeneral.submit();
}

function linkclicked(sAction, sURL) {
	document.frmGeneral.action = sURL;
	document.frmGeneral.RequestedURL.value = sURL;
	document.frmGeneral.ListType.value = sAction;
	document.frmGeneral.submit();
}

function PAAdminHome() {
	// redirect to adm_price_alert_home.asp
	document.frmGeneral.URL.value = 'adm_price_alert_home.asp';
	document.frmGeneral.action = document.frmGeneral.URL.value;
	document.frmGeneral.submit();
}
