function getBanner(inNumOfChoices)
{
    var imgElement = document.getElementById("bannerImg");

    // Get a random number between 0 and inNumOfChoices

    id = Math.floor(Math.random() * inNumOfChoices);
    imgElement.src = imgElement.src + "banner" + id + ".gif";
}



function addBookmark()
{
    var browser	       = navigator.appName;
    var browserVersion = parseInt(navigator.appVersion);

    if ((browser == "Microsoft Internet Explorer") && (browserVersion >= 4))
    {
	window.external.AddFavorite("http://www.calypso.net.au/", "My CalypsoNet");
    }
    else
    {
       alert("Press Ctrl+D to bookmark us, after clicking OK below.");
    }
}



function setWindowStatus(inStatusMsg)
{
    if (inStatusMsg == null)
	window.status = "";
    else
	window.status = inStatusMsg;

    return(true);
}



function openWholesaler(inUrl)
{
    var width	= 776;
    var height	= 558;
    var left	= (screen.width	 - width)  / 2;
    var top	= (screen.height - height) / 2;
    var params	= "toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, menubar=no, resizable=yes," +
		  "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;

    window.open(inUrl, "", params);

    return(false);
}



function reloadParent()
{
    // Get the (parent) window that opened this window.

    var parentWindow = window.opener;

    // If the parent window exists and the URL its currently at begins with "http://www.calypso.net.au/",
    // then reload the parent window.

    if ((parentWindow != null) && !parentWindow.closed)
    {
	try
	{
	    var parentUrl = new String(parentWindow.location);
	    parentUrl	  = parentUrl.toLowerCase();

	    var matches = parentUrl.match("^http://www.calypso.net.au/");

	    if (matches)
		parentWindow.location.reload();
	}
	catch (exception)
	{
	    // Access denied on parentWindow.location property. Just close current window.
	    // The function returns straight after executing the catch block.

	    window.close();
	}
    }

    // Close the current (child) window.

    window.close();
}



function submitQuickLaunch(inSelectElem)
{
    var quickLaunchForm = document.getElementById("quickLaunchForm");

    // Open the selected wholesaler.
    openWholesaler(quickLaunchForm.action + "?wholesalerId=" + inSelectElem.options[inSelectElem.selectedIndex].value);

    // Reset the select list to the first item.
    inSelectElem.options[0].selected = true;
}



function newWindow(inURL)
{
    var newWindow = window.open(inURL);

    if ((navigator.userAgent.toLowerCase().indexOf("msie") == -1) || (parseInt(navigator.appVersion) >= 5))
	newWindow.focus();

    return(false);
}


function showChoicePopupFrontPage(inUrl1, inUrl2, inLogo, inGoogleAnalyticsPath)
{
	var hideLightBox = "$.nyroModalRemove();";

	var wholesaler1 = document.getElementById("wholesaler1");
	var wholesaler2 = document.getElementById("wholesaler2");

	// De-select any previous selection
	wholesaler1.checked = false;
	wholesaler2.checked = false;

	// Set the onclick for each radio
	var track = "_gaq.push(['_trackPageview', '" + inGoogleAnalyticsPath + "']);";

	wholesaler1.onclick=Function(hideLightBox + "openWindow('" + inUrl1 + "', 'null', 'null', '1');" + track);
	wholesaler2.onclick=Function(hideLightBox + "openWindow('" + inUrl2 + "', 'null', 'null', '1');" + track);

	// Set the logo source
	document.getElementById("logo").src=inLogo;
}


function showChoicePopupActive(inWholesalerId1, inWholesalerId2, inLogo, inWholesalerName1, inWholesalerName2)
{
	var hideLightBox = "$.nyroModalRemove();";

	var wholesaler1 = document.getElementById("wholesaler1");
	var wholesaler2 = document.getElementById("wholesaler2");

	// De-select any previous selection
	wholesaler1.checked = false;
	wholesaler2.checked = false;

	// Set the onclick for each radio
	var track1 = "_gaq.push(['_trackPageview', '/apps/portal/agweb/mycalypsonet/au/" + inWholesalerName1 + "']);";
	var track2 = "_gaq.push(['_trackPageview', '/apps/portal/agweb/mycalypsonet/au/" + inWholesalerName2 + "']);";

	var openWholesaler1 = "return openWholesaler('preWholesalerLogin.do?wholesalerId=" + inWholesalerId1 + "');";
	var openWholesaler2 = "return openWholesaler('preWholesalerLogin.do?wholesalerId=" + inWholesalerId2 + "');";

	wholesaler1.onclick=Function(hideLightBox + openWholesaler1 + track1);
	wholesaler2.onclick=Function(hideLightBox + openWholesaler2 + track2);

	// Set the logo source
	document.getElementById("logo").src=inLogo;
}



