//------------------------------------------------------------------------
// Declarations 
//------------------------------------------------------------------------
var isNS = navigator.appName.indexOf("Netscape") >= 0
var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 && (parseFloat(navigator.appVersion) >= 4 && parseFloat(navigator.appVersion) < 5)) ? 1 : 0;
var isMinNS6 = (parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;
var wpopup;
//------------------------------------------------------------------------
function ExitToMainMenu()
{
	if(confirm("Are you sure you would like to exit to the Main Menu?"))
	{
		document.location = "Menu.aspx";
	}
}
//------------------------------------------------------------------------
function LogOut()
{
	if(confirm("Are you sure you would like to exit this application? If you are in the middle of editing your data, please save prior to exiting."))
	{
	}
}
//------------------------------------------------------------------------
function GetCookie (name) { 
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length; 
    var i = 0;  
    while (i < clen) {    
        var j = i + alen;    
        if (document.cookie.substring(i, j) == arg){      
            return GetCookieValue(j);    
        }
        i = document.cookie.indexOf(" ", i) + 1;    
        if (i == 0) break;   
    } 
    return null;
}
//------------------------------------------------------------------------
function SetCookie (name, value, expires, path, domain, secure) 
{  
    var defaultExpiration = new Date();
    defaultExpiration.setTime(defaultExpiration.getTime() + 1 * 24 * 60 * 60 * 1000);//Expire in 1 day
    var argv = SetCookie.arguments;  
    var argc = SetCookie.arguments.length;  
    var expires = (argc > 2) ? argv[2] : null;  
    if (expires == null)
        newExp = defaultExpiration;
    else
        newExp = expires;
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  
    document.cookie = name + "=" + escape (value) + 
        ((newExp == null) ? "" : ("; expires=" + newExp.toGMTString())) + 
        ((path == null) ? "" : ("; path=" + path)) +  
        ((domain == null) ? "" : ("; domain=" + domain)) +    
        ((secure == true) ? "; secure" : "");
}
//------------------------------------------------------------------------
function GetCookieValue (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
//------------------------------------------------------------------------
function DeleteCookie (name) {  
    var exp = new Date();  
    exp.setTime (exp.getTime() - 1);   
    var cval = GetCookie (name);  
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
//------------------------------------------------------------------------
function PopSizable(sFile) {
	var sPlat = new String(navigator.platform);
	var sBrow = new String(navigator.appVersion);

	if (sPlat.indexOf("Mac")>=0 && sBrow.indexOf("MSIE")>=0) {
	} else {
		CloseWnd("wpopup");
	}
	//wpopup = CenterWindow("","w_popup","width=550,height=450,titlebar=yes,menubar=yes,scrollbars,status,resizable=yes,location=no,toolbar=yes");
	wpopup=window.open("","w_popup","width=550,height=450,titlebar=yes,menubar=yes,scrollbars,status,resizable=yes,location=no,toolbar=yes");
	wpopup.location.href=sFile;
	if (wpopup.opener==null) wpopup.opener=window;
	wpopup.opener.name = "opener";
	wpopup.focus();
}       
//------------------------------------------------------------------------
function PopSizableCustom(sFile, Width, Height) {
	var sPlat = new String(navigator.platform);
	var sBrow = new String(navigator.appVersion);

	if (sPlat.indexOf("Mac")>=0 && sBrow.indexOf("MSIE")>=0) {
	} else {
		CloseWnd("wpopup");
	}
	var params = "width=" + Width + ",height=" + Height + ",titlebar=yes,menubar=yes,scrollbars,status,resizable=yes,location=no,toolbar=yes";

	if (window.screen) {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - parseInt(Width)) / 2;
        var yc = (ah - parseInt(Height)) / 2;

		params += ",left=" + xc + ",screenX=" + xc;
        params += ",top=" + yc + ",screenY=" + yc;
    }

	wpopup=window.open("","w_popup",params);
	wpopup.location.href=sFile;
	if (wpopup.opener==null) wpopup.opener=window;
	wpopup.opener.name = "opener";
	wpopup.focus();
}       
//------------------------------------------------------------------------
function PopChild(sFile) {
	var sPlat = new String(navigator.platform);
	var sBrow = new String(navigator.appVersion);

	if (sPlat.indexOf("Mac")>=0 && sBrow.indexOf("MSIE")>=0){
	} 
	else 
	{
		CloseWnd("wpopup");
	}
	//wpopup=CenterWindow("","w_popup","width=550,height=400,titlebar=no,menubar=no,scrollbars,status");
	wpopup=window.open("","w_popup","width=550,height=400,titlebar=no,menubar=no,scrollbars,status");
	wpopup.location.href=sFile;
	if (wpopup.opener==null) wpopup.opener=window;
	wpopup.opener.name = "opener";
	wpopup.focus();
}  
//------------------------------------------------------------------------
function CloseWnd(cName){
	var oWnd = eval(cName);
	var sPlat = new String(navigator.platform);
	var sBrow = new String(navigator.appVersion);
	if (sPlat.indexOf("Mac")>=0 && sBrow.indexOf("MSIE")>=0) {
			//if (typeof(oWnd)=="object") {
			//      oWnd.close();
			//}
	} else {                
		if (typeof(oWnd) != "undefined"){
			if (typeof(oWnd) == "object" || typeof(oWnd.name) == "string"){
				if (oWnd != null){
					if (typeof(oWnd.close) == "function" || typeof(oWnd.close) == "object")
					{
						oWnd.close();
					}
					eval(cName + "=null");
					oWnd = null;
				}
			}
		}
	}
}
//------------------------------------------------------------------------
function CloseWindow()
{
	window.close();
}
//------------------------------------------------------------------------
function CenterWindow(url, name, height, width) {
    var str = "height=" + height + ",innerHeight=" + height;
    str += ",width=" + width + ",innerWidth=" + width;
    if (window.screen) {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - width) / 2;
        var yc = (ah - height) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }
    return window.open(url, name, str);
}

function OpenCenteredWindow(sName, sUrl, iWidth, iHeight)
{
	var sParams = 'resizable=no,scrollbars=yes,menubar=no,toolbar=no,location=no,width='+iWidth+',height='+iHeight
    if (window.screen) {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - iWidth) / 2;
        var yc = (ah - iHeight) / 2;

        sParams += ",left=" + xc + ",screenX=" + xc;
        sParams += ",top=" + yc + ",screenY=" + yc;
    }

	window.open(sUrl,sName,sParams);
}		

