var dsl; if (!dsl) dsl = {};
// Config
if (!dsl.Config) dsl.Config = {};
dsl.Config.Css = { selected: "dsl_selected" }

// Util
if (!dsl.Util) dsl.Util = {};
dsl.Util.AddEvent = function(/*{}*/args) {
    var _dom = (typeof args.Dom == "string") ? document.getElementById(args.Dom) : args.Dom,
        _evt = args.Event || console.log("AddEvent must take an event"),
        _action = args.Action || function() { console.log("Add Action"); },
        _capture = args.Capture || false,
        _scope = args.Scope || _dom;
        _function = function() { _action.apply(_scope, arguments); }
        
    if (typeof _dom.addEventListener != "undefined") {
        _dom.addEventListener(_evt, _function, _capture);
    }
    else if (_dom.attachEvent != "undefined") {
        _dom.attachEvent('on' + _evt, _function);
    }
    else { _dom['on' + _evt] = _action; }
}
dsl.Util.FindDom = function(ele) {
    return (typeof ele == "string") ? document.getElementById(ele) : ele;
}
dsl.Util.AddCss = function(arg, name) {
    var dom = dsl.Util.FindDom(arg),
        classname = dom.className;
    if (typeof classname == "undefined") return;
    if (classname.search(name) == -1)
        dom.className += (classname.length > 0) ? " " + name : name;
}
dsl.Util.RemoveCss = function(arg, name) {
    var dom = dsl.Util.FindDom(arg),
        classname = dom.className;
    if (typeof classname == "undefined") return;
    dom.className = dsl.Util.NormalizeClassName(classname.replace(name, ""));
}
dsl.Util.IsArray = function(obj) {
    if (obj) return obj.constructor == Array;
    return false
}
dsl.Util.NormalizeClassName = function(classname) {
    return classname.replace(/\s{2,}/, " ");
}

// Button
dsl.Button = function(args) {
    this.Dom = dsl.Util.FindDom(args.id),
    this.NavigateUrl = this._getAttribute(args.attribute) || "";
    this._image = args.image || {};
    if (args.action) dsl.Util.AddEvent({Dom:this.Dom, Event:"click", Action:args.action});
};
dsl.Button.prototype._getAttribute = function(attr) {
    if (!this.Dom) return ""
    return this.Dom.getAttribute(attr)
};
dsl.Button.prototype._setImage = function(img) {
    if (img) this.Dom.style.backgroundImage = "url(" + img + ")"
};
dsl.Button.prototype.Navigate = function(params) {
	var _params = params || "";
	if (this.NavigateUrl) location.href = this.NavigateUrl + _params
};
dsl.Button.prototype.Disable = function() {
    this.Dom.setAttribute("disabled","true");
    if (this._image.disabled) this._setImage(this._image.disabled);
};
dsl.Button.prototype.Enable = function() {
    this.Dom.removeAttribute("disabled");
    if (this._image.enabled) this._setImage(this._image.enabled);
};

// Textbox
dsl.Textbox = function(id) {
    this.Dom = dsl.Util.FindDom(id);
};
dsl.Textbox.prototype.GetValue = function() {
    var value = this.Dom.value;
    if (!value) return ""
    return value;
};
dsl.Textbox.prototype.SetValue = function(value) {
	if (this.Dom) this.Dom.value = value;
};
dsl.Textbox.prototype.Disable = function(value) {
	if (this.Dom) this.Dom.disabled = true;
};
dsl.Textbox.prototype.Enable = function(value) {
	if (this.Dom) this.Dom.disabled = false;
};

// Checkbox
dsl.Checkbox = function(id) {
    this.Dom = document.getElementById(id);
};
dsl.Checkbox.prototype.IsSelected = function() {
    if (this.Dom.checked == true) return true
    return false
};
dsl.Checkbox.prototype.Select = function() {
    this.Dom.checked = true;
};
dsl.Checkbox.prototype.Clear = function() {
    if (this.Dom.checked) this.Dom.checked = false;
};

