//  Loads Google Map into display div on campus pages
function loadMap () {
	if (mapLoaded == false) {
		document.getElementById("googleMapContainer").innerHTML = mapContent;
		mapLoaded = true;
	}
}

//  Unique loader for Fresno Aviation
function loadFresnoAviationMap() {
	var aviationMap = '<img id="visitHeader_FresnoAviation" src="../../_presentation/images/visit_Fresno_Aviation.gif" alt="Come visit our Fresno Aviation Campus" title="" /><iframe width="450" height="450" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=4985+E+Andersen+Ave+Fresno,+CA+93727&amp;sll=37.020098,-95.712891&amp;sspn=49.531015,90.351563&amp;ie=UTF8&amp;s=AARTsJp5uIr1crfPxbYqD22yjqFu-zucLw&amp;ll=36.785298,-119.722309&amp;spn=0.030933,0.038624&amp;z=14&amp;iwloc=addr&amp;output=embed"></iframe><br /><br /><a target="_blank" href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=4985+E+Andersen+Ave+Fresno,+CA+93727&amp;sll=37.020098,-95.712891&amp;sspn=49.531015,90.351563&amp;ie=UTF8&amp;ll=36.785298,-119.722309&amp;spn=0.030933,0.038624&amp;z=14&amp;iwloc=addr" style="text-align: left; color: #4966ab; font-size: 11px;">View Larger Map</a>';
	
	document.getElementById("googleMapContainer").innerHTML = aviationMap;	
}

// Auto signature on textbox focus
function fillSignature(objSig, strSig){
	if (objSig && strSig){
		if (objSig.value == ""){
			objSig.value = strSig;
		}
	}
}

//  Turns on the map display
function openMap () {
	document.getElementById("mapPop").style.visibility = "visible";
}

//  Turns off the map display
function closeMap () {
	document.getElementById("mapPop").style.visibility = "hidden";
}

// RADIO-CHECKBOX
function radioCheck(nam,obj){
	if (nam && obj){
		for (var i=0; i < obj.form.elements.length;i++){
			var e = obj.form.elements[i];
			if (e.name.substring(0,nam.length) == nam){
				if (obj.id == e.id){
					if (obj.checked){
						obj.checked = true;
					}else{
						obj.checked = false;
					}
				}else{
					e.checked = false;
				}
			}
		}
	}
}

//  Helps IE with select width issues; prevents text getting cut off
function selectFix (newWidth) {
	var programSelect = document.getElementById("programName");
		programSelect.style.width = newWidth+"px";
}

//  Enables Program select once a campus has been selected
function enableProgram () {
	document.getElementById("programName").disabled = false;	
}

//  Returns to user to the page they were on before going to the request information page
function thanksClose(previousPage) {
	window.location = previousPage;
}

//  Rollover functionality for offered at campus map
function campusRollover(imageSrc, imageId, direction) {
	if (direction == "over") {
		var newSrc = imageSrc.replace("link_", "rollover_");
	} else if (direction == "out") {
		var newSrc = imageSrc.replace("rollover_", "link_");
	}
	
	var currentImage = document.getElementById(imageId);
		currentImage.src = newSrc;	
}

//  Rollover functionality for buttons found on the various pages
function buttonRollover(imageSrc, imageId, direction) {
	if (direction == "over") {
		var newSrc = imageSrc.replace("btn_", "rollover_");
	} else if (direction == "out") {
		var newSrc = imageSrc.replace("rollover_", "btn_");
	}
	
	var currentImage = document.getElementById(imageId);
		currentImage.src = newSrc;	
}

//  Resets the form and any error messages that may have been written out to the page
function formReset() {
	var theForm = document.getElementById('requestForm');
	
	//  Reset form error messages
	var formInputs = theForm.elements;
		for (var i = 0; i < formInputs.length; i++) {
			//  Strips out the errorMessage CSS class name
			formInputs[i].className = formInputs[i].className.replace(/errorMessage/,'');
			
			if(formInputs[i].errorMessage) {
				formInputs[i].parentNode.removeChild(formInputs[i].errorMessage);
				formInputs[i].errorMessage = null;
				formInputs[i].parentNode.errorMessage = null;	
			}
			
			formInputs[i].onfocus = null;
		}
	
	theForm.reset();
}

