/*-----------------------------------/
|	
	Name: site.js
	Author: AcroMedia Inc. (c) 2007 All Right Reserved. 
	Purpose:  JS Functions for AFCI
|  
+-----------------------------------*/

function copyrightYear() {
	if(document.getElementById('insertYear')) document.getElementById('insertYear').innerHTML = new Date().getFullYear();
}

function displayAddress() {
	if(document.getElementById('insertAddress')) document.getElementById('insertAddress').innerHTML = "2110 Artesia Blvd., Suite 234, Redondo Beach, CA 90278 – Ph: 1-323-461-2324, 1-877-722-2324 - <a href=\"mailto:info@afci.org\">info@afci.org</a>"
}

siteOnLoad = function (){
	copyrightYear();
	displayAddress();
}

function addLoadEvent(func){
	var oldOnLoad = window.onload;
	if(typeof window.onload != 'function'){ 
		window.onload = func;
	}
	else{ 
		window.onload = function(){ 
			oldOnLoad();
			func();
		}
	}
}

/* checkQuiz
***************
checks to make sure the quiz has been completed before being submitted
*/
function checkQuiz(){
	var formArray = document.getElementsByTagName('input');
	var errorField = document.getElementById('errorDiv');
	var htmlLink = document.getElementById('submitLink');
	var ulCount = document.getElementById('ulCount');
	var formID = document.getElementById('exam_01');
	var errorArray = new Array();
	var colorArray = new Array();
	var count = 0;
	var errors = false;
	var lastName = "";
	var questionNumber = 1;
	var cgiStr = "";
	var questionChecked = 0;
	var questionNumber = 0;
	
	errorField.innerHTML = "";	
	
	//for all the UL's
	for(var a=0; a < ulCount.value; a++){ //loop throught all UL's on page
		var navRoot = document.getElementById("ul" + a); 
		highlightExamErrors(false, navRoot.id, false); //unhighlight all errors
		questionNumber++;
		for (var b=0; b < navRoot.childNodes.length; b++) { // get the UL's childs
			var questionCount = document.getElementById("questionCount" + a); //get the count of LI's for the UL
			var node = navRoot.childNodes[b];	
			if (node.nodeName == "LI") {
				for (var c=0; c < node.childNodes.length; c++){ 
					var subNode = node.childNodes[c];
					if((subNode.nodeName == "INPUT") && (subNode.type == "radio")){
						//alert(subNode.checked);
						if(!subNode.checked){ //if its false, increment a count
							//alert("b: " + questionChecked);
							questionChecked++;
							//alert("a: " + questionChecked);
							if(questionChecked == questionCount.value){ //if count equals the multiple choice questions for the given UL, none are checked, store error
								//alert(questionChecked + " " + questionCount.value);	
								errorArray[count] = "You must select an answer for question " + (questionNumber);
								colorArray[count] = navRoot.id;	
								errors = true;
								count++;
							}
						}
					}
				}			   
			}
		}
		questionChecked = 0;
	}

	//for all the TextArea's
	for(var d=0; d < formID.childNodes.length; d++){
		var textArea = formID.childNodes[d];
		if((textArea.nodeName == "TEXTAREA") && (textArea.type == "textarea")){
			questionNumber++;
			highlightExamErrors(false, textArea.id, true); //unhighlight all errors
			if(textArea.textLength <= 0){
				errorArray[count] = "You must provide an answer for question " + (questionNumber);
				colorArray[count] = textArea.id;	
				errors = true;
				count++;
			}
		}
	}
	
	
	if(errors == true){	
		htmlLink.href = "#top";
		errorField.innerHTML = "";
		errorField.style.display = "block";
		for(var a=0;a<errorArray.length;a++){
			errorField.innerHTML += (a+1) + ". " + errorArray[a] + ".<br/>";
			highlightExamErrors(true, colorArray[a], false);
		}
		//errorField.innerHTML += "<br/>";
		// Scroll to the top of the page to show errors
		scroll(0,0);
		resizePage();

		return false;
	}
	else {		
		//do something with the information...
		//form submits to .php file which puts userinfo and redirects to thank you page?
		document.forms.exam_01.submit();
		return true;
	}		
	
	
	return false;
}

/* checkInfo
****************
checks the to make sure the required info on the forum has been inputted.
*/