// Radio
dsl.Radio = function(id) {
    if (typeof id == "string")
        this.Dom = dsl.Util.FindDom(id)
    if (typeof id.radio == "string")
        this.Dom = dsl.Util.FindDom(id.radio)
    if (typeof id.label == "string")
        this.Label = dsl.Util.FindDom(id.label)
};
dsl.Radio.prototype.IsSelected = function() {
    if (this.Dom.checked == true) return true
    return false
};
dsl.Radio.prototype.GetValue = function() {
    return this.Dom.value
};
dsl.Radio.prototype.Select = function(/*optional: id*/) {
    this.Dom.checked = true;
};
dsl.Radio.prototype.Clear = function() {
    if (this.Dom.checked) this.Dom.checked = false;
};
dsl.Radio.prototype._init = function() {
};

// RadioSet
dsl.RadioSet = function(radios, paramKey) {
    this.Radios = [];
    this._paramKey = paramKey || "";
    this._init(radios);
};
dsl.RadioSet.prototype.GetValue = function(id) {
    var key = (this._paramKey) ? this._paramKey + "=" : "";
    for (var i=this.Radios.length-1; i>=0; --i) {
        if (this.Radios[i].IsSelected()) return key + this.Radios[i].GetValue();
    } return ""
};
dsl.RadioSet.prototype.Clear = function() {
    for (var i=this.Radios.length-1; i>=0; --i) {
        if (this.Radios[i].IsSelected()) this.Radios[i].Clear();
    }
};
dsl.RadioSet.prototype.ClearCss = function() {
    for (var i=this.Radios.length-1; i>=0; --i) {
        if (this.Radios[i].Label) dsl.Util.RemoveCss(loanRadioset.Radios[i].Label, dsl.Config.Css.selected);
    }
};
dsl.RadioSet.prototype._addRadio = function(id) {
    var radio = new dsl.Radio(id)
    if (!radio.Dom) return;
    this.Radios.push(radio);
};
dsl.RadioSet.prototype._init = function(radios) {
    for (var i=radios.length-1; i>=0; --i) {
        this._addRadio(radios[i])
    }
};

// DepencyRadio
dsl.DepencyRadio = function(radio,textbox,paramKey) {
    this.Radio = new dsl.Radio(radio);
    this.Textbox = this._addTextbox(textbox);
    this.paramKey = paramKey || "";
    this._init();
};
dsl.DepencyRadio.prototype.IsSelected = function() {
    return this.Radio.IsSelected()
};
dsl.DepencyRadio.prototype.GetValue = function() {
    var value = "";
    if (this.Textbox.Dom) value = this.Textbox.GetValue();
    if (dsl.Util.IsArray(this.Textbox)) {
        for ( var i=this.Textbox.length-1; i>=0; --i ) {
            var _value = this.Textbox[i].GetValue(),
                _joiner = (value != "" && _value != "") ? ", " : "";
            value += _joiner + _value
        }
    }   return value
};
dsl.DepencyRadio.prototype.IsValid = function() {
    var valid = false;
    if (this.Textbox.Dom) if (this.Textbox.GetValue() != "") valid = true;
    if (dsl.Util.IsArray(this.Textbox)) {
        valid = true;
        for ( var i=this.Textbox.length-1; i>=0; --i ) {
            if (this.Textbox[i].GetValue() == "") valid = false;
        }
    }    return valid
};
dsl.DepencyRadio.prototype.ToggleDependants = function() {
    if (this.Radio.IsSelected()) {
        if (this.Textbox.Dom) this.Textbox.Enable();
        if (dsl.Util.IsArray(this.Textbox)) {
            for ( var i=this.Textbox.length-1; i>=0; --i ) {
                this.Textbox[i].Enable()
            }
        };
    } else {
        if (this.Textbox.Dom) this.Textbox.Disable();
        if (dsl.Util.IsArray(this.Textbox)) {
            for ( var i=this.Textbox.length-1; i>=0; --i ) {
                this.Textbox[i].Disable()
            }
        };
    }
};
dsl.DepencyRadio.prototype._addTextbox = function(textbox) {
    if (typeof textbox == "string") return new dsl.Textbox(textbox);
    if (dsl.Util.IsArray(textbox)) {
        var textboxes = [];
        for ( var i=textbox.length-1; i>=0; --i ) {
            textboxes.push( new dsl.Textbox( textbox[i] ) );
        }
        return textboxes
    }   return ""
};
dsl.DepencyRadio.prototype._init = function() {
    var me = this, event = "click",
        func = function(){ me.Radio.Select() };
    if (me.Textbox.Dom) dsl.Util.AddEvent({Dom:me.Textbox.Dom, Event:event, Action:func});
    if (dsl.Util.IsArray(me.Textbox)) {
        for ( var i=me.Textbox.length-1; i>=0; --i ) {
            dsl.Util.AddEvent({Dom:me.Textbox[i].Dom, Event:event, Action:func})
        }
    }
};
// DepencySet
dsl.DepencySet = function(radios) {
    this.Radios = [];
    this._init(radios);
};
dsl.DepencySet.prototype.GetValue = function(id) {
    for ( var i=this.Radios.length-1; i>=0; --i ) {
        var joiner = (this.Radios[i].paramKey) ? "=" : "";
        if (this.Radios[i].IsSelected()) return this.Radios[i].paramKey + joiner + this.Radios[i].GetValue();
    } return ""
};
dsl.DepencySet.prototype.IsValid = function() {
    for ( var i=this.Radios.length-1; i>=0; --i ) {
        if (this.Radios[i].IsSelected()) return this.Radios[i].IsValid();
    } return false
};
dsl.DepencySet.prototype.ToggleDependants = function() {
    for ( var i=this.Radios.length-1; i>=0; --i ) {
        this.Radios[i].ToggleDependants()
    }
};
dsl.DepencySet.prototype._init = function(radios) {
    for ( var i=radios.length-1; i>=0; --i ) {
        this._addRadio( radios[i] );
    }
    //this.ToggleDependants();
};
dsl.DepencySet.prototype._addRadio = function(args) {
    var radio = new dsl.DepencyRadio(args.radio,args.dependant,args.paramKey)
    this.Radios.push(radio);
};

