

function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function show(obj) {
	var el = document.getElementById(obj);
	el.style.display = '';
}

function hide(obj) {
	var el = document.getElementById(obj);
	el.style.display = 'none';
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function getAge() {
	
	var now = new Date();
	var year_now = now.getFullYear();
	var month_now = now.getMonth() + 1;
	var day_now = now.getDate();
	
	var month = document.getElementById('birthDateMonth').value;
	var day = document.getElementById('birthDateDay').value;
	var year = document.getElementById('birthDateYear').value;
	
	var age = (year_now - year);
	
		if ((month_now < month) || (month_now == month && day_now < day)) {
		    $age--;
		}
		
	return age; 
}

function dobUpdated() {
	
	var age = getAge();
	
	//alert(age);
	
	/*if(age > 16 && age < 20) {
		
		show('ashworth_main');
		show('ashworth_main_1');
		
	}
	else {
		
		hide('ashworth_main');
		hide('ashworth_main_1');
	}*/
}

//form particulars - JF
	function toggleArea(area, opt) {
		var id = document.getElementById(area);
		
		if (id === null) return;
		
		if (opt === null) {
			if (id.style.display != 'none') {
				id.style.display = 'none';
			} else {
				id.style.display = 'block';
			}
			return;
		}
		
		if (opt == 'Yes') {
			id.style.display = 'inline';		
		} else {
			id.style.display = 'none';				
		}	
	}
	
	function toggleScholarship(opt) {
		for (var i = 1; i < 9; i++) {
			toggleArea('q' + i, opt);
		}
		
/*		toggleArea('q1', opt);
		toggleArea('q2', opt);
		toggleArea('q3', opt);
		toggleArea('q4', opt);
		toggleArea('q5', opt);
		toggleArea('q6', opt);
		toggleArea('q7', opt);
		toggleArea('q8', opt);*/
	
	}
	
	function checkQuestions() {
		var val = document.getElementById('interest');
		var ids = new Array("itt","itt2","p1", "p2", "feg1", "feg2");
		
		if (val.value == 'no') {
			for(var i = 0; i < ids.length; i++) {
				var field = document.getElementById(ids[i]);
				if (field === null) continue;				
				field.style.display = 'inline';
			}
			toggleScholarship('No');
			toggleArea('scholarship', 'No');							
		} else {
			for(var i = 0; i < ids.length; i++) {
				var field = document.getElementById(ids[i]);
				if (field === null) continue;				
				field.style.display = 'none';
			}
			toggleScholarship('No');
			toggleArea('scholarship', 'Yes');									
		}
	}

	function tooltip(opt) {
		if (opt == 'on') {
			document.getElementById('tooltip').style.display = 'block';
		} else {
			document.getElementById('tooltip').style.display = 'none';
		}
	}
	function countWords(area, counter){
		var i = 0;
		var words = 1;
	
		while(i <= area.length) {
			if (area.substring(i, i+1) == " ") {
				words++;
				i++; 
			}
			if (area.substring(i, i+1) == "\n") {
				words++;
				i++;
			}
			i++;
		}
		document.getElementById(counter).innerHTML = words;
	}	

	function validateScholarship() {
		var interest = document.getElementById('interest');
		
		if (interest === null) {
			return;
		}
		var gpa = document.getElementById('scholarship_applications_understand_gpa_TRUE');
		var agree = document.getElementById('scholarship_applications_agree_terms_TRUE');
		var error = "";
				
		if (interest.value != 'no') {
			if (!gpa.checked) {
				error += 'You must agree to our GPA requirements.\n';
			}
			if (!agree.checked) {
				error += 'You must agree to our terms and policy to apply.\n';
			}
			if (error != "") {
				alert(error);
				return false
			}					
		}
	}
	
	function validatePCDI() {
		var pcdi_fields = new Array("source", "target", "quest4", "quest5", "quest6");
		var opt_in_obj = document.getElementById('pcdi_optin');
		
		if (opt_in_obj === null) {
			return;
		}
				
		var opt_in = getCheckedValue(opt_in_obj);

		if (opt_in == 'Yes') {
			for( var f = 0; f < pcdi_fields.length; f++) {
				if (document.getElementById(pcdi_fields[f]).value == "") {
					alert("You've selected that you would like more information about PCDI Education offers.  Please ensure all fields are complete.");
					return false;
				}
			}
			return true;
		} else {
			return true;
		}

	}
		
	function validateFields() {
		var state = document.getElementById('state');
		var error = "";
		if (state.value == "") {
			error += 'You must select a state.\n';
		}
		if (document.getElementById('birthDateMonth').value == "") {
			error += 'You must select a birthday month.\n';
		}
		if (document.getElementById('birthDateDay').value == "") {
			error += 'You must select a birthday day.\n';
		} 
		if (document.getElementById('birthDateYear').value == "") {
			error += 'You must select a birthday year.\n';
		}
		
		if (error != "") {
			alert(error);
			return false
		}
	}
	function checkSuccess() {				
		if (   validateFields() != false 
			&& document.MM_returnValue != false 
			&& validateScholarship() != false
			&& validatePCDI() != false ) {
			return true;
		} else {
			return false;	
		}
	}

	function noAcceptWarning() {
		alert("You must select \"Yes\" to receive a username and password and sign up for the course.");	
	}

	function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}
	
	function MM_validateForm() { //v4.0
	  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
	    if (val) { nm=val.name; if ((val=val.value)!="") {
	      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
	        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
	      } else if (test!='R') { num = parseFloat(val);
	        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
	        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
	          min=test.substring(8,p); max=test.substring(p+1);
	          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
	    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	  } if (errors) alert('The following error(s) occurred:\n'+errors);
	  	document.MM_returnValue = (errors == '');
	}	
	
	function popUp(URL) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=1,resizable=0,width=800,height=600,left = 320,top = 150');");
	}



                                            

