

// round the number to a given number of decimal places.
function roundNumber(num, dec) {
	//alert('num is ' + num);
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


// display all of the properties of an object
function dumpProps(obj, parent){
	// go through all of the properties of the object
	for (var i in obj) {
		// if a parent was passed in, then use that to build the message. Message includes i (the  object's property name) then the object's property value
		if(parent){var msg = parent + '.' + i + '\n' + obj[i];}
		else {var msg = i + '\n' + obj[i];}
		if(!confirm(msg)){ return; }
		if(typeof obj[i] == 'object'){
			if(parent){dumpProps(obj[i], parent + '.' + i);}
			else {dumpProps(obj[i], i);}
		}
	}
}	 

// insert an element after another element
function insertAfter(newElement, targetElement){
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement){ parent.appendChild(newElement);}
	else {parent.insertBefore(newElement, targetElement.nextSibling);}
}

// use dom to get the value of code:
function get_code(){
   	var hiddenArray = document.getElementsByTagName('input');
   	for (var i=0; i < hiddenArray.length; i++){
		if(hiddenArray[i].name == 'code'){
			//alert('code for i = ' +i + '  ' + hiddenArray[i].value);
			return hiddenArray[i].value;
			break;
		}
	}
}			   		
  
// need a request object to create an ajax request
function get_request_object(){
    var request = null;
    try {
        request = new XMLHttpRequest(); 
    }
    catch(trymicrosoft){
        try {
            request = new ActiveXObject("Microsoft.MsxmlHTTP");
        }
        catch(othermicrosoft){
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(failed){
                request = null;
            }
        }
    }
    if(request == null){alert("Error creating request object")}
    return request;
}
// calls all the properties of an object
function listProperties(obj, objName){
	var result = "";
	for(var i in obj){
		result += objName + "." + i + "=" + obj[i] + "\n";
	}
	alert(result);
} 

// calls the printer for a document
function varitext(text){
	text=document
	print(text)
}
function click_to_print(){ 
		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 newWindow(url, winWidth, winHeight, type) {
	//alert('type is ' + type);
	if(type == 'regular'){ // open a regular window
		regularWindow = window.open(url,'regularWin');		
	}
	else if(type == 'sub_frm'){
		var windowFeatures = 'height=' + winHeight + ',width=' + winWidth + ',resizable, scrollbars';
		sub_frm = window.open(url,'subForm', windowFeatures); 	
	}
	else {
    	helpWindow = window.open(url,'helpWin','toolbar=0,location=0,left=250%,top=150,screenX=0,screenY=100,status=no,menubar=no,scrollbars=1,resizable=no,width=' + winWidth + ',height=' + winHeight);
	}
}

function closeHelpWindow(){self.close();}

// functions used date checking a field
<!-- Begin
function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
datefield.select();
alert("That date is invalid.  Please try again.");
datefield.focus();
return false;
}
else {
return true;
   }
}
// validates a date as not being earlier than today or later than one year from today:
function validateDate(objName) {
	var dateIn = objName.value;
	var partsArray = [];
	partsArray = dateIn.split(' ');
	if(partsArray[0] == 'Jan'){partsArray[0] = 0}
	if(partsArray[0] == 'Feb'){partsArray[0] = 1}
	if(partsArray[0] == 'Mar'){partsArray[0] = 2}
	if(partsArray[0] == 'Apr'){partsArray[0] = 3}
	if(partsArray[0] == 'May'){partsArray[0] = 4}
	if(partsArray[0] == 'Jun'){partsArray[0] = 5}
	if(partsArray[0] == 'Jul'){partsArray[0] = 6}
	if(partsArray[0] == 'Aug'){partsArray[0] = 7}
	if(partsArray[0] == 'Sep'){partsArray[0] = 8}
	if(partsArray[0] == 'Oct'){partsArray[0] = 9}
	if(partsArray[0] == 'Nov'){partsArray[0] = 10}
	if(partsArray[0] == 'Dec'){partsArray[0] = 11}
	// alert("month is " + partsArray[0]);
	// alert("year is " + partsArray[2]);
	// alert("day is " + partsArray[1]);
	
	var dateRef = new Date(partsArray[2], partsArray[0], partsArray[1], 23, 59, 59);
	// alert("dateRef is " + dateRef);
	
	var today = new Date();
	var future = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
	if(dateRef < today){
		alert ("The date you entered is in the past.  Try again");
		objName.value = '';
		objName.focus();
		}
	if(dateRef > future){
		alert("The date you selected is too far into the future.  Try again");
		objName.value = '';
		objName.focus();
		}
}
// returns the last day of a given month
function getMonthLastDay(aDate){
    var m = new Number(aDate.getMonth());
    var y = new Number(aDate.getYear());

    var tmpDate = new Date(y, m, 28);
    var checkMonth = tmpDate.getMonth();
    var lastDay = 27;

    while(lastDay <= 31){
        temp = tmpDate.setDate(lastDay + 1);
        if(checkMonth != tmpDate.getMonth())
            break;
        lastDay++
    }
    aDate.setDate(lastDay);
  //  var dateOut = aDate.getFullYear() + '-' + aDate.getMonth + 1 + '-' + aDate.getDate;
  	
    return aDate;
}
// validates that a transaction date is not greater than the end of next month:
function validateTransDate(dateIn) {
	var dateInObj = new Date();
	
	var parts = [];
	parts = dateIn.split('-');
	dateInObj.setFullYear(parts[0]);
	dateInObj.setMonth(parts[1] - 1);
	dateInObj.setDate(parts[2]);
	
	//alert('date in is ' + dateIn);
	var today = new Date();
	today.setMonth(today.getMonth() + 1);
	var nextMonth = getMonthLastDay(today);
	//alert('nextMonth is ' + nextMonth + ' and dateInObj is ' + dateInObj);	
	if(dateInObj > nextMonth){
		alert ("The date you entered is in the future and will be posted to next months GL.");
	}
	
	return true;
}


function chkdate(objName) {
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}

function callAHAH(url, pageElement, callMessage) {
    document.getElementById(pageElement).innerHTML = callMessage;
    try {
        req = new XMLHttpRequest(); /* e.g. Firefox */
    }
    catch(e) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
        }
        catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
            }
            catch (E) {
                req = false;
            }
        }
    }
    req.onreadystatechange = function() {responseAHAH(pageElement);};
    req.open("GET",url,true);
    req.send(null);
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    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));
    //return (((sign)?'':'-') + '$' + num + '.' + cents);
    return (((sign)?'':'-') +  num + '.' + cents);
}
function clean(num) {
	var numType = typeof num;
	if(numType == 'number'){ return num }
	else if (numType == 'string')  {
		num = +num.replace(/[^\d\.-]/g,'');  
		return parseFloat(num);   
	}
	else { return 0}
}
function responseAHAH(pageElement) {
    var output = '';
    if(req.readyState == 4) {
        if(req.status == 200) {
            output = req.responseText;
            document.getElementById(pageElement).innerHTML = output;
        }
        else {
           document.getElementById(pageElement).innerHTML="ahah error:\n" +
               req.statusText;
       }

    }
}
sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}


//  End -->