// OnOffBox
dsl.ShowHide = function(id, initialState) {
    this.Dom = dsl.Util.FindDom(id);
    var _initState = ((initialState && initialState == false) || (this.Dom.style.display == 'none'))
        ? false : true;
    this._init(_initState);
};
dsl.ShowHide.prototype._init = function(display) {
    if (display) this._show();
    else this._hide();
};
dsl.ShowHide.prototype._show = function() {
    this.IsShown = true;
    this.Dom.style.display = 'block';
};
dsl.ShowHide.prototype._hide = function() {
    this.IsShown = false;
    this.Dom.style.display = 'none';
};
dsl.ShowHide.prototype.Show = function() {
    if (!this.IsShown) this._show()
};
dsl.ShowHide.prototype.Hide = function() {
    if (this.IsShown) this._hide()
};

//ajax
dsl.ajax = {
    createScriptTag: function(url) {
	var s = document.createElement("script");
	s.setAttribute("id", Math.random());
	s.setAttribute("language", "javascript");
	s.setAttribute("type", "text/javascript");
	s.setAttribute("src", url);
	return s;
    },
    head: document.getElementsByTagName("head")[0]
};

dsl.ajax.xhr = {
    createXhr: function(){
	return (XMLHttpRequest)
	? new XMLHttpRequest()
	: new ActiveXObject("MSXML.HTTP");
    },
    invoke: function(verb, url, onComplete, onError, postData, scope){
	var xhr = dsl.ajax.xhr.createXhr(),
	    pdata = postData || "",
	    scp = scope || this;
	    
	function complete() { onComplete.apply(scp, arguments); };
	function error() { onError.apply(scp, arguments); };
	function chkProgress() {
	    if(xhr.readystate < 4) return;
	    if(/(200)|(30.*)/.test(xhr.status)) complete();
	    else error();
	}
	xhr.open(verb, url, true);
	xhr.onreadystatechange = chkProgress;
	xhr.send(pdata);
    }
};

