﻿function myPage_ClientValidate() {
	var i;
	for (i = 0; i < Page_Validators.length; i++) {
		ValidatorValidate(Page_Validators[i]);
	}
	ValidatorUpdateIsValid();    
	ValidationSummaryOnSubmit();
	Page_BlockSubmit = !Page_IsValid;
	if (!Page_IsValid) {
		//alert("moving divMain now");
		moveControlDown(divMain, calculateShift());
	}
	return Page_IsValid;
}
function onPageValidate(top) {
	repositionControl(divMain, top, divMain.style.left);
	if (typeof(myPage_ClientValidate) == 'function') {
		//alert("page_clientvalidate is function")
		if ( myPage_ClientValidate() == true ) {
		}
	}
}

function moveControlDown(ctlToMove, distance) {
	ctlToMove.style.top = String(getNumericPart(ctlToMove.style.top) + calculateShift()) + "px";
}

function calculateShift() {
	return 21 * countErrors();
}

function countErrors() {
	var numOfErrors;
	var i;
	numOfErrors = 0;
	for (i = 0; i < Page_Validators.length; i++) {
		if (!Page_Validators[i].isvalid) {
			numOfErrors++;
		}
	}
	return numOfErrors;
}

function getNumericPart(styleString) {
	return parseInt(styleString.substring(0, styleString.indexOf("px")));
}

function repositionControl(ctlToMove, top, left) {
	ctlToMove.style.top = top;
	ctlToMove.style.left = left;
}


function Start(page,PopUp,ScnName,ShowName,MainPg) 
{
    //when putting in the dialog box url use ../../../../common/dialog_boxes/SAVE01.aspx 
    //depending on the name of the dialogbox (SAVE01.aspx or CALC01.aspx)
    //and the number of directories to go up ==> ../../../../ or ../../../

    //RENAME uses the save01.aspx box
	
    var windowW=424; // wide
    var windowH=224; // high
    var windowX = (screen.width/2)-(windowW/2);
    var windowY = (screen.height/2)-(windowH/2);
    var autoclose = true;
    var page1= "../../../../common/dialog_boxes/" + page+"?Popup=" + PopUp + "&Name=" + ScnName + "&ShowName=" + ShowName + "&MainPage=" + MainPg;
    //
    //?Popup=2&Name="+scenarioName+"&ShowName=1&MainPage="+MainPage

    OpenWin = this.open(page1, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=" + windowW + ",height=" + windowH + ",top=" + windowY + ",left=" + windowX );
    OpenWin.focus();

    if (autoclose){
	    window.onunload = function(){OpenWin.close()};
    }

}


function ReloadMainPage()
{	
		//alert("You are in CloseWindow()");
		if (document.all['hdnCloseFlag'].value == "1")
		{	
			//alert("hdnCLoseFlag = 1");
			// window.opener.location.reload();
			window.opener.location = window.opener.location;
			//window.opener.location.href = "investment_account_asset_allocation.aspx";
			window.close();
		}
}

function Start2(page,PopUp,ScnName,ShowName,MainPg,NewName) 
{
    //when putting in the dialog box url use ../../../../common/dialog_boxes/SAVE01.aspx 
    //depending on the name of the dialogbox (SAVE01.aspx or CALC01.aspx)
    //and the number of directories to go up ==> ../../../../ or ../../../

    //RENAME uses the save01.aspx box
	
    var windowW=424; // wide
    var windowH=224; // high
    var windowX = (screen.width/2)-(windowW/2);
    var windowY = (screen.height/2)-(windowH/2);
    var autoclose = true;
    var page1= "../../../../common/dialog_boxes/" + page+"?Popup=" + PopUp + "&Name=" + ScnName + "&ShowName=" + ShowName + "&MainPage=" + MainPg + "&NewName=" + NewName;
    //
    //?Popup=2&Name="+scenarioName+"&ShowName=1&MainPage="+MainPage

    OpenWin = this.open(page1, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=" + windowW + ",height=" + windowH + ",top=" + windowY + ",left=" + windowX);
    OpenWin.focus()

    if (autoclose){
	    window.onunload = function(){OpenWin.close()};
    }

}

//
//	The following is to validate a date input being valid
//
var dtCh= "/";
var minYear=1800;
var maxYear=2500;

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
	} 
	return this;
}

function dateValidate(sender, args){
	var dtStr = args.Value;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.length != 2 || strMonth.length != 2 || strYear.length != 4) {
		args.IsValid = false;
		return;
	}
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		args.IsValid = false;
		return;
	}
	if (month<1 || month>12){
		//alert("Please enter a valid month")
		args.IsValid = false;
		return;
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		args.IsValid = false;
		return;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		args.IsValid = false;
		return;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		args.IsValid = false;
		return;
	}
	args.IsValid = true;
	return;
}

function openSupport(nodekey) {
	var strHref;
	
	strHref = "../../../../reference/subsections/support/pages/SupportPopup.aspx?NodeKey=" + nodekey + "&amp;NodeDepth=5";
	window.open(strHref,"SupportPopup","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=600,height=420");
}

