/* external script file                            */
/* Copyright ©1997-2006                            */
/* Sage Software, Inc.                             */
/* All Rights Reserved                             */
/* default.js                                      */

	function focusIt() {
		if (document.cookie.indexOf('AXPrefs') > -1) {
			document.mainform.ckActiveMail.checked = (cookie.getCookieParm('withAM', 'AXPrefs') == "T");
		}
		document.mainform.user.focus();
	}


function GetTimeZoneInfo()
{
	// output:

	// iBias		:	bias in minutes from GMT
	// iDLTBias		:	change in bias (in minutes) when daylight time is in effect
	// bDLT			:	true if Daylight time observed, false if not
	//					If false, the rest of the output variables will be 0
	// idowStart	:	0(Sunday)..6(Sat) for the day of week of daylight time start
	// idowEnd		:	0(Sunday)..6(Sat) for the day of week of daylight time end

	// inthDayStart	:	1(1st)..5(last) to indicate the nth day of month format of the TIME_ZONE_INFORMATION structure for
	//					daylight time start
	// inthDayEnd	:	1(1st)..5(last) to indicate the nth day of month format of the TIME_ZONE_INFORMATION structure for
	//					daylight time end

	// iMonthStart	:	1(Jan)..12(Dec) to indicate the month of the TIME_ZONE_INFORMATION structure for daylight time start
	// iMonthEnd	:	1(Jan)..12(Dec) to indicate the month of the TIME_ZONE_INFORMATION structure for daylight time end

	// iHourStart	:	0..23 to indicate daylight change time of the TIME_ZONE_INFORMATION structure for daylight time start
	// iHourEnd		:	0..23 to indicate daylight change time of the TIME_ZONE_INFORMATION structure for daylight time end

	// first piece of code determines if the bias changes at two arbritrary points in the current year

	// ------------------------------
	// output vars
	var iBias			= 0;
	var iDLTBias		= 0;
	var bDLT			= false;
	var idowStart		= 0;
	var idowEnd			= 0;
	var inthDayStart	= 0;
	var inthDayEnd		= 0;
	var iMonthStart		= 0;
	var iMonthEnd		= 0;
	var iHourStart		= 0;
	var iHourEnd		= 0;
	var strTZInfo		= "";
	var tzValues;

	// start of routine
	var iYear = new Date().getYear();
	//var iYear = 2004;
	var aDateTest1 = new Date(iYear, 0, 1);
	var aDateTest2 = new Date(iYear, 7, 1);

	iBias = aDateTest1.getTimezoneOffset();
	if ( aDateTest1.getTimezoneOffset() != aDateTest2.getTimezoneOffset())
	// if they don't equal, then we have a change in bias at some point in the year
	{
		// starting from March 1st, we will scan each day
		// and determine when the bias changes.
		// This date was chosen, as no timezones in the
		// Windows Registry have daylight times outside of this.
		var aDate1 = new Date(iYear, 0, 1);

		// Get the start
		var iOldOfs = aDate1.getTimezoneOffset();
		var bFound = false;

		var iHour1 = 0;

		for (var iLoop = 0; iLoop < 180; iLoop++)
		{
			aDate1.setDate( aDate1.getDate()+ 1);
			if (iOldOfs != aDate1.getTimezoneOffset())
			{
				aDate1.setDate( aDate1.getDate() - 1);
				var iHrs = aDate1.getHours();
				for (var iHour = iHrs; iHour < iHrs + 24; iHour++)
				{
					aDate1.setHours(iHour);
					if (iOldOfs != aDate1.getTimezoneOffset())
					{
						aHour1 = aDate1.getHours();
						bFound = true;
						break;
					}
				}
				break;
			}
		}

		if (bFound)	// search for end period
		{
			// determine begin date
			var aDate2 = new Date(iYear, 7, 1);		// starting from August 1st

			var iOldOfs = aDate2.getTimezoneOffset();
			var bFound = false;

			var aHour2 = 0;

			for (var iLoop = 0; iLoop < 180; iLoop++)
			{
				aDate2.setDate( aDate2.getDate() + 1);
				if (iOldOfs != aDate2.getTimezoneOffset())
				{
					aDate2.setDate( aDate2.getDate() - 1);
					// now find the actual hour it crosses over
					// NOTE: the result is always +1 hour, as the
					// JScript date functions automatically add the bias.
					// E.g. if you set date to 02:00:00, at the time daylight
					// adjusts, it will return 03:00:00!
					var iHrs = aDate2.getHours();
					for (var iHour = iHrs; iHour < iHrs + 24; iHour++)
					{
						aDate2.setHours(iHour);
						if (iOldOfs != aDate2.getTimezoneOffset())
						{
							aHour2 = aDate2.getHours();
							bFound = true;
							break;
						}
					}
					break;
				}
			}

			if (bFound)
			{
				bDLT = true;

				if (aDate1.getTimezoneOffset() > aDate2.getTimezoneOffset())
				{
					iBias			= aDate1.getTimezoneOffset();
					iDLTBias		= aDate2.getTimezoneOffset() - aDate1.getTimezoneOffset();

					iHourStart		= aHour2 - 1;
					if (iHourStart < 0)
						iHourStart += 24;

					idowStart		= aDate2.getDay();
					iMonthStart		= aDate2.getMonth();
					inthDayStart	= getnthWeek(aDate2);

					iHourEnd		= (aHour1 + 1) % 24;
					idowEnd			= aDate1.getDay();
					iMonthEnd		= aDate1.getMonth();
					inthDayEnd		= getnthWeek(aDate1);

				}
				else
				{
					//WScript.echo("2");
					//WScript.echo(aDate1.getTimezoneOffset());
					//WScript.echo(aDate2.getTimezoneOffset());
					iBias			= aDate2.getTimezoneOffset();
					iDLTBias		= aDate1.getTimezoneOffset() - aDate2.getTimezoneOffset();

					iHourStart		= aHour1 - 1;
					if (iHourStart < 0)
						iHourStart += 24;

					idowStart		= aDate1.getDay();
					iMonthStart		= aDate1.getMonth() + 1;
					inthDayStart	= getnthWeek(aDate1);

					iHourEnd		= (aHour2 + 1) % 24;
					idowEnd			= aDate2.getDay();
					iMonthEnd		= aDate2.getMonth() + 1;
					inthDayEnd		= getnthWeek(aDate2);
				}
			}
			else
			{
				//WScript.echo("No daylight time");
			}

		}
	}

	tzValues = new Array(iBias, iDLTBias, bDLT, idowStart, idowEnd, inthDayStart, inthDayEnd, iMonthStart, iMonthEnd, iHourStart, iHourEnd);
	strTZInfo = tzValues.toString();

	document.mainform.tz_info.value = strTZInfo;

	function getLastDayofYear(aDate)
	{
		var aDate2 = new Date(aDate.getYear(), aDate.getMonth() < 5 ? 1 : 5, 1);

		var iOldOfs = aDate2.getTimezoneOffset();


		for (var iLoop = 0; iLoop < 180; iLoop++)
		{
			aDate2.setDate( aDate2.getDate() + 1);
			if (iOldOfs != aDate2.getTimezoneOffset())
			{
				aDate2.setDate( aDate2.getDate() - 1);
				return (Math.floor((aDate2.getDate() - 1) / 7) + 1);
			}
		}
		return (0);
	}

}