dsl.ajax.xss = {
    invoke: function(url, /*{}*/params, callback, error){
	 if (dsl.ajax.xss._cstag) dsl.ajax.head.removeChild(dsl.ajax.xss._cstag);
	var _url = url + "?callback=" + callback + "&error=" + error;
	for(var n in params){ _url += ("&" + n + "=" + encodeURI(params[n])); }
	dsl.ajax.xss._cstag = dsl.ajax.createScriptTag(_url);
	dsl.ajax.head.appendChild(dsl.ajax.xss._cstag);
    },
    _cstag:undefined
};

//Dom Content Loaded Event | User dsl.loaded.subscribe
dsl.loaded = {
    _funcs: [],
    subscribe: function(func){ dsl.loaded._funcs[dsl.loaded._funcs.length] = func; },
    invoke: function() { for(var n in dsl.loaded._funcs) dsl.loaded._funcs[n](); }
}

dsl.Util.AddEvent({Dom:document, Event:"DOMContentLoaded", Action:dsl.loaded.invoke});
document.onreadystatechange = function(){ if(document.readyState == "complete") dsl.loaded.invoke(); }

// ****************
//
// JavaScript Document
// show / hide ids
var browserType;
var fieldToHide;
var fieldToShow;
if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {browserType= "gecko"}
// hide an id 
function hideMe(fieldToHide) {
	if (browserType == "gecko" )
		document.poppedLayer =  eval('document.getElementById(fieldToHide)');
	else if (browserType == "ie")
		document.poppedLayer = eval('document.getElementById(fieldToHide)');
	else 
		document.poppedLayer =  eval('document.layers[fieldToHide]');
		document.poppedLayer.style.display = "none";
}
// show an id 
function showMe(fieldToShow) {
	if (browserType == "gecko" ) 
		document.poppedLayer = eval('document.getElementById(fieldToShow)');
	else if (browserType == "ie") 
		document.poppedLayer = eval('document.getElementById(fieldToShow)');
	else 
		document.poppedLayer = eval('document.layers[fieldToShow]');
		document.poppedLayer.style.display = "block";
}
// show and hide an id, with arrow image
function showHideIds(theId,theImage) {
	if (browserType == "gecko" ) 
		document.poppedLayer = eval('document.getElementById(theId)');
	else if (browserType == "ie") 
		document.poppedLayer = eval('document.getElementById(theId)');
	else 
		document.poppedLayer = eval('document.layers[theId]');
	
	if (document.poppedLayer.style.display == "none" || document.poppedLayer.style.display == ''){
		document.poppedLayer.style.display = "block";
		document.getElementById(theImage).src = "/imgs/arrow_down.gif";
	}
	else {
		document.poppedLayer.style.display = "none";
		document.getElementById(theImage).src = "/imgs/arrow_up.gif";
	}
}
/* show all instances of a class (with arrow) */
var allPageTags = new Array(); 
function showClasses(theClass,theImage) {
	var allPageTags=document.getElementsByTagName("*");
	for (i=0; i<allPageTags.length; i++) {
		if (allPageTags[i].className==theClass) {
			allPageTags[i].style.display = 'block';
		}
		if (allPageTags[i].className==theImage) {
			allPageTags[i].src = "/imgs/arrow_down.gif";
		}
	}
} 

/* hide all instances of a class (with arrow) : used on esign promissory  note page*/
var allPageTags = new Array(); 
function hideClasses(theClass,theImage) {
	var allPageTags=document.getElementsByTagName("*");
	for (i=0; i<allPageTags.length; i++) {
		if (allPageTags[i].className==theClass) {
			allPageTags[i].style.display = 'none';
		}
		if (allPageTags[i].className==theImage) {
			allPageTags[i].src = "/imgs/arrow_up.gif";
		}
	}
} 

