/*
<!-- 
 * $Source: /web/cvs/olc/webapp/styles/shared/v2_infocenter.js,v $
 * $Revision: 1.9 $
 * $Author: daniel $
 * $Date: 2004/12/28 17:54:54 $
 * 
 * Copyright 2002 The McGraw-Hill Companies. All Rights Reserved
 *
 * REVISION HISTORY 
 * ================
 * NOTE: CVS automatically inserts check-in comments below.  Add manually only if reqd.
 *
 * $Log: v2_infocenter.js,v $
 * Revision 1.9  2004/12/28 17:54:54  daniel
 * grammar corrections in error messages
 *
 * Revision 1.8  2004/09/15 21:53:08  jodie_wu
 * Added quizResult and mixQuizResult quick fix for new email result values
 *
 * Revision 1.7  2004/04/19 18:52:38  sshukla
 * removed the extra " " space from the inst center link
 *
 * Revision 1.6  2004/02/18 03:17:05  ychen
 * add sectionid in java script
 *
 * Revision 1.5  2003/12/10 17:45:53  daniel
 * added validation for survey email submit
 *
 * Revision 1.4  2003/08/12 14:07:40  ychen
 * add goto_url
 *
 * Revision 1.3  2003/08/12 13:06:12  ychen
 * The instructor Edition link change
 *
 * Revision 1.2  2003/06/05 21:29:56  ychen
 * add more errors condition for the login
 *
 * Revision 1.1  2003/04/13 02:37:08  daniel
 * First version with new CVS structure; not yet fully tested
 *
 *
-->
*/


/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   Cookies and Login
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

function showAlerts() {
	// New alert system for eRights
	if (getQueryVal('SMS_ERRNO') > 0) {
		// there was an error; find out what it was:
		var errorMessage = "";
		if (getQueryVal('SMS_SERVICE') == "0") {
			// login
			if (getQueryVal('SMS_ERRNO') == "1") {
				errorMessage = "The username and password you entered were not correct.";
			} else if (getQueryVal('SMS_ERRNO') == "2") {
				errorMessage = "The account you selected is already logged in.";
			} else if (getQueryVal('SMS_ERRNO') == "3") {
				errorMessage = "There was an unrecoverable error during login.";
			} else if (getQueryVal('SMS_ERRNO') == "4") {
				errorMessage = "This username and password are not valid for the current site.";
			} else if (getQueryVal('SMS_ERRNO') == "5") {
				errorMessage = "The username doesn't match the current site.";
			}
		} else if (getQueryVal('SMS_SERVICE' == "1")) {
			// edit prefs
			if (getQueryVal('SMS_ERRNO') == "1") {
				errorMessage = "You are not logged in.";
			} else if (getQueryVal('SMS_ERRNO') == "2") {
				errorMessage = "There was an unrecoverable error while trying to save your preferences.";
			}
		}
		if (errorMessage == "") {
			errorMessage = "There was an unexpected error: SERVICE='" + getQueryVal('SMS_SERVICE') + "', ERRNO='" + getQueryVal('SMS_ERRNO') + "'.";
		}
		alert(errorMessage);
	}
}




function infoCenterLink(isbn,wantsProf,isInstructorOnly) {
	// wantsProf is the desired isProf value.
	// isInstructorOnly means there's no student view -- so the url is different.
	// need absolute 
	
	var cookieName = getCookie("OLCGroup") ? "OLCGroup" : "OLC"+isbn ;
	var theUrl = "";
	
	if (getCookieVal(cookieName,"isProf") == 1) {
		// logged in instructors
		if (wantsProf == 1) {
			theUrl = (isInstructorOnly == 1) ? "/sites/" + isbn + "/instructor_view0/index.html" : "/sites/" + isbn + "/student_view0/index.html" ;
		} else {
			if (confirm("You are currently logged in as an instructor. To view the student site, you must first log out. Do you wish to continue?")) {
				deleteCookie(cookieName);
				theUrl = "/sites/" + isbn + "/student_view0/index.html";
			} else {
				theUrl = "";
			}
		}
	} else {
		// students, or non-logged-in users
		if (wantsProf == 1) {
			 //      theUrl = "/sites/" + isbn + "/information_center_view0/instructorlogin2.html";
theUrl = "/webintegration/WebIntegrationServlet?call=InstructorHomeWeblet&external=1&next=sitesubscription/registration/InstructorHome.vm&isbn="+ isbn + "&chrome=/sites/" + isbn + "/chrome.vm&goto_url=/sites/" + isbn + "/student_view0/"

		} else {
			theUrl = (isInstructorOnly == 1 ) ? "/sites/" + isbn + "/instructor_view0/instructorlogin.html" : "/sites/" + isbn + "/student_view0/index.html" ;
		}
	}
	
	// Directly setting the href fails in some browsers, but using
	// window.setTimeout works fine. Go figure.
	if (theUrl) {
		window.setTimeout("window.location.href='"+theUrl+"';",100);
	}
}