//  Creates and then displays the results to the online quiz
function resultsDisplay() {
	var quizForm = document.getElementById('onlineQuiz');	
	var totalScore = 0;
	
	var quizQuestions = quizForm.elements;
		for (var i = 0; i < quizQuestions.length; i++) {
			var questionNum = Math.floor((i + 3)/3);
			
			if (quizQuestions[i].checked) {
				totalScore += Number(quizQuestions[i].value);
			} 
		}
	
	var resultsPop = document.createElement('div');
	document.body.appendChild(resultsPop);
		resultsPop.id = "resultsPop";
	
	
	
	var resultsBg = document.createElement('img');
	resultsPop.appendChild(resultsBg);
		resultsBg.className = "png";
		resultsBg.id = "resultsBg";
		resultsBg.src = "../../_presentation/images/background_ResultsPop.png";
	
	
	var closeBtn = document.createElement('img');
	resultsPop.appendChild(closeBtn);
		closeBtn.id = "closeResults";
		closeBtn.src = "../../_presentation/images/btn_CloseMap.gif";
		closeBtn.onclick = function () {
			document.body.removeChild(resultsPop);
			
			window.location.hash = "";
		}
	
	var resultsInfo = document.createElement('p');
	resultsPop.appendChild(resultsInfo);
		resultsInfo.id = "resultsInfo";
	
	var score = document.createElement('h2');
	resultsPop.appendChild(score);
		score.id = "score";
	
	var scoreText = document.createTextNode("Your score: " + totalScore);
	score.appendChild(scoreText);
	
	var feedback = document.createElement( 'p' );
	resultsPop.appendChild( feedback );
		feedback.id = "feedback";
	
	
	var feedbackHeader = document.createElement( 'span' );
	feedback.appendChild(feedbackHeader);
	
	var feedbackText = "";
	var feedbackHeaderText = "";
	
	if ( totalScore >= 20 ) {
		feedbackHeaderText = "20 points or higher";
		feedbackText = "an online course is a real possibility for you.";
	} else if ( (totalScore < 20) && (totalScore >= 11) ) {
		feedbackHeaderText = "Between 11 and 20 points";
		feedbackText = "an online course may work for you, but you may need to make a few adjustments in your schedule and study habits to succeed.";
	} else {
		feedbackHeaderText = "Less than 10 points";
		feedbackText = "an online course may not currently be the best alternative for you; talk to your counselor. ";
	}
	
	feedbackHeader.appendChild(document.createTextNode(feedbackHeaderText));
	feedback.appendChild(document.createTextNode(feedbackText));
	
	var infoText = document.createTextNode("No matter what you scored, remember that online learning is not easy. Your professor will demand at least the same quality of work as they would receive from you in a face-to-face classroom. A similar number of hours will need to be committed throughout the course of a semester for an online course as to a face-to-face course. Remember that your course may include deadlines and instructions on assignments, but there will not be anyone telling you to \"turn in your assignment.\" Online learning is convenient - you do not have to commute to campus, and you can attend class at your convenience - either early in the morning, late at night, or anytime in-between. Just remember, no matter when you decide to study, your professor will hold you accountable - so study hard, and have a great term!");
	resultsInfo.appendChild(infoText);
	
	window.location.hash = "top";
}

//  ===========================================================================================================
//  Functions brought straight over from SJVC provided code

if (parseInt(navigator.appVersion)>3){

	screenw = screen.width;

	screenh = screen.height;

}else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled()){

	var jToolkit = java.awt.Toolkit.getDefaultToolkit();

	var jScreenSize = jToolkit.getScreenSize();

	screenw = jScreenSize.width;

	screenh = jScreenSize.height;

}