// for opening tell-a-friend-pop-up.aspx from footer 
function openTellFriend() { 
	tellfriend = window.open("/CompareApply/TellAFriend.aspx",null,"height=560,width=690,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes");
	if (window.focus) {tellfriend.focus()}
	return false;
}
// read-comment-pop-up.aspx > this pop-up may not yet be called from the new marketing site. This may be for phase 2 .
function readComment() { 
	readcomment = window.open("/read-comment-pop-up.aspx",null,"height=450,width=600,status=no,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes");
	if (window.focus) {readcomment.focus()}
	return false;
}
// show and hide an id
function switchIds(idToHide,idToShow) {
	var inputs = document.getElementById("inputs")
	var results = document.getElementById("results")
		showMe("results");
		hideMe("inputs");
			return false;
}
// financial-aid-glossary.aspx
function hideTerms() {
	var a = document.getElementById("a")
	var b = document.getElementById("b")
	var c = document.getElementById("c")
	var d = document.getElementById("d")
	var e = document.getElementById("e")
	var f = document.getElementById("f")
	var g = document.getElementById("g")
	var i = document.getElementById("i")
	var l = document.getElementById("l")
	var m = document.getElementById("m")
	var n = document.getElementById("n")
	var o = document.getElementById("o")
	var p = document.getElementById("p")
	var r = document.getElementById("r")
	var s = document.getElementById("s")
	var t = document.getElementById("t")
	var u = document.getElementById("u")
	var v = document.getElementById("v")
	var w = document.getElementById("w")
		hideMe("a");
		hideMe("b");
		hideMe("c");
		hideMe("d");
		hideMe("e");
		hideMe("f");
		hideMe("g");
		hideMe("i");
		hideMe("l");
		hideMe("m");
		hideMe("n");
		hideMe("o");
		hideMe("p");
		hideMe("r");
		hideMe("s");
		hideMe("t");
		hideMe("u");
		hideMe("v");
		hideMe("w");
		return false;
}

