function popLegalWindow(URL) {
	var now = new Date();
	windowName = "dh_" + now.getSeconds();
	window.open('/legal/'+URL+'/','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=585,height=450');
}

/* Grid: update and save grid settings
 ---------------------------------- */
var Grid = {

	toggleSortCol:function(url,key,direction){
		//alert(params)
		var params = 'colSort=' + key + '&colSortDir=' + direction;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	},
	
	togglePage:function(url, pageNum){
		var params = 'pageNum=' + pageNum;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	
	},
		
	toggleNumRows:function(url, numRows){
		var params = 'numRows=' + numRows;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	}, 
	
	togglePageNum:function(url, pageNum) {
		var params = 'pageNum=' + pageNum;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	}, 
	
	setFilterString:function(url, searchTerms) {
		//alert(url);
		var params = 'searchTerms=' + searchTerms;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	},
	
	setStatusString:function(url, status) {
		var params = 'leadDisp=' + status;
		var target = 'grid';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true
		});	
	}
}

pic1=new Image(100,25);
pic1.src="/images/layout/rednext_gray.gif";

var Question = {
	
	validateExclusive:function(max){
		
		var proceed;
		
		proceed = this.getCheckBoxAnswer2(0,'questiondiv0','10');
		
		if (!proceed)
			return false;
			
		for(var i = 1; i < max; i++)
		{	
			var thisquestion = $('question' + (i));
			var thislabel = $('label' + (i)).innerHTML;

			if (thisquestion == null )
				continue ;
			
			if (thisquestion.value == '') {
				alert('Please answer question: "' + thislabel.replace(/[\?:]*/g, '') + '"');
				thisquestion.focus();
				return false;
			}
		}
		
		return this.checkZipCode('question8');
		
		return true;
	},
	
	getAnswer:function(i, thisquestion,max){
		if(thisquestion==undefined){
			var thisquestion=$('question'+(i));
		}
		if(thisquestion.value=='')
			alert('Please select an answer for this question')
		else{
			if(this.passQualifier(thisquestion.value, thisquestion.name))
				this.next(i,max);
		}
	},
	getText:function(i, thisquestion, max){
		if(thisquestion==undefined){
			var thisquestion=$('question'+(i));
		}
		if(this.isZipCode(thisquestion.value)){
			this.next(i,max);
		}
	},
	getCheckBoxAnswer:function(i, thischeckboxholder,max){
		var container=$(thischeckboxholder+'_1');
		var checked=0;
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					if(child.checked) checked++;
				}
			}
		}
		var container=$(thischeckboxholder+'_2');//check other one
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					if(child.checked) checked++;
				}
			}
		}
		if(checked==0){
			alert("Please select at least one option");
		}
		else{
			this.next(i,max);
		}
	},
	
	getCheckBoxAnswer2:function(i, thischeckboxholder,max){
		var container=$(thischeckboxholder+'_1');
		var checked=0;
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					if(child.checked) checked++;
				}
			}
		}
		var container=$(thischeckboxholder+'_2');//check other one
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					if(child.checked) checked++;
				}
			}
		}
		
		var container=$(thischeckboxholder+'_3');//check other one
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					if(child.checked) checked++;
				}
			}
		}
		if(checked==0){
			alert("Please select at least one area of interest.");
			return false;
		}
		
		return true;
	},
	getRadioAnswer:function(i ,max){

		var container=$('questiondiv'+(i));
		var val='';
		if(container)
			for(var child = container.firstChild; child; child = child.nextSibling) 
				if(child.value!=undefined)
					if(child.checked)
						val=child.value;
				
			
		
		if(this.passQualifier(val, 'citizenquestion'))
			this.next(i,max);
	},
	passQualifier:function(val,name){
		var busted=0;


		if(val==114){//what year did you graduate highschool AND 2009 and after
			window.location=this.ageurl;
			busted=1
		}
		if(val==16){//what current degree do you have AND do not have / not pursuing degree
			window.location=this.degreeurl;
			busted=1
		}
		if(val==108){ //are you a current us citizen AND no
			window.location=this.citizenurl;
			busted=1
		}
		if(busted==1){
			//alert("you are busted TODO forward user somewhere here")
			return false;
		}
		else
			return true;
	},
	previous:function(i,max){
		$('hiderdiv'+(i)).hide();
		$('hiderdiv'+(i-1)).show();
		$('next'+(i-1)).show();
		this.showBar(i-1,max);
	},
	next:function(i,max){
		$('hiderdiv'+(i)).hide();
		$('hiderdiv'+(i+1)).show();
		$('next'+(i)).hide();
		this.showBar(i+1,max);
	},
	
	showBar:function(i,max){
		var maxwidth=250;max--;
		$('statusbarinside').style.width=(maxwidth*i/max)+"px";
	},
	
	checkAll:function(id, thisbox){
		var container=$(id);
		for(var child = container.firstChild; child; child = child.nextSibling) {
			if(child.id!=undefined){
				if(child.id.substring(0,6)=='answer'){
					child.checked=thisbox.checked;
				}
			}
		}

	},
	
	checkZipCode:function(id){
		var ret = this.isZipCode($(id).value);
		if(ret){
			$('submitbutton').disabled=true;
			$('submitbutton').src='/images/layout/rednext_gray.gif';
		}
		return ret;
	},
	
	//this function only checks for us zip code, add functionality for other formats if you want to support other countries
	isZipCode:function(field){
		var valid = "0123456789-";
		var hyphencount = 0;
		if (field.length!=5 && field.length!=10) {
			alert("Please enter your 5 digit or 5 digit+4 zip code.");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in your zip code.  Please try again.");
				return false;
			}
			if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
				alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
				return false;
		   }
		}
		return true;
	},
	restart:function(){}
}