/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   Quiz Mailform verifiers
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

function onSurveyEmailSubmit() {
	var s1 = document.forms.frmMail.txtName.value;
	var s2 = document.forms.frmMail.txtMyEmailId.value;
	if (isEmpty(s1)) {
		alert("You must enter your name.");
		document.forms.frmMail.txtName.focus();
	} else if (!isEmail(s2)) {
		alert("You must enter a valid e-mail address.");
		document.forms.frmMail.txtMyEmailId.focus();
	} else {
		document.forms.frmMail.submit();
	}
}

function onPollEmailSubmit() {
	if (validateMailFormFields()) {
	      document.forms.frmMail.submit();
	}
}

function onEmailSubmit(){
	var f = document.forms.frmMail;
	var se = convertSpaces(f.sectionid.value);
	var s1 = convertSpaces(f.txtName.value);
	var s2 = f.txtMyEmailId.value;
	var s3 = f.txtInstEmailId.value;
	var s4 = f.txtTAEmailId.value;
	var s5 = f.txtOtherEmailId.value;
	var o1 = f.cboMyOption[f.cboMyOption.selectedIndex].value; 
	var o2 = f.cboInstOption[f.cboInstOption.selectedIndex].value;
	var o3 = f.cboTAOption[f.cboTAOption.selectedIndex].value; 
	var o4 = f.cboOtherOption[f.cboOtherOption.selectedIndex].value; 
	
	//quizResults and mixedQuizResults XSL using 0 and 1 as values, convert for sending email
	if(o1 == "0"){o1 = "text";}else if(o1 == "1"){o1 = "html";}
	if(o2 == "0"){o2 = "text";}else if(o2 == "1"){o2 = "html";}
	if(o3 == "0"){o3 = "text";}else if(o3 == "1"){o3 = "html";}
	if(o4 == "0"){o4 = "text";}else if(o4 == "1"){o4 = "html";}
	
	var qType = 0;

	for (i=0; i<this.document.forms.length; i++) {
		var ff = this.document.forms[i];
		var eL = ff.elements.length;
		for (j=0; j<eL; j++) {
			var eN = ff.elements[j].name;
			if (eN == "TypeOfQuiz") {
				qType = ff.elements[j].value;
				break;
			}
		}
	}
	if (validateMailFormFields()){
		var URL = "/novella/MailServlet?";
		if (!isEmpty(se)) URL += "&sectionid="+se;
		if (!isEmpty(s1)) URL += "&nm="+s1;
		if (!isEmpty(s2)) {URL += "&a1="+s2+"&o1="+o1;};
		if (!isEmpty(s3)) {URL += "&a2="+s3+"&o2="+o2;};
		if (!isEmpty(s4)) {URL += "&a3="+s4+"&o3="+o3;};
		if (!isEmpty(s5)) {URL += "&a4="+s5+"&o4="+o4;};
		if (qType == 5) {
			f.action = URL;
			f.target = "_new";
			f.submit();
		} else {
			window.open(URL,'popup','toolbar=no,scrollbar=no,width=300,height=100');
		}
	}
}

function validateMailFormFields() {
	var f = document.forms.frmMail;
	var strRequired = "";
	var strEmailError = "";
	var s1 = f.txtName.value;
	var s2 = f.txtMyEmailId.value;
	var s3 = f.txtInstEmailId.value;
	var s4 = f.txtTAEmailId.value;
	var s5 = f.txtOtherEmailId.value;

	if (isEmpty(s1)) {
		strRequired = strRequired + "MYNAME";
		f.txtName.focus();
	}
	else if (isEmpty(s2)) {
		strRequired = strRequired + "MYEMAILID";
		f.txtMyEmailId.focus();
	}
	else if (!isEmail(s2)) {
		strEmailError += "YOUREMAILID";
		f.txtMyEmailId.focus();
	}
	else if (!isEmpty(s3) && !isEmail(s3)) {
		strEmailError += "INSTEMAILID";
		f.txtInstEmailId.focus();
	}
	else if (!isEmpty(s4) && !isEmail(s4)) {
		strEmailError += "TAEMAILID";
		f.txtTAEmailId.focus();
	}
	else if (!isEmpty(s5) && !isEmail(s5)) {
		strEmailError += "OTHEREMAILID";
		f.txtOtherEmailId.focus();
	}
	if (!isEmpty(strRequired)) {
		alert("Please enter your name and email address.");
		return false;
	}
	else if(!isEmpty(strEmailError)) {
		alert("Please enter a correct email address.");
		return false;
	}
	else return true;
}