function openPopup(vstrURL,vintWidth,vintHeight)
{
	var winleft = (screen.width - vintWidth) / 2;
	var wintop = (screen.height - vintHeight) / 2;
	window.open(vstrURL, '_blank', 'menubar=no, toolbar=no, scrollbars=yes, width=' + vintWidth + ', height=' + vintHeight + ', top='+wintop+', left='+winleft+', resizable');
}
	
function addItemToDropDownList(vstrDropDownID, vstrText, vstrValue)
{
	var intNewIndex = document.all[vstrDropDownID].options.length;
    document.all[vstrDropDownID].options[intNewIndex] = new Option(vstrText,vstrValue,true);
}

function openPopup(vstrURL,vintWidth,vintHeight)
{
	var winleft = (screen.width - vintWidth) / 2;
	var wintop = (screen.height - vintHeight) / 2;
	window.open(vstrURL, '_blank', 'menubar=no, toolbar=no, scrollbars=yes, width=' + vintWidth + ', height=' + vintHeight + ', top='+wintop+', left='+winleft+', resizable');
}

function onWebPageValidate()
{			
	
	var i;
	for (i = 0; i < Page_Validators.length; i++)
	{
		ValidatorValidate(Page_Validators[i]);
	}
	
	ValidatorUpdateIsValid();    
	ValidationSummaryOnSubmit();
	Page_BlockSubmit = !Page_IsValid;
			
}
// Displays the Warning Popup			
function openWarningPopup() {
	if (window.WarningPopUp) {
		WarningPopUp();
		window.WarningPopUp = false;
	}
}		

function AJAXDefaultLock(vstrID, vstrButtonID, vstrProcessingID, blnLock)
{
    var strCursor;
    var strDisplay;
    var strPrefix = vstrID;
    if(blnLock) {
        strCursor = 'wait';
        strDisplay = '';
    } else {
        strCursor = 'hand';
        strDisplay = 'none';
    }
    
    if (strPrefix.length > 0)
        strPrefix+= '_';
    if($get(strPrefix + vstrButtonID)) {
        $get(strPrefix + vstrButtonID).disabled = blnLock;
        $get(strPrefix + vstrButtonID).style.cursor = strCursor;
    }
    if($get(strPrefix + vstrProcessingID)) {
        $get(strPrefix + vstrProcessingID).style.display = strDisplay;
    }
}

function defaultAction(defaultButton)
{
    if(event.keyCode==13){
        defaultButton.click();
        event.keyCode=0;
    }
}

function onError(objError, userContext, methodName)
{
    // Display the error message. 
    if($get(mstrMasterPrefix + 'divErrorContainer'))
        $get(mstrMasterPrefix + 'divErrorContainer').innerHTML = '<span style="color:red">' +  objError.get_message() + '</span>';
    Sys.Debug.trace(objError.get_message());
} 

var cintDefaultNumber = -2147483648;
var cdtmDefaultDate = new Date('');
var cstrDefaultString = '';
function InitDefaultValues(vintDefaultNumber, vdtmDefaultDate, vstrDefaultString) {
    cintDefaultNumber = vintDefaultNumber;
    cdtmDefaultDate = vdtmDefaultDate;
    cstrDefaultString = vstrDefaultString;
}

var mstrMasterPrefix = '';
function setMasterPerfix(vstrPrefix) {
    mstrMasterPrefix = vstrPrefix;
}

var mstrSameAccount = '';
function setSameAccount(vstrAccount) {
    mstrSameAccount = vstrAccount;
}

function getRadioListSelectedValue(vstrRadioListID) {
    var intCount = 0;
    var intReturn = cintDefaultNumber;
    while (($get(vstrRadioListID + '_' + intCount)) && (intReturn == cintDefaultNumber))
    {
        if($get(vstrRadioListID + '_' + intCount).checked)
            intReturn = $get(vstrRadioListID + '_' + intCount).value;
        intCount++;
    }
    return intReturn;
}

function getRadioListSelectedItem(vstrRadioListID) {
    var intCount = 0;
    var objReturn = null;
    while (($get(vstrRadioListID + '_' + intCount)) && (objReturn == null))
    {
        if($get(vstrRadioListID + '_' + intCount).checked)
            objReturn = $get(vstrRadioListID + '_' + intCount);
        intCount++;
    }
    return objReturn;
}

function $value(vstrID) {
    var objReturn;
    var objElem = $get(vstrID);
    if(objElem) {
        // Doesn't exist, no value to return
        objReturn = objElem.value;
        if(objReturn == null) {
            // Doesn't have a value, see if it's a checkbox
            objReturn = objElem.checked;
            if(objReturn == null) {
                // Not checked, maybe it's a RadioList
                if($get(vstrID + '_0')) {
                    objReturn = getRadioListSelectedValue(vstrID);
                } else {
                    // Still nothing? Return the innerHTML...
                    objReturn = objElem.innerHTML;
                }
            }
        }
    }
    return objReturn;
}

function mLoad()
{
    if(window.load)
        load();
}
function myOpenPop(url,name)
{
    newname=window.open(url,name,'width=394,height=415,top=100,left=400,resizable,scrollbars');
    newname.focus();
}
function IsSameAcct()
{
    return document.all.cboAccountNumber.value != mstrSameAccount;
}