var Specific = {
	
	getSubprograms:function(select, pid){
		
		// if Kaplan then toggle nursing level of edu
		if(pid==9) {
			if(select.value == '1413'){ 
				$('question5').replace('<select name="data[5]" id="question5"><option value="">Select</option><option value="16">Still in high school</option><option value="17">GED</option><option value="17">High school diploma</option><option value="18">Some college (fewer than two years)</option><option value="20">Associate\'s degree</option><option value="21">Bachelor\'s degree</option><option value="22">Master\'s degree</option><option value="1414">Doctoral degree</option><option value="1414">Post-graduate study</option><option value="626">Nursing Diploma</option><option value="16">None of the above</option></select>');
			} else {
				$('question5').replace('<select name="data[5]" id="question5"><option value="">Select</option><option value="16">Still in high school</option><option value="17">GED</option><option value="17">High school diploma</option><option value="18">Some college (fewer than two years)</option><option value="20">Associate\'s degree</option><option value="21">Bachelor\'s degree</option><option value="22">Master\'s degree</option><option value="1414">Doctoral degree</option><option value="1414">Post-graduate study</option><option value="16">None of the above</option></select>');
			}
		}
		
		// load program optoins for area of interest
		if(select.value != '') {
			var url = '/apply/specific/ajax/';
			var params = 'pid=' + pid +'&spid='+select.value;
			var target = 'subprograms';
			$('submitbutton').disabled=true;
			$('submitbutton').src='/images/layout/rednext_gray.gif';
			
			var myAjax = new Ajax.Updater(target, url, {
				method: 'get',
				parameters: params,
				evalScripts: true,
				onSuccess: function(){
					$('submitbutton').disabled=false;
					$('submitbutton').src='/images/layout/rednext.gif';
					$('subprogramquestions').innerHTML='';
				}
			});
		}		
	}

}

var PrivateLabel = {
	
	getSubprograms:function(element, provider_id){
		
		// tell user that its loading
		$('subprogram_id').replace('<select id="subprogram_id" name="subprogram_id" ><option value="">Loading...</option></select>');
		$('subprogramquestions').innerHTML = '';
			
		// if Kaplan then toggle nursing level of edu
		if(provider_id == 9) {
			if(element.value == '1413'){ 
				$('question5').replace('<select name="data[5]" id="question5"><option value="">Select</option><option value="16">Still in high school</option><option value="17">GED</option><option value="17">High school diploma</option><option value="18">Some college (fewer than two years)</option><option value="20">Associate\'s degree</option><option value="21">Bachelor\'s degree</option><option value="22">Master\'s degree</option><option value="1414">Doctoral degree</option><option value="1414">Post-graduate study</option><option value="626">Nursing Diploma</option><option value="16">None of the above</option></select>');
			} else {
				$('question5').replace('<select name="data[5]" id="question5"><option value="">Select</option><option value="16">Still in high school</option><option value="17">GED</option><option value="17">High school diploma</option><option value="18">Some college (fewer than two years)</option><option value="20">Associate\'s degree</option><option value="21">Bachelor\'s degree</option><option value="22">Master\'s degree</option><option value="1414">Doctoral degree</option><option value="1414">Post-graduate study</option><option value="16">None of the above</option></select>');
			}
		}
		
		// load program optoins for area of interest
		if(element.value != '') {
			var url = '/apply/privatelabel/subprograms/';
			var params = 'provider_id=' + provider_id + '&program_id=' + element.value;
			var target = 'subprograms';
			var myAjax = new Ajax.Updater(target, url, {
				method: 'get',
				parameters: params,
				evalScripts: true
			});
		}		
	},
	
	// for use with private label
	getSupplemental:function(element) {
		var url = '/apply/privatelabel/supplemental/';
		var params = 'subprogram_id='+element.value;
		var target = 'subprogramquestions';
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true,
			onSuccess: function(){
				$('submitbutton').disabled = false;
			}
		});	
	}
	
}

