
/* global document.getElementById function */
function $( id ) {
	if(document.getElementById(id)) {
		var obj = document.getElementById(id);
		return obj;
	}// else id not found
}



/* form and error functions */
function getRdoGroupsArray() {
	// set up radio groups array
	var rdoGroups = new Array();
	rdoGroups[0] = document.cnnElexBadgeFrm.rdoElex00;
	rdoGroups[1] = document.cnnElexBadgeFrm.rdoElex01;
	rdoGroups[2] = document.cnnElexBadgeFrm.rdoElex02;
	rdoGroups[3] = document.cnnElexBadgeFrm.rdoElex03;
	rdoGroups[4] = document.cnnElexBadgeFrm.rdoElex04;
	rdoGroups[5] = document.cnnElexBadgeFrm.rdoElex05;
	rdoGroups[6] = document.cnnElexBadgeFrm.rdoElex06;
	rdoGroups[7] = document.cnnElexBadgeFrm.rdoElex07;
	rdoGroups[8] = document.cnnElexBadgeFrm.rdoElex08;
	rdoGroups[9] = document.cnnElexBadgeFrm.rdoElex09;
	rdoGroups[10] = document.cnnElexBadgeFrm.rdoElex10;
	rdoGroups[11] = document.cnnElexBadgeFrm.rdoElex11;
	rdoGroups[12] = document.cnnElexBadgeFrm.rdoElex12;
	rdoGroups[13] = document.cnnElexBadgeFrm.rdoElex13;
	rdoGroups[14] = document.cnnElexBadgeFrm.rdoElex14;
	rdoGroups[15] = document.cnnElexBadgeFrm.rdoElex15;
	return rdoGroups;
}

function cnnElexSendError( id, intFlag ) {
	var message = '&#171; Please answer this question';
	if(intFlag == 0) {
		message = '';
	}
	var errObj = $(id);
	errObj.innerHTML = '';
	errObj.innerHTML = message;
}
function cnnElexSendMessage( intFlag ) {
	var message = 'You have not answered all of the questions above! Please select your answers.';
	var errObj = $('errMessage');
	if(intFlag == 0) {
		message = '';
		errObj.style.display = "none";
	}
	errObj.innerHTML = '';
	errObj.innerHTML = message;
	errObj.style.display = "block";
}

function cnnElexVerifyForm() {
	var rdoIsChecked;
	var atLeastOneMissing = false;
	var rdoGroups = getRdoGroupsArray();
	var rdoGrp;

	// loop through 15 questions...
	for(j=0;j<rdoGroups.length;j++) {
		if(j<10) { c = '0' } else { c = ''; }
		rdoIsChecked = false;
		rdoGrp = rdoGroups[j];
		
		// loop through answer possibilities
		for(i=0;i<rdoGrp.length;i++) {
			if(rdoGrp[i].checked) {
				rdoIsChecked = true;
			}
		}
		if(!rdoIsChecked) {
			atLeastOneMissing = true;
			errId = 'err'+c+j;
			cnnElexSendError(errId);
		}
		else { // send blank message (in case it's not the first submit)
			errId = 'err'+c+j;
			cnnElexSendError(errId, 0);
		}
	}
	if(atLeastOneMissing) {
		cnnElexSendMessage();
	}
	else { // send blank message (in case it's not the first submit)
		cnnElexSendMessage(0);
		cnnElexGenerateBadgeId();
	}
	return false;
}

function cnnElexGenerateBadgeId() {
	// there are six categories of concern
	var intParty; // 1 = Dem; 2 = Rep; 3 = Ind

	// topics scored 1 - 4
	var tpcIraq = 1;
	var tpcEconomy = 1;
	var tpcTerrorism = 1;
	var tpcImmigration = 1;
	var tpcMoral = 1;

	var rdoGroups = getRdoGroupsArray();
	
	// score the party
	intParty = getRdoValue(rdoGroups[0]);
	
	// score terrorism
	tpcTerrorism = tpcTerrorism + (getRdoValue(rdoGroups[1]) - 0) + (getRdoValue(rdoGroups[2]) - 0) + (getRdoValue(rdoGroups[3]) - 0);
	tpcImmigration = tpcImmigration + (getRdoValue(rdoGroups[4]) - 0) + (getRdoValue(rdoGroups[5]) - 0) + (getRdoValue(rdoGroups[6]) - 0);
	tpcIraq = tpcIraq + (getRdoValue(rdoGroups[7]) - 0) + (getRdoValue(rdoGroups[8]) - 0) + (getRdoValue(rdoGroups[9]) - 0);
	tpcEconomy = tpcEconomy + (getRdoValue(rdoGroups[10]) - 0) + (getRdoValue(rdoGroups[11]) - 0) + (getRdoValue(rdoGroups[12]) - 0);
	tpcMoral = tpcMoral + (getRdoValue(rdoGroups[13]) - 0) + (getRdoValue(rdoGroups[14]) - 0) + (getRdoValue(rdoGroups[15]) - 0);
	
	// combine for code
	strCode = '' + intParty + '' + tpcIraq + '' + tpcEconomy + '' + tpcTerrorism + '' + tpcImmigration + '' + tpcMoral;
	
	// set the cookie
	cnnElexCreateCookie('cnnElexBadgeId',strCode,30);
	
	// redirect to results
	location.href = 'results.html';
	
	
}
function getRdoValue( rdoBtn ) {
	for(i=0;i<rdoBtn.length;i++) {
		if(rdoBtn[i].checked == true) {
			return rdoBtn[i].value;
		}
	}
	return false;
}


/* round corner functions */
function getElementsByClass(searchClass, node, tag) { 
	var classElements = new Array(); 
	if(node == null) node = document; 
	if(tag == null) tag = '*'; 
	var els = node.getElementsByTagName(tag); 
	var elsLen = els.length; 
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)"); 
	for (i = 0, j = 0; i < elsLen; i++){ 
		if(pattern.test(els[i].className)) { 
			classElements[j] = els[i]; j++;
		}
	}
	return classElements;
}

function cnnElexRoundCnrs(strClassName) {
	var objs = new Array();
	var startStr = '<div class="t"><div class="b"><div class="l"><div class="r"><div class="bl"><div class="br"><div class="tl"><div class="tr">';
	var endStr = '</div></div></div></div></div></div></div></div>';
	objs = getElementsByClass(strClassName, null, null);
	for(i=0;i<objs.length;i++) {
		currentHTML = objs[i].innerHTML;
		strNewHTML = startStr + currentHTML + endStr;
		objs[i].innerHTML = strNewHTML;
	}
}

