var gAutoPrint = true;

function leftTrim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) {
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trim(sString) {
	return leftTrim(rightTrim(sString));
}

function URLEncode(saString) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = saString;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}





function Clickheretoprint()
{ 
  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 

  var content_vlue = document.getElementById("print_content").innerHTML; 
  
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><head><title>Inel Power System</title>'); 
   docprint.document.write('</head><body onLoad="self.print()"><center>');          
   docprint.document.write(content_vlue);          
   docprint.document.write('</center></body></html>'); 
   docprint.document.close(); 
   docprint.focus(); 
}



function formattedPrint() {
	if (document.getElementById != null) {
		var html = '<HTML>\n<HEAD>\n';
		if (document.getElementsByTagName != null) {
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0) {
				html += headTags[0].innerHTML;
			}
		}
		html += '\n</HEAD>';

		var printReadyElement = document.getElementById("formattedPrint");
		
		if (printReadyElement != null) {
				html += printReadyElement.innerHTML;
		} else {
			alert("There is no text on this page for printing.");
			return;
		}
			
		html += '\n</BODY>\n</HTML>';
		
		var printWin = window.open("","printWindow");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();


		var inputs = printWin.document.getElementsByTagName("a");
		var ii;
		if( null != inputs ) {
			for (ii = 0; ii < inputs.length; ++ii) {
				inputs[ii].href="javascript:void(0);";
			}
		}
		inputs = printWin.document.getElementsByTagName("input");
		if( null != inputs ) {
			for (ii = 0; ii < inputs.length; ++ii) {
				inputs[ii].disabled=true;
			}
		}
		inputs = printWin.document.getElementsByTagName("TEXTAREA");
		if( null != inputs ) {
			for (ii = 0; ii < inputs.length; ++ii) {
				inputs[ii].disabled=true;
			}
		}
		inputs = printWin.document.getElementsByTagName("select");
		if( null != inputs ) {
			for (ii = 0; ii < inputs.length; ++ii) {
				inputs[ii].disabled=true;
			}
		}
		var skips = printWin.document.getElementById("noprint");
		if( null != skips ) {
			skips.style.visibility = 'hidden';
		}

		if (gAutoPrint) {
			printWin.print();
			printWin.close();
		}		
	} else {
		alert("The Print-ready feature is not available for your browser.");
	}
}

function WindowOnload() {
	show_menu_prep();
}

function show_menu_prep() {
// Hides the two sub-menues ('Add a New Product' and 'Manage Catalog' sub-menus)
	show_menu();
	show_menu2();
	show_menu3();
	show_menu4();
	show_menu5();
}

function show_menu(id) {
// Display sub-menu '1' ('Add a New Product')
	if (document.getElementById('smenu1'))
		document.getElementById('smenu1').style.display='none';
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
		hideForm('hidden');
		}
	else
		hideForm('visible');
}

function show_menu2(id) {
// Display sub-menu '2' ('Manage Catalog')
	if (document.getElementById('smenu2'))
		document.getElementById('smenu2').style.display='none';
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
		hideForm2('hidden');
		}
	else
		hideForm2('visible');
}

function show_menu3(id) {
// Display sub-menu '2' ('Manage Catalog')
	if (document.getElementById('smenu3'))
		document.getElementById('smenu3').style.display='none';
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
		hideForm2('hidden');
		}
	else
		hideForm2('visible');
}

function show_menu4(id) {
// Display sub-menu '4' ('Manage Catalog')

	if (document.getElementById('smenu4'))
		document.getElementById('smenu4').style.display='none';
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
		hideForm2('hidden');
		}
	else
		hideForm2('visible');
}

function show_menu5(id) {
// Display sub-menu '5' ('Manage Catalog')
	if (document.getElementById('smenu5'))
		document.getElementById('smenu5').style.display='none';
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
		hideForm2('hidden');
		}
	else
		hideForm2('visible');
}

function hideForm(state) {
// Hide select boxes for menu '1' roll-over
	if (document.getElementById('hideSelect')) {
		if(state == "hidden")
			document.getElementById('hideSelect').style.visibility = "hidden";
		else
			document.getElementById('hideSelect').style.visibility = "visible";
	}
}

function hideForm2(state) {
// Hide select boxes for menu '2' roll-over
	if (document.getElementById('hideSelect2')) {
		if(state == "hidden")
			document.getElementById('hideSelect2').style.visibility = "hidden";
		else
			document.getElementById('hideSelect2').style.visibility = "visible";
	}
	if (document.getElementById('hideSelect3')) {
		if(state == "hidden")
			document.getElementById('hideSelect3').style.visibility = "hidden";
		else
			document.getElementById('hideSelect3').style.visibility = "visible";
	}
}

function open_window(loc) {
	/*
	var arrDomain = document.domain.split(".");
	//The 1st time the function is called, the domain is set
	//Subsequent calls should not reser the domain
	if (arrDomain.length==3) {
		if(navigator.appName=="Netscape"){
			document.domain = arrDomain[1] + "." + arrDomain[2] + ":80";
		}else{
			document.domain = arrDomain[1] + "." + arrDomain[2];
		}
	}
	*/
	var w = window.open(loc,"Model_Details","scrollbars,width=500,height=600,resizable=yes");
	return;
}

function open_window_simple(loc, name) {
	var newWindow = window.open(loc,name,"left=50,top=50,height=500,width=440,resizable=yes,menubar=no,scrollbars=no");
	newWindow.focus();
}

function open_search_window_include_all(fieldName, formName) {
	var loc =
	"peopleSearch.do?formName=" + formName + 
	   "&fieldName=" + fieldName + "&includeTerminated=true";

	open_window_simple(loc, "new_search_window");
}

function open_search_window(fieldName, formName) {
	var loc =
	"peopleSearch.do?formName=" + formName + 
	   "&fieldName=" + fieldName + "&includeTerminated=false";

	open_window_simple(loc, "new_search_window");
}


function formatCurrency(field) {
	var num = field.value;
	if (isBlank(num)) {
		field.value = "";
		return;
	}

	var Chars = "0123456789.,";

	for (var i = 0; i < num.length; i++) {
		if (Chars.indexOf(num.charAt(i)) == -1) {
			alert("Invalid Character(s)\n\nOnly numbers (0-9), a comma and a period are allowed in this field.");
			field.value = "";
			field.focus();
			return;
		}
	}

	num = num.toString().replace(/\,/g,'');

	if(isNaN(num)) {
		num = "0";
	}

	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	field.value = (num + '.' + cents);
}