function getnthWeek(aDate) {
	// Given a date, this returns the nth week the date is on.
	//  Example, if the date is the first tuesday in the month,
	//  this function returns 1.
	//  If the date is the last Sunday in the month, this function
	//  will return either 4 or 5 depending on the year.  (There might
	//  be 4 of that day this year, but 5 next year.)
	if (!aDate) {
		return 0;
	}
	var d = aDate.getDate();
	var dayOfWeek = aDate.getDay();
	var weekNum = 0;
	var tempDate = aDate;
	for (var i = 1; i < d+1; i++) {
		tempDate.setDate(i);
		if (tempDate.getDay() == dayOfWeek) {
			weekNum++;
		}
	}
	return weekNum;
}


function submitMe() {
    document.mainform.submit();
}




function updateAllData(obj) {
//alert("infunction")
   // alert(top.main_content.query.document.getElementsByName("querydata").length);
    //var x = top.main_content.query.querydata.document.getElementsByName(obj.name);
    	// alert(x[0].value);

//    //check for the field on the tab level
//    if (top.main_content.query.document.getElementsByName("tabs").length > 0) {
//        var x = top.main_content.query.tabs.document.getElementsByName(obj.name);
//        if (x.length > 0) {
//            x[0].value = obj.value;
//        }
//    }
    var objName = obj.name;
    $([name = objName]).each(function () {
        //alert($(this).val());
        //frames layout while in custoppmain
        //top.frame[0].document is topnav, [1] = left, [2] = main, [3] = right
        //top.frame[2].frames[0].document is a container iframe
        //top.frame[2].frames[0].frames[0].document is oppinfo
        //top.frame[2].frames[0].frames[1].document is tabs, which now contains infolevel
        //Next line is the reference to Oppinfo
        // alert(objName)
//        var topfram = top.frames[2].frames[0].frames[0].document.id
//        alert(topfram)
        //alert($(topfram + objName).name);
    });

//    //check for the field on the enrollment level
//    if (top.main_content.query.querydata.document.getElementsByName("oppselinfo").length > 0) {
//        var x = top.main_content.query.querydata.oppselinfo.document.getElementsByName(obj.name);
//        if (x.length > 0) {
//            x[0].value = obj.value
//        }
//    }

    //Check for the field at the info level
    if (top.main_content.document.getElementsByName("infodisp").length > 0) {
        var x = top.main_content.infodisp.document.getElementsByName(obj.name);
        if (x.length > 0) {
            x[0].value = obj.value;
        }
    }
}
function saveAllData() {
    //alert("infunction")
    // alert(top.main_content.query.document.getElementsByName("querydata").length);
    //var x = top.main_content.query.querydata.document.getElementsByName(obj.name);
    // alert(x[0].value);

    //check for the field on the tab level
    if (top.main_content.query.document.getElementsByName("querydata").length > 0) {
        top.main_content.query.document.querydata.submitMe();
    }

    //check for the field on the enrollment level
    if (top.main_content.query.querydata.document.getElementsByName("oppselinfo").length > 0) {
        top.main_content.query.querydata.oppselinfo.submitMe()
    }

    //Check for the field at the info level
    if (top.main_content.document.getElementsByName("infodisp").length > 0) {
        top.main_content.infodisp.submitMe();
       
    }
}

