var currpage = 1;
var offersLoaded = 0;
var ocfg = new Object();
var perror = false;
var campusCheck = false;
var _ajaxConfig = { '_cfscriptLocation':'/siteInc/com/offerValidate.cfc','_jsscriptFolder':'/siteInc/js','debug':false};
var genInfo = false;
var schoolShow = false;
var curroid = 0;
var finalizeButton;

$(document).ready(function() {
	if(typeof otmplt == 'undefined')
		otmplt = 'default';
	if(typeof showtype == 'undefined')
		showtype = 'default';
	if(typeof thankYouMsg == 'undefined')
		thankYouMsg = 'Thank you.  In order to finalize your request for information, you must click the "Submit" button below.'
	//continue / NEXT >> to go from step to step
	if(typeof btn_cont == 'undefined')
		btn_cont = '';
	//When finished with the current offer's questions
	if(typeof btn_finish == 'undefined')
		btn_finish = 'FINISH >>';
	//After questions are submitted for offers, this button appears at bottom of page and will bring up gen info when clicked
	if(typeof btn_fnl == 'undefined')
		btn_fnl = '<div id="finalize"><input type="button" value="Submit" onClick="finalize();"></div>';
	//the gen info submit/final button of the process
	if(typeof btn_gi == 'undefined')
		btn_gi = '';
	if(typeof btn_camp == 'undefined')
		btn_camp = '';
		
	if(typeof loaddiv == 'undefined')
		loaddiv = '<div id="loading" style="display:none;"><img src="/images/processing_window.gif"></div>';

	if(typeof skipGI == 'undefined')
		skipGI = true;
		
	if(typeof gototop == 'undefined')
		gototop = false;
	if(typeof preloadschool == 'undefined')
		preloadschool = false;

	var displayOffers = '';
	var initoffers = '';
	
	if(op >= 2)
		initoffers = '<div style="border:1px solid red;width:500px;margin-left:40px;" align=center><strong>We\'re sorry, you have reached your daily limit of 2 school applications.</strong></div>';

	offersLoaded = $("#offerHolder .row").length;

	$("#offerHolder .row").each(function(ix) {
		var pages = 0;
		var newoffer = '';

		for(p=1; p<=10; p++)
			if($("#offerHolder .row:eq("+ix+") table tr[page="+p+"]").length != 0)
				pages = p;

		oid = $.Right(this.id,32);
		ocfg[oid] = new Object();
		ocfg[oid].pages = pages;
		ocfg[oid].idx = ix;
		ocfg[oid].currpage = 1;
		ocfg[oid].logo = $("#offerHolder .row:eq("+ix+") .photo").html();
		ocfg[oid].description = $("#offerHolder .row:eq("+ix+") .description").html();
		ocfg[oid].headline = $("#offerHolder .row:eq("+ix+") .description strong:eq(0)").html();
		ocfg[oid].offerName = $("#offerHolder .oh[oid="+oid+"] .offerName").html();
		ocfg[oid].campus = $.ListFind(zt, oid) ? true : false;

		newoffer = buildOffer(oid);
		
		 //alert($(this + ' table').length);
		if(!ix)
			$("#viewOffers").html(initoffers);
		$("#viewOffers").append(newoffer);
		if(preloadschool)
			showschool(oid)
	});
	$("#viewOffers").append(btn_fnl);
});

