// Copyright 1994-2002 Onyx Software Corporation. All rights reserved.

var giIsChanged	= 0;
var giIsReset	= 0;

// This function should be called anytime the location of the main edit window is changed
function verifyNavAway(w) {
	
	
	if (!w) {
		var oTargetWindow		= window
	} else {
		var oTargetWindow		= w
	}
	
	// customer edit window
	try {
		if (oTargetWindow.top.oFrame.oEdit.confirmLeave) {  		
			return oTargetWindow.top.oFrame.oEdit.confirmLeave();}
	}catch(e) {}


	// customer New window  and partner administration
	try {
		if (oTargetWindow.top.oFrame.confirmLeave) {  		
			return oTargetWindow.top.oFrame.confirmLeave();}
	}catch(e) {}	
	
	 
	 
	// other windows
	
	
	//default return value	
	return true;
	
}
 

function stoperror() {
	return true;
}

function oppHTMLEncode(psString) {

	try {
		// must replace & first...
		psString = psString.replace(/&/g, "&amp;");
		psString = psString.replace(/</g, "&lt;");
		psString = psString.replace(/>/g, "&gt;");
		psString = psString.replace(/'/g , "&#039;");
		psString = psString.replace(/"/g, "&#034;");
	}
	catch(e) {}
		
	return psString;

}	

function oppHTMLUnencode(psString) {

	try {
		// must replace & first...
		psString = psString.replace(/&#034;/g, '"');
		psString = psString.replace(/&#039;/g , "'");
		psString = psString.replace(/&gt;/g, ">");
		psString = psString.replace(/&lt;/g, "<");
		psString = psString.replace(/&amp;/g, "&");
		psString = psString.replace(/&nbsp;/g, " ");
	}
	catch(e) {}
		
	return psString;

}
	

function jsAlertException( e, sURL, sLocation, sDefault, sRoot ) {
	try { 
		jsAlert(	"" + e.number + "		\n" + 
					"" + sURL + "			\n" + 
					"" + sLocation + "		\n" + 
					"" + e.description + "	\n" + 
					"------------------------------------\n\n" + 
					sDefault, sRoot );
	} catch(e){ 
		jsAlert( sDefault, APP_ROOT );
	}
}
	
function doKeyPressSubmit(oBtn, evt) { 
	try{
		if(event.keyCode == 13) { 
			oBtn.click(); 
		} 
	}
	catch(e) {
		try{
			if(evt.which == 13) {
				oBtn.onclick(); 
			}
		}
		catch(e) {}
	}
} 
function onBeforeUnloadProcessor( oControl, sDefaultText ) {
	try {
		if( oControl.focus ) {
			oControl.focus();
		}
		if( giIsChanged == 1 && giIsReset == 0 ) {
			event.returnValue		= oppHTMLUnencode(sDefaultText);
		}
	} catch(e) { }
}

function setChangeFlag() { 
	giIsChanged			= 1;
}

function resetChangeFlag() {
	giIsChanged = 0;
	giIsReset	= 1;
}

//Moves selected items from oSource to oTarget
function moveSelected( oSource, oTarget, bSort ) {
	try {
		for( var i = 0; i < oSource.options.length; i++ ) {
			if( "" != oSource.options[i].selected && oSource.options[i].value ) {
				var oNew								= new Option();
				oNew.value								= oSource.options[i].value;
				oNew.text								= oSource.options[i].text;
				oTarget.options[oTarget.options.length] = oNew;
				oSource.options[i].value				= "";
				oSource.options[i].text					= "";
			}
		}

		cleanUp(oSource);
	
		if(bSort) {
			sortOptions(oTarget);
		}
	} catch(e) { }
}

//Removes the options that were cleared by moveSelected
function cleanUp(oSource) {
	try {
		var iLength = oSource.options.length;

		for( var i = 0; i < oSource.options.length; i++ ) {
			if( "" == oSource.options[i].value ) {
				//must use this approach so that Netscape works (it cannot handle the remove method)
				oSource.options[i] = null;
				i--;
			}
		}
	} catch(e) { }
}

//Sort the options
function sortOptions(oSource)  {
	try {
		var oOptions	= new Array();
		var oTemp		= new Object();
	
		for( var i = 0; i < oSource.options.length; i++ ){
			oOptions[i] = oSource.options[i];
		}
	
		for( var x = 0; x < ( oOptions.length - 1 ); x++ ) {
			for( var y = ( x + 1 ); y < oOptions.length; y++) {
				if( oOptions[x].text.toLowerCase() > oOptions[y].text.toLowerCase() ) {
					oTemp = oOptions[x].text;
					oOptions[x].text = oOptions[y].text;
					oOptions[y].text = oTemp;
					
					oTemp = oOptions[x].value;
					oOptions[x].value = oOptions[y].value;
					oOptions[y].value = oTemp;
				}
			}
		}
		for( var i = 0; i < oSource.options.length; i++ ) {
			oSource.options[i].value = oOptions[i].value;
			oSource.options[i].text = oOptions[i].text;
		}
	} catch(e) { }
}

function jsAlert( sDisplayString, sRoot , bResponse ) {
	var iHeight 				= 120;
	var iWidth 					= 200;
	var bSuccess                = true;

	if( navigator.appName.toUpperCase().match(/NETSCAPE/) != null ) { //netscape...
		try{
			sDisplayString = unescape(sDisplayString);
		} catch(e) {}
		sDisplayString = oppHTMLUnencode(sDisplayString);
		alert(sDisplayString);
		return;
	}
	
	var iRowSize 				= 15;
	sDisplayString				= oppHTMLUnencode(sDisplayString);
	var iDisplayLength 			= String(sDisplayString).length;

	if( iDisplayLength > 33 )  {
		var iHeightAdjustFactor = iDisplayLength / 33;
		var iTempAdjust			= iHeightAdjustFactor * iRowSize
		iHeight 				= iHeight + iTempAdjust;
		iWidth 					= iWidth + iTempAdjust;
	}
	if (typeof(bResponse) == "undefined")
	var sFileName 				= "/" + sRoot + "/inc/dialogs/jsAlert.asp?sDisplay=" + sDisplayString;
    else
        var sFileName 				= "/" + sRoot + "/inc/dialogs/jsAlert.asp?sDisplay=" + sDisplayString + "&bResponse=" + bResponse;

	var sParam 					= "dialogHeight="  + String(iHeight) + "px;dialogWidth="  + String(iWidth) + "px;edge=Raised;scroll=no;help:no;resizable:no;status:no;";
	return window.showModalDialog( sFileName, "Downloads", sParam );
	
}

function jsConfirm( sDisplayString ) {

	return confirm( oppHTMLUnencode( unescape(sDisplayString) ) );	
}

function stringTrim() {
    // Trim the leading and trailing spaces from str.
    // If str is all spaces, return a empty string.
	var i = this.length;
	var x = 0;

	while (this.substring(x,x+1) == ' ') x++;
	while (this.substring(i-1,i) == ' ') i--;

	if(x == this.length && i == 0) {
	 return '';
	} else { return this.substring(x,i);
	}
}
String.prototype.trim = stringTrim;

window.onerror = stoperror;