function formatCurrency(num) {

		if (num.length >= 2) {	
		if (num.substring(0,1) == "(") {
		   num = num.substring(1,num.length);
		   num = "-" + num.substring(0,num.length -1);
		  // alert(num);
	    }}
    num = num.toString().replace(/\$|\,/g,'');	
	if (num == "") {
	  return '';
	} else { 
	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));

	var cAmt = (((sign)?'':'-') + '$' + num + '.' + cents);
	
	if (cAmt == "$0.00"){
	return ""
	} else {

	return (((sign)?'':'-') + '$' + num + '.' + cents);	
 
	}
     }
}

function setAX() {
	/* Set the ActiveX preferences in a cookie so this page will remember what the user chose last time.   */
	/* Note: These cookies are used later to decide whether or not to enable certain functionality.        */
	/* For example, on leftnav and in the opportunity statistics dialog, the withRpt cookie is used        */
	/* to determine whether or not to show the reports button.  If you customize this to remove the        */
	/* option to choose either of these, you will need to either set the cookies appropriatly, or update   */
	/* any place that looks for these cookies.                                                             */
	var withActiveMail = (document.mainform.ckActiveMail.checked) ? "T" : "F";
	var cookStr = "withAM=" + withActiveMail
  d = new Date();
  d.setFullYear(d.getFullYear() + 1);
	document.cookie = "AXPrefs=" + escape(cookStr) + "; expires=" + d.toGMTString();
	document.mainform.nextpage.value = (withActiveMail == "T") ? "mainpage" : "mainpagenoax";
}

function showMoreInfo() {
	var address = htmlpath + "/activexinfo.htm";
	var win = window.open(address, 'AlarmMgrWin', 'width=425,height=425,directories=no,location=no,menubar=no,status=yes,scrollbars=yes,resizable=yes,titlebar=no,toolbar=no');
}


$(document).ready(function () {

    $(function () {
        $(".datePicker").datepicker();
    });
    $(function () {
        $(".tabs").tabs({
            ajaxOptions: {
                error: function (xhr, status, index, anchor) {
                    $(anchor.hash).html(
						"Couldn't load this tab. We'll try to fix this as soon as possible. ");
                }
            }

        });
    });

//    ,
//                success: function () {
//                    if ($('.ui-tabs-panel')) {
//                        $('.ui-tabs-panel').jScrollPane();
//                    }
//                }

    $(".currDisp").blur(function () {
        this.value = formatCurrency(this.value);
    });

    $('input[type="text"]').bind("blur", function () {
        updateAllData(this);
    });

    $(".currDisp").bind("keyup", function () {
        addPage();
    });




  //  $(function () {
//
 //       if ($('.ui-tabs-panel')) {
  //          $('.ui-tabs-panel').jScrollPane();
   //     }
   // });


    $.fn.sumValues = function () {
        var sum = 0;
        this.each(function () {
            if ($(this).is(':input')) {
                var val = $(this).val();
            } else {
                var val = $(this).text();
            }
            sum += parseFloat(('0' + val).replace(/[^0-9-\.]/g, ''), 10);
        });
        return formatCurrency(sum);
    };

    $.fn.replaceClass = function (toReplace, replaceWith) {
        //  alert(replaceWith)
        return $(this).each(function () {
            return $(this).removeClass(toReplace).addClass(replaceWith);
        });
    }


});