function buildOffer(oid) {
	newoffer = '<div oid=' + oid + ' class="row">';
	
	if(otmplt == 'FDS') {
		/*
		<a href="javascript:showschoolclk(document.getElementById('school#currentrow#'),#currentrow#); positionschool(event);" onMouseOver="showschool(document.getElementById('school#currentrow#'),#currentrow#);" onMouseOut="showlink=false"><!-- <span>Rollover for more info</span> --></a>
		*/
		newoffer += '<li><div class="resultRollover" id="school_link" onClick="javascript:showschool(\'' + oid + '\');" onMouseOver="delayshow(\'' + oid + '\')" onMouseOut="unshow();" style="cursor:pointer;"></div>';
		newoffer += '<span class="schoolDescription" id="school_link">' + ocfg[oid].description;
		newoffer += '<a href="javascript:showschool(\'' + oid + '\');">Click here to read more</a>';
		newoffer += '</span></li>';

	/*
	
						<span class="schoolLink school#currentrow#">
							<div id="school#currentrow#" style="position:absolute;border:1px solid SteelBlue;background-color:white;z-index:1000;width:690px;height:440px;display:none; margin: 0 0 0 -326px;">
								<div style="text-align:right;padding:10px;"><a href="javascript:hideschool(document.getElementById('school#currentrow#'));" style="color:black;">CLOSE [X]</a></div>
								<iframe src="showschool.cfm?#encryptURL("offer_location=#offer_location#&pg=#pagenum#","fastdegreesearch")#" height="420" width="100%" frameborder="0" style="padding-left:30px;"></iframe>
							</div>

							<a href="javascript:showschoolclk(document.getElementById('school#currentrow#'),#currentrow#); positionschool(event);">#listFirst(offer_name,"-")#</a>
						</span>
	
	
	
	
		newoffer = '<div oid=' + oid + ' class="row">';
		newoffer += '<div class="photo" style="float:left;">' + ocfg[oid].logo + '</div><div class="description">' + ocfg[oid].description + '</div>';
		newoffer += '<div style="clear:both;"><a onClick="showschool(\'' + oid + '\')" style="color:blue;font-weight:bold;text-decoration:underline;cursor:pointer;">Click here for more information!</a></div>';
		newoffer += '<div class="oform"></div>';
		newoffer += '</div>';
		
		*/
	} else if (otmplt == 'CEW') {
		
	} else {
		newoffer += '<div class="photo" style="float:left;">' + ocfg[oid].logo + '</div><div class="description">' + ocfg[oid].description + '</div>';
		/* newoffer += '<div style="clear:both;"><a onClick="showschool(\'' + oid + '\')" style="color:blue;font-weight:bold;text-decoration:underline;cursor:pointer;">Click here for more information!</a></div>'; */
	}
	if(showtype != 'pop')
		newoffer += '<div class="FormTextWarning" align=center>* All Questions Are Required</div>';
	newoffer += '<div class="oform"></div>';
	newoffer += '</div>';
	
	return newoffer;
}

function validateCampus(zs,oid) {
	var pagehtml = '';  //<strong>' + ocfg[oid].headline + '</strong><br>
	if(!zs) {
		zp = $("#ARBzip")[0].value;
		campusCheck = true;
	}
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'checkCampus', oid, zp, function(cm) {
		if(cm.status == 'false') {
			pagehtml += '<div align="center" class="nocampus">We\'re sorry, this school does not have any campus locations in your area.</div>';
			$("#viewOffers .row[oid="+oid+"] .oform").html(pagehtml);
			schoolShow = false;
		} else {
			$('#offerHolder .oh[oid='+oid+']').html(cm.content);
			showschool(oid, true);
		}
	});
}

	
function delayshow(oid) {
	if(!schoolShow) {
		curroid = oid;
		y = setTimeout('showDelaySchool(\''+oid+'\')' ,1000);
	}
}
	
function unshow() {
	if(!schoolShow)
		curroid = 0;
}

function showDelaySchool(oid) {
	if(curroid == oid)
		showschool(oid);
}