function openWindow(inClient, inAgSu, inAuNz, inType)
{
    var width	= 776;
    var height	= 558;
    var left	= (screen.width - width) / 2;
    var top	= (screen.height - height) / 2;
    var params1	= "toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, menubar=no, resizable=yes," +
		  "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
    var params2 = "toolbar=yes, location=yes, directories=yes, status=yes, scrollbars=yes, menubar=yes, resizable=yes," +
		  "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
    var url;

    if (inAuNz == "au")
	url = "https://www.calypso.net.au/" + inAgSu + "/" + inClient + "?o=p";
    else
    if (inAuNz == "nz")
	url = "https://calypsoweb.co.nz/" + inAgSu + "/" + inClient + "?o=p";
    else
	url = inClient;

    if (inType == 1)
	window.open(url, "", params1);
    else
	window.open(url, "", params2);
}



function openWindowUrl(inUrl)
{
    var width	= 776;
    var height	= 558;
    var left	= (screen.width - width) / 2;
    var top	= (screen.height - height) / 2;
    var params1 = "toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, menubar=no, resizable=yes," +
		  "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;

    window.open(inUrl, "", params1);
}



function openPrintableDisclaimer()
{
    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* Obtain disclaimer from text area of create user form.									 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    var disclaimer = document.getElementById("disclaimer").innerHTML;

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* Open new window and populate the page with a title and heading.								 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    var disclaimerWindow = open("","","toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes," +
				 "resizable=yes,width=600,height=700");

    disclaimerWindow.document.write("<html>");
    disclaimerWindow.document.write("<head>");
    disclaimerWindow.document.write("<title>CalypsoNet - Disclaimer and Privacy</title>");
    disclaimerWindow.document.write("</head>");
    disclaimerWindow.document.write("<body>");
    disclaimerWindow.document.write("<h2>CalypsoNet - Disclaimer and Privacy</h2><br/>");

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* For every new line character found ('\n'), write the characters up to and including the new line character to the page,	 */
    /* and then write an additional <br/> tag.											 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    var newLineIndex = disclaimer.indexOf('\n');

    while (newLineIndex >= 0)
    {
	disclaimerWindow.document.write(disclaimer.substring(0, newLineIndex + 1));
	disclaimerWindow.document.write("<br/>");

	disclaimer = disclaimer.substring(newLineIndex + 1, disclaimer.length);
	newLineIndex = disclaimer.indexOf('\n');
    }

    disclaimerWindow.document.write(disclaimer);

    disclaimerWindow.document.write("</body>");
    disclaimerWindow.document.write("</html>");
    disclaimerWindow.document.close();
}



function checkSelections(inFieldIds)
{
    for (index in inFieldIds)
    {
	checkSelection(inFieldIds[index]);
    }
}



function checkSelection(inFieldId)
{
    var field		  = document.getElementById(inFieldId);
    var fieldOtherHeading = document.getElementById(inFieldId + "OtherHeading");
    var fieldOther	  = document.getElementById(inFieldId + "Other");

    if (field.options[field.selectedIndex].innerHTML == "Other")
    {
	showVisibility(fieldOtherHeading);
	showVisibility(fieldOther);
	enableField(fieldOther);
    }
    else
    {
	hideVisibility(fieldOtherHeading);
	hideVisibility(fieldOther);
	disableField(fieldOther);
	clearContent(fieldOther);
    }
}



function focusOnField(inFieldId)
{
    anchor(inFieldId);
    cursorOnField(inFieldId);
}



function showAddWholesaler(inClickedButton)
{
    var addForm	  = document.getElementById("wholesalerAddForm");
    var addDiv	  = document.getElementById("wholesalerAddDiv");
    var addButton = document.getElementById("wholesalerAddButton");

    noneDisplay(addButton);
    blockDisplay(addDiv);
    location.hash = "#wholesalerAddDiv";

    // Hide status and/or error messages when the user manually clicked the "Add wholesaler" button.

    if (inClickedButton)
	hideStatusErrorMsg();

    focusFirstElement(addForm);
}



function showChangeWholesaler(inWholesalerName, inWholesalerId, inIsContainChildren)
{
	var header     = document.getElementById("wholesalerChangeHeader");
    var changeForm = document.getElementById("wholesalerChangeForm");
    var changeDiv  = document.getElementById("wholesalerChangeDiv");
    var nameLabel  = document.getElementById("wholesalerChangeNameLabel");
    var nameCell   = document.getElementById("wholesalerChangeName");
    var idHidden   = document.getElementById("wholesalerChangeIdHidden");
    var addDiv	   = document.getElementById("wholesalerAddDiv");
    var addButton  = document.getElementById("wholesalerAddButton");

    var errorChangeLogin    = document.getElementById("errorChangeLogin");
    var errorChangePassword = document.getElementById("errorChangePassword");

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* "Change Wholesaler" is currently hidden											 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    if (changeDiv.style.display == "none")
    {
	if (addDiv)
	{
	    if (addDiv.style.display == "block")
	    {
		noneDisplay(errorChangeLogin);
		noneDisplay(errorChangePassword);
	    }

	    noneDisplay(addButton);
	    noneDisplay(addDiv);
	}

	if (inIsContainChildren != null)
	{
		if (inIsContainChildren == "true")
		{
			header.innerHTML = "Change Wholesalers";
			nameLabel.innerHTML = "Wholesalers:";
		}
		else
		{
			header.innerHTML = "Change Wholesaler";
			nameLabel.innerHTML = "Wholesaler:";
		}
	}

	if (inWholesalerName != null) nameCell.innerHTML = inWholesalerName;
	if (inWholesalerId   != null) idHidden.value	 = inWholesalerId;

	blockDisplay(changeDiv);

	location.hash = "#wholesalerChangeDiv";
    }

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* "Change Wholesaler" is already displaying but the wholesaler to change is different					 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    else if (idHidden.value != inWholesalerId)
    {
    	if (inIsContainChildren == "true")
    	{
    		header.innerHTML = "Change Wholesalers";
    		nameLabel.innerHTML = "Wholesalers:";
    	}
    	else
    	{
    		header.innerHTML = "Change Wholesaler";
    		nameLabel.innerHTML = "Wholesaler:";
    	}

    	nameCell.innerHTML = inWholesalerName;
    	location.hash = "#wholesalerChangeDiv";

    	resetForm(document.getElementById("wholesalerChangeForm"));
    	noneDisplay(errorChangeLogin);
    	noneDisplay(errorChangePassword);

    	idHidden.value = inWholesalerId;
    }

    hideStatusErrorMsg();
    focusFirstElement(changeForm);

    return(false);
}



function showMyWholesaler()
{
    var addDiv	   = document.getElementById("wholesalerAddDiv");
    var changeDiv  = document.getElementById("wholesalerChangeDiv");
    var addButton  = document.getElementById("wholesalerAddButton");
    var addForm	   = document.getElementById("wholesalerAddForm");
    var changeForm = document.getElementById("wholesalerChangeForm");

    var errorAddWholesalerId = document.getElementById("errorAddWholesalerId");
    var errorAddLogin	     = document.getElementById("errorAddLogin");
    var errorAddPassword     = document.getElementById("errorAddPassword");

    var errorChangeLogin    = document.getElementById("errorChangeLogin");
    var errorChangePassword = document.getElementById("errorChangePassword");

    noneDisplay(addDiv);
    noneDisplay(changeDiv);
    blockDisplay(addButton);

    location.hash = "#";

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* Reset the "Add wholesaler" form and hide any error messages associated with the previous form submission.		 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    resetForm(addForm);
    noneDisplay(errorAddWholesalerId);
    noneDisplay(errorAddLogin);
    noneDisplay(errorAddPassword);

    /* ------------------------------------------------------------------------------------------------------------------------- */
    /* Reset the "Edit wholesaler" form and hide any error messages associated with the previous form submission.		 */
    /* ------------------------------------------------------------------------------------------------------------------------- */

    resetForm(changeForm);
    noneDisplay(errorChangeLogin);
    noneDisplay(errorChangePassword);
}