// college planner calculator scripts
function returnNumber(sText, el){
	var ValidChars = "0123456789.";
	var newNum = "";
	var Char;

	for (i = 0; i < sText.length ; i++){ 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) != -1) 
		newNum += Char;
	}
	
	newNum = (newNum == "")?0:newNum;
	
	if(!el == null)el.value = newNum;
		
	return newNum;
}
function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function doCollegeCalc(){
	//get the numbers
	var c1 = parseInt(returnNumber(document.getElementById("a1").value));
	var c2 = parseInt(returnNumber(document.getElementById("a2").value));
	var c3 = parseInt(returnNumber(document.getElementById("a3").value));
	var c4 = parseInt(returnNumber(document.getElementById("a4").value));
	var f1 = parseInt(returnNumber(document.getElementById("d1").value));
	var f2 = parseInt(returnNumber(document.getElementById("d2").value));
	var f3 = parseInt(returnNumber(document.getElementById("d3").value));
	var f4 = parseInt(returnNumber(document.getElementById("d4").value));
	//format their inputs
	document.getElementById("a1").value = addCommas(returnNumber(document.getElementById("a1").value));
	document.getElementById("a2").value = addCommas(returnNumber(document.getElementById("a2").value));
	document.getElementById("a3").value = addCommas(returnNumber(document.getElementById("a3").value));
	document.getElementById("a4").value = addCommas(returnNumber(document.getElementById("a4").value));
	document.getElementById("d1").value = addCommas(returnNumber(document.getElementById("d1").value));
	document.getElementById("d2").value = addCommas(returnNumber(document.getElementById("d2").value));
	document.getElementById("d3").value = addCommas(returnNumber(document.getElementById("d3").value));
	document.getElementById("d4").value = addCommas(returnNumber(document.getElementById("d4").value));
	//do the calcs and display the results
	document.getElementById("t1").innerHTML = '$ ' + addCommas(c1+c2+c3+c4);
	document.getElementById("t2").innerHTML = '$ ' + addCommas(f1+f2+f3+f4);
	document.getElementById("t3").innerHTML = '$ ' + addCommas((c1+c2+c3+c4) - (f1+f2+f3+f4));
}
function check(turnMeOn){
  	document.getElementById("sel-1").checked=false;
  	document.getElementById("sel-2").checked=false; 
  	document.getElementById("sel-3").checked=false; 
  	document.getElementById("sel-4").checked=false;
  	document.getElementById("sel-5").checked=false;
  	document.getElementById("sel-6").checked=false;
  	document.getElementById("sel-7").checked=false;
  	document.getElementById("sel-8").checked=false;
  	document.getElementById("sel-9").checked=false; 
  	document.getElementById("sel-10").checked=false; 
  	document.getElementById("sel-11").checked=false;
  	document.getElementById("sel-12").checked=false;
  	document.getElementById("check-1").checked=false;
  	document.getElementById("check-2").checked=true;
	document.getElementById(turnMeOn).checked=true;/**/
  
}
function first_check(){
	document.getElementById("check-1").checked=false;
	document.getElementById("check-2").checked=true;
	hideMe('credit'); 
	showMe('credit_off'); 
}
function second_check(){
	document.getElementById("check-1").checked=false;
	document.getElementById("check-2").checked=false;
	showMe('credit'); 
	hideMe('credit_off'); 
}
function third_check(){
	var cosignerNote = document.getElementById("cosignerNote");
	document.getElementById("check-3").checked=false;
	document.getElementById("check-4").checked=true;
	hideMe('credit_three'); 
	showMe('credit_four'); 
	showMe("cosignerNote");
}
function fourth_check(){
	document.getElementById("check-3").checked=false;
	document.getElementById("check-4").checked=false;
	showMe('credit_three'); 
	hideMe('credit_four'); 
}
// compare-apply-student-loans.aspx > simulates user selections from home page, to be coded properly in aspx by nstar 
function ca_referrer() {
	if (
		(location.href == '/CompareApply/CompareApply.aspx' || location.href == '/CompareApply/CompareApply.aspx')
		&& 
		(document.referrer == '/home.aspx' || document.referrer == '/home.aspx')
	){
		showMe("educationType"); 
		showMe("school"); 
		showMe("select_loan_on"); 
		showMe("compare_loans");
	}
}
// compare-apply-student-loans.aspx > simulates user/ system sequencing, to be coded properly in aspx by nstar 
function compareButton() {
	hideMe('select_loan_on'); 
	hideMe('select_loan_staff'); 
	hideMe('select_loan_plus'); 
	hideMe('select_loan_priv');  
	hideMe('required_info');  
	hideMe('required_info_plus'); 
	hideMe('required_info_priv'); 
	hideMe('credit');  
	hideMe('credit_off'); 
	hideMe('credit_three'); 
	hideMe('credit_four'); 

	showMe('educationType'); 
	showMe('school'); 
	showMe('select_loan_on'); 
	showMe('compare_loans'); 
}
function staffRadio() {
	hideMe('select_loan_on'); 
	hideMe('select_loan_staff'); 
	hideMe('select_loan_plus'); 
	hideMe('select_loan_priv');  
	hideMe('required_info');  
	hideMe('required_info_plus'); 
	hideMe('required_info_priv'); 
	hideMe('credit');  
	hideMe('credit_off'); 
	hideMe('credit_three'); 
	hideMe('credit_four'); 
	
	showMe('select_loan_staff'); 
	showMe('required_info')
}
function plusRadio() {
	 hideMe('select_loan_on'); 
	 hideMe('select_loan_staff'); 
	 hideMe('select_loan_plus'); 
	 hideMe('select_loan_priv'); 
	 hideMe('required_info'); 
	 hideMe('required_info_plus'); 
	 hideMe('required_info_priv'); 
	 hideMe('credit');  
	 hideMe('credit_off'); 
	 hideMe('credit_three'); 
	 hideMe('credit_four'); 
	 
	 showMe('select_loan_plus');  
	 showMe('required_info_plus');
	 showMe('credit');	
}
function privRadio() {
	hideMe('select_loan_on'); 
	hideMe('select_loan_staff'); 
	hideMe('select_loan_plus'); 
	hideMe('select_loan_priv');  
	hideMe('required_info');  
	hideMe('required_info_plus'); 
	hideMe('required_info_priv'); 
	hideMe('credit');  
	hideMe('credit_off'); 
	hideMe('credit_three'); 
	hideMe('credit_four'); 

	showMe('select_loan_priv'); 
	showMe('required_info_priv'); 
	showMe('credit_three');
}

function detailed() {
	hideMe('compare_loans_short'); 
	showMe('compare_loans');
}
function summary() {
	hideMe('compare_loans'); 
	showMe('compare_loans_short');
}