function showschool(oid, bpcc) {
	var pagehtml = '';
	//alert(curroid + '  ' + oid + '  ' + schoolShow);
	if((showtype == 'pop' && (curroid == oid || !schoolShow)) || showtype != 'pop') {
		curroid = oid;
		if(campusCheck && ocfg[oid].campus && bpcc != true) {
			validateCampus(1,oid);
		} else if(ocfg[oid].campus && zp == '') {
			pagehtml = '<div id="zipBoxSmall"><p>This school is campus based.  Please enter your zipcode in the box below to see if there are any campus locations in your area.</p><div>Your 5-digit Zipcode: <input name="ARBzip" id="ARBzip" size=5 maxlength=5 value=""> <a style="cursor:pointer;" onClick="validateCampus(0,\'' + oid + '\');">';
			
			if(btn_camp == '')
				pagehtml += 'GO >>';
			else if (!$.FindNoCase("<img",btn_camp,1))
				pagehtml += '<img src="/designs/'+designpath+'/images/'+btn_camp+'">';
			else
				pagehtml += btn_camp;

			pagehtml += '</a></div></div>';
			
			
			
			//$("#viewOffers .row[oid="+oid+"] .oform").html(pagehtml);
		} else if(ocfg[oid].pages == 0 || ocfg[oid].pages == 1) {
			//$.dump($('#offerHolder .row:eq("+ ocfg[oid].idx +") table').html());
			//$("#viewOffers .row[oid="+oid+"] .oform").html(
	
			pagehtml += "<table class=\"offerTable\">" + $('#offerHolder .row:eq('+ ocfg[oid].idx +') table').html() + "</table><div class=finishbutton><a onClick='nextPage(\"" + oid + "\");' class='finishLink'>";

			if(btn_finish == '')
				pagehtml += 'FINISH >>';
			else if (!$.FindNoCase("<img",btn_finish,1))
				pagehtml += '<img src="/designs/'+designpath+'/images/'+btn_finish+'">';
			else
				pagehtml += btn_finish;
 			pagehtml += "</a></div>";
		} else {
			pagehtml = '<table class="offerTable">';
			$('#offerHolder .row:eq('+ ocfg[oid].idx +') table tr[page='+ocfg[oid].currpage+']').each(function() {
				pagehtml += "<tr id="+this.id+" class=\"FormText "+this.className+"\" style=\"display:"+this.style.display+"\">" + $(this).html() + "</tr>";
				//alert(this.className + ' = ' + this.style.display);
			});
			pagehtml += '</table><div class=finishbutton><a onClick="nextPage(\'' + oid + '\');" style="cursor:pointer;">';
			if(btn_cont == '')
				pagehtml += 'NEXT >>';
			else if (!$.FindNoCase("<img",btn_cont,1))
				pagehtml += '<img src="/designs/'+designpath+'/images/'+btn_cont+'">';
			else
				pagehtml += btn_cont;
			pagehtml += '</a></div>';
			/*		if(ocfg[oid].pages == ocfg[oid].currpage)
				pagehtml += 'FINISH >>'
			else
				pagehtml += 'CONTINUE >>'
			pagehtml += '</a>'; */
		}

		schoolShow = true;
		if (showtype == 'pop') {
			popit = '<div id="'+oid+'" class="popwindow">';
			popit += '<div class="xclose"><a href="javascript:hideschool(\'' + oid + '\');" style="color:black;">CLOSE [X]</a></div><div class="offerBG">';
			popit += '<div class="description"><div class="photo">' + ocfg[oid].logo + '</div>' + ocfg[oid].description + '</div><div = "clear"></div><div style="padding:10px;">';
			popit += pagehtml;
			popit += '</div></div></div>';
			$("#viewOffers .row[oid="+oid+"] .oform").html(popit);
		} else {
			$("#viewOffers .row[oid="+oid+"] .oform").html(pagehtml);
		}

	}
}

function hideschool(oid) {
	schoolShow = false;
	$("#"+oid).hide();
}