function checkInfo(){
	var formArray = document.getElementsByTagName('input');
	var errorField = document.getElementById('errorDiv');
	var htmlLink = document.getElementById('submitLink');
	var afciMember = document.getElementById('memberField');
	var commission = document.getElementById('commission');
	var company = document.getElementById('company');
	var stateProvince = document.getElementById('stateProvince');
	var country = document.getElementById('country');	
	var errorArray = new Array();
	var colorArray = new Array();
	var count = 0;
	var errors = false;
	errorField.innerHTML = "";

	if(afciMember.value == ""){
		errorArray[count] = "<strong>You must select if you are an existing AFCI Member</strong>";
		colorArray[count] = afciMember.id;
		count++;
		errors = true;	
	}
	else{
		afciValue = afciMember.value;
		highlightError(false, afciMember.id);
	}

	if(!errors){
		if((afciValue == "yes") && (commission.value == "")){
			errorArray[count] = "<strong>Commision</strong> cannot be blank";
			colorArray[count] = commission.id;
			count++;
			errors = true;	
		}
	
		if((afciValue == "no") && (company.value == "")){
			errorArray[count] = "<strong>Company</strong> cannot be blank";
			colorArray[count] = company.id;
			count++;
			errors = true;		
		}
	}
	
	for(var i=0;i<formArray.length;i++){			
		if((formArray[i].type == "text") || (formArray[i].type == "select") || (formArray[i].type == "password")){
			// Unhighlight all the inputs
			if(formArray[i].className == 'error' && formArray[i].value != '') {
				highlightError(false, formArray[i].id);
			}			
			
			if(afciMember.value == "no"){
				if((formArray[i].value == "") && (formArray[i].title != "")){
					errorArray[count] = "<strong>" + formArray[i].title + "</strong> cannot be blank";
					colorArray[count] = formArray[i].id;
					count++;
					errors = true;				
				}
			}								
			else{
				if((formArray[i].value == "") && (formArray[i].title != "") && (formArray[i].alt == '0')){
					errorArray[count] = "<strong>" + formArray[i].title + "</strong> cannot be blank";
					colorArray[count] = formArray[i].id;
					count++;
					errors = true;				
				}
			}
		}
	}
 	
	
	if(afciMember.value != "yes"){
		if((stateProvince.value == "Unspecified") || (stateProvince.value == "")){
			errorArray[count] = "You must select a <strong>state/province</strong>";
			colorArray[count] = stateProvince.id;
			count++;
			errors = true;	
		}
		else{
			highlightError(false, stateProvince.id);
		}
	
		if((country.value == "Unspecified") || (country.value == "")){
			errorArray[count] = "You must select a <strong>country</strong>";
			colorArray[count] = country.id;
			count++;
			errors = true;	
		}
		else{
			highlightError(false, country.id);
		}
	}
		
	if(errors == true){	
		htmlLink.href = "#top";
		errorField.innerHTML = "";
		errorField.style.display = "block";
		for(var a=0;a<errorArray.length;a++){
			errorField.innerHTML += (a+1) + ". " + errorArray[a] + ".<br/>";
			highlightError(true, colorArray[a]);
		}
		//errorField.innerHTML += "<br/>";
		// Scroll to the top of the page to show errors
		scroll(0,0);
		resizePage();

		return false;
	}
	else {		
		//do something with the information...
		//form submits to .php file which puts userinfo and redirects to thank you page?
		document.forms.members_form.submit();
		return true;
	}	
	
}

/*highlightError
************
*/
function highlightError(highlight, errorID){
	var tempID = document.getElementById(errorID);
	
	if(highlight) {
		tempID.className = 'error';		
	} else {	
		tempID.className = '';
	}
}

/*resize 
************
*/
function resizePage(){
	var timerID = 0;
	
	document.getElementById('siteHTML').style.height = "auto";
	document.getElementById('siteBODY').style.height = "auto";
	timerID = setTimeout("resetSizePage()", 1);
}

/* resetSizes
*************
*/
function resetSizePage () { //Solves FireFox Page Rendering Issue With Changing Heights
	clearTimeout (timerID);
	document.getElementById('siteHTML').style.height = "100%";
	document.getElementById('siteBODY').style.height = "100%";

}

/*highlightError
************
*/
function highlightExamErrors(highlight, errorID, isTextArea){
	var tempID = document.getElementById(errorID);
	
	if(highlight) {
		tempID.className += ' questionError';		
	} else {	
		if(!isTextArea){
			tempID.className = 'radioList';
		}
		else{
			tempID.className = '';		
		}
	}
}

function showComm() {
	var hideTable = document.getElementById('hideInfo');
	var submitButton = document.getElementById('submitLink');
	document.getElementById('siteHTML').style.height = "auto";
	document.getElementById('siteBODY').style.height = "auto";

	var divComm = document.getElementById("commField");
	var divComp = document.getElementById("compField");
	var mem = document.getElementById("memberField");

	if (mem.options[mem.selectedIndex].value == "yes") {
		divComp.style.display = "none";
			try {
				divComm.style.display = "table-row";
				hideTable.style.display = "none";
			}
			catch(e) {
				divComm.style.display = "block";
				hideTable.style.display = "none";
				
			}
		timerID = setTimeout("resetSizes()", 0);
		}
	else if (mem.options[mem.selectedIndex].value == "no") {
		divComm.style.display = "none";
			try {
				divComp.style.display = "table-row";
				hideTable.style.display = "table-row";
			}
			catch(e) {
				divComp.style.display = "block";
				hideTable.style.display = "block";	
			}
		timerID = setTimeout("resetSizes()", 0);
		}	
}
	
function setCookie(name, value, expires, path, domain, secure){
	var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	document.cookie = curCookie;
}	

function deleteMemberCookie(){
		var timeNow = new Date() - 3600;
		setCookie('sb_memberID', '', 0, "/", "");
}

addLoadEvent(siteOnLoad);