function popapp(jobid){

	var popapp = '';
	popapp = window.open('https://www.sjvc.edu/jobs/jobs_app.php?jobid='+jobid+'','popapp','toolbar=no,menubar=no,resizable=no,scrollbars=yes,status=no,location=no,width=710,height=' + (screenh - 100) + ',left=0,top=0');
	if (window.focus) {popapp.focus()}

}

function popapphelp(){

	var popapphelp = '';
	popapphelp = window.open('http://www.sjvc.edu/jobs/jobs_app_help.php','popapphelp','toolbar=no,menubar=no,resizable=no,scrollbars=yes,status=no,location=no,width=710,height=' + (screenh - 100) + ',left=0,top=0');
	if (window.focus) {popapphelp.focus()}

}

function ctlPastJobs(){
	objPJlnk = findObj("pastjobs_link");
	objPJdiv = findObj("pastjobs");
	if (objPJlnk && objPJdiv){
		if (objPJdiv.style.display == "inline"){
			objPJdiv.style.display = "none";
			objPJlnk.innerHTML = "[Click here to VIEW past jobs you've applied for]";
		}else{
			objPJdiv.style.display = "inline";
			objPJlnk.innerHTML = "[Click here to HIDE past jobs you've applied for]";
		} 
	}

}

function divContact(objCampus,key){
	if (objCampus.checked){
		objshow("dspcontact" + key);
	}else{
		objhide("dspcontact" + key);
	}
}

function divEmploymentApp(element){
	objDiv = findObj("div_"+element);
	objExp = findObj("exp_"+element);
	if (objDiv && objExp){
		if (objDiv.style.display == "inline"){
			objDiv.style.display = "none";
			objExp.innerHTML = "[+]";
		}else{
			objDiv.style.display = "inline";
			objExp.innerHTML = "[-]";
		}
	}
}

// SEND COMMENTS (FROM JOBS_APP_MAIN)
// ---------------------------------------------------------------------------------------------------------------------------------------
function msgsendwm(id){

	msgsenderr('');
	var sendnow = true;
	objMsgBody = findObj('msgbody');
	objMsgErrBody = findObj('msgerrbody');
	objMsgErrDspBody = findObj('msgerrdspbody');
	objMsgStatus = findObj('msgstatus');

	// CHECK BODY
	var msgbody = "";
	if (objMsgBody && objMsgErrBody && objMsgErrDspBody){
		if (!objMsgBody.value){
			objMsgErrBody.style.display = "inline";
			objMsgErrDspBody.innerHTML = "Enter a descriptive plain text message";
			sendnow = false;
		}else{
			msgbody = objMsgBody.value;
			objMsgErrBody.style.display = "none";
			objMsgErrDspBody.innerHTML = "";
		}
	}	

	// AJAX CALL
	if (sendnow){
		// SHOW SENDING ANIMATED GIF
		if (objMsgStatus){
			objMsgStatus.innerHTML = "Sending Response...";
		}
		// AJAX POST TO ADSMSGI ROUTINE
		var postData = "id="+encodeURIComponent(id)+"&msgbody="+encodeURIComponent(msgbody);
		makePOSTRequest('jobs_ajxsndwm.php', postData);
	}

}

