//  Javascript utility functions

// opens a new window
function OpenWindow ( url )
	{
	window.open(url,'','scrollbars=yes,resizable=yes,width=630,height=450');
	// window.open(url,'','');
	}

function debug ( str )
	{
	top . window . frames [ "hiddenFrame" ].document . writeln (str );
	}

function closeWindow ()
	{
	window.close();
	}

	
	


// Removes leading and trailing spaces from the passed string. Also removes
// consecutive spaces and replaces it with one space. If something besides
// a string is passed in (null, custom object, etc.) then return the input.

function trim ( inputString )
	{
	return trimChar ( inputString, " " );
	}


function trimChar ( inputString, trimChar )
	{
	// alert ("Trimming  " + inputString);
	if (typeof inputString != "string") { return inputString; }
	if (inputString == null || inputString.length <1) { return inputString; }

	var retValue = inputString;
	var ch = retValue.substring(0, 1);


	while (ch == trimChar)
		{
		// Check for spaces at the beginning of the string
		retValue = retValue.substring ( 1, retValue.length ) ;
		ch = retValue.substring ( 0, 1 ) ;
		}

	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == trimChar)
		{
		// Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
		}

	/*
	while (retValue.indexOf("		") != -1)
		{
		// Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("		")) + retValue.substring(retValue.indexOf("		")+1, retValue.length); // Again, there are two spaces in each of the strings
		}
	*/

 	return retValue; // Return the trimmed string back to the user
	}



function trimLeadingChar ( inputString, lChar )
	{
	// alert ("Trimming  " + inputString);
	if (typeof inputString != "string") { return inputString; }
	if (inputString == null || inputString.length <1) { return inputString; }

	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == lChar)
		{
		// Check for char at the beginning of the string
		retValue = retValue.substring ( 1, retValue.length ) ;
		ch = retValue.substring ( 0, 1 ) ;
		}

 	return retValue; // Return the trimmed string back to the user
	}


function formatPrice (price, bgColour) {

	 // document . writeln ( "formating " + price);
	if (price == null || price.length <1 )
		return "";
	price = trim(price);
	// check again
	if (price == null || price.length <1 || price=="0" || price == "0.00" || price == "0.0000")
		return "";

	var decIndex = price.indexOf('.');

        if (decIndex < 1) {
          return price;
        }

        var lastDec = price.length - 1;
        for (; lastDec > (decIndex + 2); lastDec--) {
          if (price.charAt(lastDec)!='0') {
            break;
          }
        }
        lastDec++;


	var htmlPrice = price.substring(0,lastDec) + "<font color='"+bgColour+"'>"+price.substring(lastDec,price.length)+"</font>";
	// alert (htmlPrice);

	return htmlPrice;

}



 function submitList ( event ) 
	{
	myFrame = top . window . frames [1];  // need to get frame first to resolve target resolution problems
	eventField = myFrame . document . getElementById ("hiddenEvent") ;
	eventField.value = event;

	myForm = myFrame . document . getElementById ("form1") ;
	// alert(myForm.name);
	myForm.submit();
	}

function submitEvent ( frame, form, event, redirect )
	{
	myFrame = top . window . frames [frame];

	// set event
	eventElm = myFrame . document . getElementById ("event");
	//alert("Current event is  " + eventElm.value );
	eventElm.value = event;
	//alert("Set event to  " + eventElm.value );

	// set redirect
	//alert(event + "Redirect " +redirect);



	redirectElm = myFrame . document . getElementById (event + "Redirect");
	redirectElm.value = redirect;
	//alert("Set " + event + " Redirect" + " to " + redirectElm.value );


	myForm = myFrame . document . getElementById (form) ;
	// alert(myForm.name);
	// myForm.submit();
	myFrame . smartSubmit();

	}



function promptSubmitEvent(frame,form,event,redirect)
	{
	var msg = "Editing the product list will result \n in all prices being requoted." ;

	if ( confirm ( msg ) )
		{
		myFrame = top . window . frames [frame];

		// set event
		eventElm = myFrame . document . getElementById ("event");
		//alert("Current event is  " + eventElm.value );
		eventElm.value = event;
		//alert("Set event to  " + eventElm.value );

		// set redirect
		//alert(event + "Redirect " +redirect);


		redirectElm = myFrame . document . getElementById (event + "Redirect");
		redirectElm.value = redirect;
		//alert("Set " + event + " Redirect" + " to " + redirectElm.value );


		myForm = myFrame . document . getElementById (form) ;
		// alert(myForm.name);
		myForm.submit();
        }
	}



function activateQuote()
	{
	var msg = "Editing the product list will result \n in all prices being requoted.";

	if ( confirm ( msg ) )
		{
		myFrame = top . window . location = cActivateOrderUrl ;
		}
	}