//------------------------------------------------------------------------
function isNumeric(strValue) 
{
    var valid = "0123456789";
    var ok = true;
    var temp;
    for (var i=0; i<strValue.length; i++) {
        temp = "" + strValue.substring(i, i+1);
        if (valid.indexOf(temp) == "-1"){
            ok = false;
        }
    }
	return ok;
}

//========================================================================================================================================================================================================================================================================
function Decimal(number, places) 
{
	var fn = number * Math.pow(10,places);
	number = Math.round(fn);
	return number;
}

//========================================================================================================================================================================================================================================================================
function JustNumber(cNum)
{
	var cNewNum = "";
	
	for (var nIter = 0; nIter < cNum.length; nIter++){
		cChar = cNum.substring(nIter,nIter+1);
		if ((parseInt(cChar)>=0 && parseInt(cChar) <=9) || cChar== "."){
			cNewNum = cNewNum + cChar;
		}
	}
	
	if (isNaN(parseFloat(cNewNum))){
		cNewNum = "0";
	}
	
	return parseFloat(cNewNum);
}

//========================================================================================================================================================================================================================================================================
function justInt(cNum){
	var cNewNum;
	cNewNum = "";
			
	for (var nIter = 0; nIter < cNum.length; nIter++){
		cChar = cNum.substring(nIter,nIter+1);
		if (cChar == ".")
		{
			break;
		}
			
		if ((parseInt(cChar)>=0 && parseInt(cChar) <=9)){cNewNum = cNewNum + cChar;}
	}
			
	if (isNaN(parseFloat(cNewNum))){cNewNum = "0";}
		return parseFloat(cNewNum);
}	

//========================================================================================================================================================================================================================================================================
function formatnumberold(number,places) {

	var fnumber = "0.00";

	number = Decimal(number,places);
			
	if (number || 0) {
		fnumber = number.toString(10);
				
		//fnumber = fnumber.substr(0, fnumber.length-places) + "." + fnumber.substr(fnumber.length-places);
		// concatonate leading zeros
		var fconcat = '';
		if (fnumber.length < places) {
			for (var icnt=0; icnt < places; icnt++){fconcat = fconcat.concat('0')}
		}
		fnumber = fconcat.concat(fnumber);
			
		var flength = fnumber.length-places;
		if (places == 0){
			fnumber = fnumber.substr(0, flength);
		}else{
			fnumber = fnumber.substr(0, flength) + "." + fnumber.substr(flength);
		}
	}
	return fnumber;
}