// SEND MESSAGE
// ---------------------------------------------------------------------------------------------------------------------------------------
function msgsend(jobid){

	// CLEAR GENERAL ERROR
	msgsenderr('');

	// GET OBJECTS
	objMsgEmail = findObj('msgemail');
	objMsgBody = findObj('msgbody');
	objMsgStatus = findObj('msgstatus');

	objMsgErrEmail = findObj('msgerremail');
	objMsgErrBody = findObj('msgerrbody');

	objMsgErrDspEmail = findObj('msgerrdspemail');
	objMsgErrDspBody = findObj('msgerrdspbody');

	var sendnow = true;

	// CHECK EMAIL
	var msgemail = "";
	if (objMsgEmail && objMsgErrEmail && objMsgErrDspEmail){
		if (!objMsgEmail.value){
			objMsgErrEmail.parentNode.parentNode.style.visibility = "visible";
			objMsgErrEmail.style.display = "inline";
			objMsgErrDspEmail.innerHTML = "Enter your email address";
			sendnow = false;
		}else{
			msgemail = objMsgEmail.value;
			objMsgErrEmail.style.display = "none";
			objMsgErrDspEmail.innerHTML = "";
		}
	}

	// CHECK BODY
	var msgbody = "";
	if (objMsgBody && objMsgErrBody && objMsgErrDspBody){
		if (!objMsgBody.value){
			objMsgErrBody.parentNode.parentNode.style.visibility = "visible";
			objMsgErrBody.style.display = "inline";
			objMsgErrDspBody.innerHTML = "Enter a descriptive plain text message";
			sendnow = false;
		}else{
			msgbody = objMsgBody.value;
			objMsgErrBody.style.display = "none";
			objMsgErrDspBody.innerHTML = "";
		}
	}	

	// AJAX CALL
	if (sendnow){
		// SHOW SENDING ANIMATED GIF
		if (objMsgStatus){
			objMsgStatus.innerHTML = "Sending Response...";
		}
		// AJAX POST TO ADSMSGI ROUTINE
		var postData = "jobid="+encodeURIComponent(jobid)+"&msgemail="+encodeURIComponent(msgemail)+"&msgbody="+encodeURIComponent(msgbody);
		makePOSTRequest('http://www.sjvc.edu/jobs/jobs_ajxsnd.php', postData);
	}

}

// CALLED FROM MSGSEND()
function msgsendscc(){

	objMsgEmail = findObj('msgemail');
	objMsgBody = findObj('msgbody');
	objMsgStatus = findObj('msgstatus');

	objMsgErrEmail = findObj('msgerremail');
	objMsgErrBody = findObj('msgerrbody');

	objMsgErrDspEmail = findObj('msgerrdspemail');
	objMsgErrDspBody = findObj('msgerrdspbody');

	// CLOSE ERRORS
	if (objMsgErrEmail && objMsgErrDspEmail){
		objMsgErrEmail.style.display = "none";
		objMsgErrDspEmail.innerHTML = "";
	}
	if (objMsgErrBody && objMsgErrDspBody){
		objMsgErrBody.style.display = "none";
		objMsgErrDspBody.innerHTML = "";
	}

	// CLEAR INPUT
	if (objMsgEmail){
		objMsgEmail.value = "";
	}
	if (objMsgBody){
		objMsgBody.value = "";
	}

	// SUCCESS MESSAGE
	if (objMsgStatus){
		objMsgStatus.innerHTML = "Your message has been sent.";
	}

}

// MESSAGE ERROR
function msgsenderr(value){

	objMsgStatus = findObj('msgstatus');
	objMsgErrEmail = findObj('msgerremail');
	objMsgErrDspEmail = findObj('msgerrdspemail');

	// DISPLAY ERROR
	if (objMsgErrEmail && objMsgErrDspEmail){
		if (value){
			objMsgErrEmail.style.display = "inline";
			objMsgErrDspEmail.innerHTML = value;
		}else{
			objMsgErrEmail.style.display = "inline";
			objMsgErrDspEmail.innerHTML = "Error not passed...";
		}
	}
	if (objMsgStatus){
		objMsgStatus.innerHTML = "";
	}

}

// AJAX XMLHTTP METHOD
// ---------------------------------------------------------------------------------------------------------------------------------------
// NOTE: In order to avoid a syntax error in Firefox, if the returned content is
// NOT valid XML, use the overrideMimeType method to set the content-type.

var http_request = false;

function makePOSTRequest(url, parameters){

	http_request = false;

	// IE7, Mozilla, Safari, etc: Use native object
	if (window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}

	 // IE5.X and IE6.X
	}else if (window.ActiveXObject){

		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}

	}

	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
      
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);

}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			eval(http_request.responseText);
			//alert(http_request.responseText);
		} else {
			alert('There was a problem with the request.');
		}
	}
}