var Information = {
	
	getQuestions:function(select, c){
		
		//if(select.value==''){
			//selectedsubprograms[c] = 0;
			//alert('Please select an answer for this question')
		//}
		
		//selectedsubprograms[c]=select.value
		var url = '/apply/exclusive/ajax/';
		var params = 'id=' + select.value +'&qid='+c;
		//var params = 'id=669&qid='+c;
		//alert(c+" "+select.value);
		var target = 'subprogramquestions'+c;
		$('submitbutton').disabled=true;
		//$('submitbutton').src='/images/layout/rednext_gray.gif';
		
		var myAjax = new Ajax.Updater(target, url, {
			method: 'get',
			parameters: params,
			evalScripts: true,
			onSuccess: function(){$('submitbutton').disabled=false;
			
			}
		});
		//$('submitbutton').src='/images/layout/rednext.gif';
	},
	
	validateForm:function(form){
	
		// check first name		
		if (!this.isMinLength('fname', 2)) {
			alert("First Name needs to be at least 2 characters.");
			return false;
		}
		if (!this.isValidRegex('fname', "^[0-9a-zA-Z .-]+$")) {
			alert("First Name cannot contain any special characters.");
			return false;						
		}
		
		// check last name			
		if (!this.isMinLength('lname', 2)) {
			alert("Last Name needs to be at least 2 characters");
			return false;
		}
		if (!this.isValidRegex('lname', "^[0-9a-zA-Z .-]+$")) {
			alert("Last Name cannot contain any special characters.");
			return false;						
		}
				
		// check to make sure firstName is not equal to the last name
		if (this.isEqual('fname', 'lname')) {
			alert("First Name cannot be the same as Last Name.");
			return false;
		}
		
		// check streeet address		
		if(this.isNull('addr', 'Street Address is a required field'))      
			return false;
		if (!this.isValidRegex('addr', "^[0-9a-zA-Z .-]+$")) {
			alert("Street Address cannot contain any special characters.");
			return false;						
		}
		
		// check city
		if(this.isNull('city', 'City is a required field'))
			return false;
		if (!this.isValidRegex('city', "^[a-zA-Z .-]+$")) {
			alert("City cannot contain any special characters or numbers.");
			return false;						
		}
		
		// check state
		if(this.isNull('state', 'State is a required field'))         
			return false;
		
		// check zip
		if (!this.isValidRegex('zip', "^[0-9]{5}$")) {
			alert("Invalid Zip Code");
			return false;
		}
		
		// check email address
		if (!this.isValidRegex('email', /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i)) {
			alert("Invalid Email Address");
			return false;
		}
		
		// check phone number
		if(!this.isNumber('phone1',3))            
			return false;
		
		if(!this.isNumber('phone2',3))            
			return false;
			
		if(!this.isNumber('phone3',4))            
			return false;

		//if(!this.isChecked('agreetoform','You must consent to be contacted in order to submit your information'))            
		//	return false;

		//if(this.isNull('country', 'Country'))return false;
		//if(this.isNull('phone', 'Day Phone'))return false;
		//if(this.isNull('phone2', 'Evening Phone'))return false;
		//we don't care about the evening phone
		//if(!this.isNumber('phone21',3)) return false;
		//if(!this.isNumber('phone22',3)) return false;
		//if(!this.isNumber('phone23',4)) return false;
		
		$('submitbutton').disabled = true;
	
		return true;
	},
	
	isMinLength: function(id, minValue){
		element = $(id);
		text = element.getValue();
		
		if(text.length >= minValue){
			return true;
		}
		else{
			$(id).focus();
			return false;
		}
	},	
	
	isValidRegex: function(id, regexString){
		element = $(id);
		textField = element.getValue();
		var reg = new RegExp(regexString);
		
		if(textField != '' && reg.test(textField)){
			return true;
		} else {
			$(id).focus();
			return false;
		}
	},	
	
	isMaxLength: function(id, maxValue){
		element = $(id);
		text = element.getValue();
		
		if(text.length <= maxValue){
			$(id).focus();
			return true;
		}
		else{
			return false;
		}
	},
	
	isSelected: function(id, errormessage){
		text = $(id).selectedIndex;
		if(text > 0){
			return true;
		} else{
			alert(errormessage); // +" is empty, please fill it in"
			return false;
		}
	},
	
	isEqual: function(id, id2){
		element = $(id);
		text = element.getValue();

		element2 = $(id2);
		text2 = element2.getValue();
				
		if(text == text2){
			$(id).focus();
			return true;
		}
		else{
			return false;
		}
	},
	
	isNull:function(id, errormessage){
		element = $(id);
		text = element.getValue();
		if(text==''){
			alert(errormessage); // +" is empty, please fill it in"
			$(id).focus();
			return true;
		}
		else{
			return false;
		}
	},
	
	isChecked:function(id, errormessage){
		element = $(id);
		if(element.checked){
			return true;
		}
		else{
			alert(errormessage); // +" is empty, please fill it in"
			$(id).focus();
			return false;
		}
	},
	
	isInt:function(x) {
		var y=parseInt(x);
		if (isNaN(y)) return false;
 		return x==y && x.toString()==y.toString();
 	},
	
	isNumber:function(id, length){
		
		message = 'Please enter a valid phone number starting with area code.';
		for (i=0; i<$(id).value.length; i++){
			if(!this.isInt($(id).value.charAt(i))){
				alert(message);
				$(id).focus();
				return false;
			}
		}
		
		if($(id).value.length!=length){
			alert(message);
			$(id).focus();
			return false;
		}
		
		//Bug#1572 Check to ensure area code field doesn't begin with a 0 or 1
		if(id=='phone1' && ($(id).value.charAt(0)==0 || $(id).value.charAt(0)==1)){
			alert(message);
			$(id).focus();
			return false;
		}
		
		return true;
	},
	
	copyPhone:function(){
		$('phone21').value=$('phone1').value
		$('phone22').value=$('phone2').value
		$('phone23').value=$('phone3').value
	},
	
	moveCell:function(current, count, id){
		return;
		//this function prevented users from shift-tabbing back cells, so we just remove the autotab functionality, so sad
		//if(current.value.length==count){
		//	$(id).focus();
		//}
	},
	
	checkSup:function(form){

		if($('agreetoform') && !$('agreetoform').checked){
			alert("Please agree to the terms and conditions")
			$('submitbutton').disabled = false;
			return false;
		}
		

		for(var x=0; x<form.length; x++) {
	    	
			name = form.elements[x].name;
			
			if (name) 
			{				
				if (name.substring(0, 10) == 'subprogram') {
					if (form.elements[x].value == '') {//no answer provided
						alert('Please select a concentration');
						form.elements[x].focus();
						$('submitbutton').disabled = false;
						//$('submitbutton').src='/images/layout/rednext.gif';
						return false;
					}
				}
				else 
					if (name.substring(0, 3) == 'sup' || name.substring(0, 4) == 'data') {
						
						if (name == 'sup[dob_ds]') {
							if (!isDate(form.elements[x].value)) {
								$('submitbutton').disabled = false;
								//$('submitbutton').src='/images/layout/rednext.gif';
								return false;
							}
						}
						else if (name == 'sup[dob]') {
							if (!isValidDate(form.elements[x].value)) {
								alert('Please enter a valid Date of Birth');
								form.elements[x].focus();
								$('submitbutton').disabled = false;
								return false;
							}
						} else if (form.elements[x].value == '') { //no answer provided
							errorid = form.elements[x].name + "_";
							errormsg = '';
							if ($(errorid)) 
								errormsg = $(errorid).innerHTML;
							alert('Please select: "' + errormsg + '"');
							form.elements[x].focus();
							$('submitbutton').disabled = false;
							//$('submitbutton').src='/images/layout/rednext.gif';
							return false;
						} 									
				
					//alert(form.elements[x].value);
					}
					else 
						if (name.substring(0, 3) == 'add') {
							if (form.elements[x].value == '') {//no answer provided
								
								if (form.elements[x].previousSibling.className == 'label')
								{
									var label = form.elements[x].previousSibling.innerHTML;
									alert('Please select: "' + label.replace(/[\?:]*/g, '') + '"');
								}
								else
									alert('Please provide an answer to this question');
								
								form.elements[x].focus();
								$('submitbutton').disabled = false;
								//$('submitbutton').src='/images/layout/rednext.gif';
								return false;
							}
						//alert(form.elements[x].value);
						}
			}
			
			
		}
		
		return true;	
	}
}


var Kaplan = {
	//var selectedsubprograms=new Array(),
	showMilitaryFunding:function(select){
		if(select.value=='Yes'){
			$('militaryfunding').show();
		}
		else
			$('militaryfunding').hide();
	}
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 isDate(dtStr){
	
	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.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")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}

function isValidDate(dtStr) {
	var re = /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/

  	if (re.test(dtStr))
    	return true;

    return false;
}
	