function nextPage(oid) {
	var validate = new Object();
	var errorMessage = '';
	var errors = false;
	
	// Text and select boxes
	$("#viewOffers .row[oid="+oid+"] select,#viewOffers .row[oid="+oid+"] input[type!=checkbox][type!=radio]").each(function() {
		$(this).each(function() { 
			validate[this.name] = this.value;
		})
	});
						
	// checkboxes
	$("#viewOffers .row[oid="+oid+"] input[type=checkbox]").each(function() {
		$(this).each(function() { 
			if(this.checked)
				validate[this.name] = this.value;
			else
				validate[this.name] = '';
		})
	});

	// radio buttons
	$("#viewOffers .row[oid="+oid+"] input[type=radio]").each(function() {
		$(this).each(function() { 
			if(this.checked) {
				validate[this.name] = this.value;
			} else if (validate[this.name] == undefined)
				validate[this.name] = '';
			})
	});
	
	//$.dump(validate);

	for (var rr in validate)
		$("#viewOffers .row #_" + rr).removeClass("FormTextWarning").addClass("FormText");
	 
	//$.dump(validate);
	
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'validateOffer', oid, validate, zp, function(es) {
		
		
		for (var f in es) {
			$("#viewOffers .row #_" + f).removeClass("FormText").addClass("FormTextWarning");
			errorMessage = $.ListAppend(errorMessage, es[f], "\n");
			errors = true;
		}

		if(errors)
			alert(errorMessage);
		else {
			$("#submitform").append($("#viewOffers .row[oid="+oid+"] .oform").html());

			if(ocfg[oid].pages <= ocfg[oid].currpage) {
				document.submitform.offers.value = $.ListAppend(document.submitform.offers.value, oid);
				if(skipGI) {
					finalize();
				} else {
					$("#viewOffers .row[oid="+oid+"] .oform").html('<div id="thankYou">' + thankYouMsg + '</div>');
					schoolShow = false;
					//alert($("#viewOffers .row[oid="+oid+"]").html());
					$("#viewOffers .row[oid="+oid+"] .resultRollover").removeClass("resultRollover").addClass("offerDone").attr({ onclick: '',onmouseover: ''});
					$("#viewOffers .row[oid="+oid+"] A").hide();
					$("#finalize").show();
					//finalize();
				}
			} else {
				ocfg[oid].currpage++;
				showschool(oid);
			}
		}
	});
}

function finalize() {
	if(op >= 2)
		alert('We\'re sorry, you have reached your daily limit of 2 school applications.')
	else {
		if(genInfo) {
			$("#viewOffers .hop").hide();
			$("#loading").show();

			var GIvalidate = new Object();
		
			$("#viewOffers #reg-form select,#viewOffers #reg-form input[type!=radio]").each(function() {
				$(this).each(function() { 
					GIvalidate[this.name] = this.value;
				})
			});
								
			// radio buttons
			$("#viewOffers #reg-form input[type=radio]").each(function() {
				$(this).each(function() { 
					if(this.checked) {
						GIvalidate[this.name] = this.value;
					} else if (GIvalidate[this.name] == undefined)
						GIvalidate[this.name] = '';
					})
			});
			
			//$.dump(GIvalidate);
			//alert(perror);
			DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'infoValidate', GIvalidate, perror, function(rs) {
				if(rs.status) {
					//success with validation
					$("#submitform").append($("#viewOffers").html());
					$("#submitform").append("<input type=hidden name=prevalidated value=true>");
					$("#submitform").submit();
				} else {
					$("#loading").hide();
					$("#viewOffers .hop").show();
					perror = rs.perror;
					alert(rs.details.replace(/\\n/g,"\n"));
				}
			});
	
		} else {
			myHTML = loaddiv;
			if(gototop)
				window.location.href='#';
			myHTML += '<div align="center" style="margin-bottom:6px;" class="hop"><strong>Please tell us where to send your information.</strong></div>' + $("#geninfo").html() + '<div align="center"  class="hop">';
			if(btn_gi == '')
				myHTML += '<input type=button onClick="finalize();" value="Request Information" class="requestInfo">';
			else if (!$.FindNoCase("<img",btn_gi,1))
				myHTML += '<img src="/designs/'+designpath+'/images/' + btn_gi + '" style="cursor:pointer;" onClick="finalize();">';
			else
				myHTML += btn_gi;
				
			myHTML += '</div>';
			
			$("#viewOffers").html(myHTML);
			$("#loading").hide();
			//$("#finalize").html('<input type="button" value="Request Information">');
			genInfo = true;
		}
	}
}

function limitreached() {
	alert('We\'re sorry, you have reached your daily limit of 2 school applications.');
	return false
}