function hideStatusErrorMsg()
{
    var statusMsg = document.getElementById("statusMsg");
    var errorMsg  = document.getElementById("errorMsg");

    noneDisplay(statusMsg);
    noneDisplay(errorMsg);
}



function resetForm(inForm)
{
    for (var elemIdx = 0; elemIdx < inForm.elements.length; elemIdx++)
    {
	var elem = inForm[elemIdx];

	if (elem.type == "text")
	    clearContent(elem);
	else if (elem.type == "select-one")
	    elem.options[0].selected = true;
    }
}



function focusFirstElement(inForm)
{
    for (i = 0; i < inForm.length; i++)
    {
	var element = inForm.elements[i];

	if (element.type == "text" || element.type == "select-one" || element.type == "radio" || element.type == "password")
	{
	    cursorOnField(element.id);
	    return;
	}
    }
}



function removeWholesaler(inWholesalerName)
{
    if (confirm("Are you sure you want to remove " + inWholesalerName + "?"))
	return(true);

    return(false);
}



function cursorOnField(inFieldId)
{
    var element = document.getElementById(inFieldId);
    if (element != null) element.focus();
}



function anchor(inAnchor)
{
    window.location.hash = inAnchor;
}



function showVisibility(inElement)
{
    if (inElement != null)
	inElement.style.visibility = "visible";
}



function hideVisibility(inElement)
{
    if (inElement != null)
	inElement.style.visibility = "hidden";
}



function blockDisplay(inElement)
{
    if (inElement != null)
	inElement.style.display = "block";
}



function noneDisplay(inElement)
{
    if (inElement != null)
	inElement.style.display = "none";
}



function disableField(inField)
{
    if (inField != null)
	inField.disabled = true;
}



function enableField(inField)
{
    if (inField != null)
	inField.disabled = false;
}



function clearContent(inField)
{
    if (inField != null)
	inField.value = "";
}



function highlightRow(inRow)
{
    inRow.bgColor = "#c0cfec";
}



function unhighlightRow(inRow)
{
    inRow.bgColor = "#dde6f7";
}