//-------------------------------------------------------
function FormatNumber(symbol, padLeft, number, places, separator)
{
	var fnumber = "0.00";
	if(places == 0)
	{
		fnumber = "0";
	}
	
	if (typeof(separator) == "undefined") {separator = ""}
	
	number = Decimal(number,places);
	
	if (number || 0) {
		fnumber = number.toString(10);

		var fconcat = '';
		if (fnumber.length < places) {
			for (var icnt=0; icnt < places; icnt++){fconcat = fconcat.concat('0')}
		}
		fnumber = fconcat.concat(fnumber);

		var flength = fnumber.length-places;
		if(flength == 0)
		{
			fnumber = fnumber.substr(0, flength) + padLeft + "." + fnumber.substr(flength);
		}
		else
		{
			if(places > 0)
			{
				fnumber = fnumber.substr(0, flength) + "." + fnumber.substr(flength);
			}
			else
			{
				fnumber = fnumber.substr(0, flength);
			}
		}
	}

	// this will add the speparator if there is one.	
	if (fnumber > 999.99 && separator != "")
	{
		var theNumber = fnumber.split(".");
		var wholeNumber = theNumber[0];
		var theLength = wholeNumber.length;

		fnumber = "";
		
		for (var i = 0; theLength > i; i++)
		{
			if (((theLength - i) % 3 == 0) && (theLength > 3))
			{
				fnumber =  fnumber + separator;
			}
			fnumber = fnumber.concat(wholeNumber.substr(i,1));
		}

		if (theNumber.length > 1)
		{
			fnumber = fnumber + "." + theNumber[1];
		}
	}
	
	return symbol + fnumber;
}

//------------------------------------------------------------------------
function parseQueryString (str, item) 
{
    str = str ? str : location.search;
    var query = str.charAt(0) == '?' ? str.substring(1) : str;
    var args = new Object();
    if (query) {
        var fields = query.split('&');
        for (var f = 0; f < fields.length; f++) {
            var field = fields[f].split('=');
            args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
        }
    }
    for (var arg in args) {
        if (arg == item){
            return args[arg];
        }
    }
    return "";
}
//------------------------------------------------------------------------
function ValidateName(source, args)
{
	var firstname = document.getElementById("beneficiaryEdit:txtFirstName");
	firstname = firstname.value;
	
	var lastname = document.getElementById("beneficiaryEdit:txtLastName");
	lastname = lastname.value;
	
	var trust = document.getElementById("beneficiaryEdit:txttrust");
	trust = trust.value;
	alert("validatename");
	if (firstname == "" && lastname == "" && trust == "")
	{
		args.IsValid = false;
	}
}
//------------------------------------------------------------------------
function ClientValidateNameOrTrust(source, args)
{
	var firstname = document.getElementById("beneficiaryEdit:txtFirstName");
	firstname = firstname.value;
	
	var lastname = document.getElementById("beneficiaryEdit:txtLastName");
	lastname = lastname.value;
	
	var trust = document.getElementById("beneficiaryEdit:txttrust");
	trust = trust.value;
	
	if ((firstname != "" || lastname != "" ) && trust !== "")
	{
		args.IsValid = false;
	}
}
//------------------------------------------------------------------------
function ValidateLastNameClient(source, args)
{
	var firstname = document.getElementById("beneficiaryEdit:txtFirstName");
	firstname = firstname.value;
	
	var lastname = document.getElementById("beneficiaryEdit:txtLastName");
	lastname = lastname.value;
	
	if (firstname != "" && lastname == "" )
	{
		args.IsValid = false;
	}

}
//----------------------------------------------------------------------------
function ValidateFirstNameClient(source, args)
{
	var firstname = document.getElementById("beneficiaryEdit:txtFirstName");
	firstname = firstname.value;
	
	var lastname = document.getElementById("beneficiaryEdit:txtLastName");
	lastname = lastname.value;
	
	if (firstname == "" && lastname != "" )
	{
		args.IsValid = false;
	}

}
//----------------------------------------------------------------------------