/*

 * $Source: /web/cvs/olc/webapp/styles/shared/v1_functions.js,v $
 * $Revision: 1.11 $
 * $Author: mathews $
 * $Date: 2010/02/23 08:33:01 $
 *
 * Copyright 2002 The McGraw-Hill Companies. All Rights Reserved
 * Revision history omitted from clientside files; check CVS log instead
*/

/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   Scripts used by all 2002 styles
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

var loggedIn = 0;

/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   New for SMS integration
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

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 readPrefs(isbn) {
	// reads the prefs cookie and plugs the data into the mailform, if present.
	// This function was called "getCookie" in previous versions.
	
	var cookieName = getCookie("OLCGroup") ? "OLCGroup" : "OLC"+isbn ;
	if (cookieName != "") {
		if (document.forms.frmMail) {
			var f = document.forms.frmMail;
			var mergedNames ="";
			
			if(getCookieVal(cookieName,"firstName") != ""){
			mergedNames = getCookieVal(cookieName,"firstName")+" ";
			}
			
			if(getCookieVal(cookieName,"MI") != ""){
			mergedNames = mergedNames+getCookieVal(cookieName,"MI") + " ";
			}
			if(getCookieVal(cookieName,"lastName") != ""){
			mergedNames = mergedNames+getCookieVal(cookieName,"lastName");
			}
			
			if(f.txtName != undefined){f.txtName.value = mergedNames;}
			if(f.txtMyEmailId != undefined){f.txtMyEmailId.value = getCookieVal(cookieName,"myEmail");}
			if(f.cboMyOption != undefined){f.cboMyOption.selectedIndex = getCookieVal(cookieName,"myStyle");}
			if(f.txtInstEmailId != undefined){f.txtInstEmailId.value = getCookieVal(cookieName,"profEmail");}
			if(f.cboInstOption != undefined){f.cboInstOption.selectedIndex = getCookieVal(cookieName,"profStyle");}
			if(f.txtTAEmailId != undefined){f.txtTAEmailId.value = getCookieVal(cookieName,"taEmail");}
			if(f.cboTAOption != undefined){f.cboTAOption.selectedIndex = getCookieVal(cookieName,"taStyle");}
			if(f.txtOtherEmailId != undefined){f.txtOtherEmailId.value = getCookieVal(cookieName,"otherEmail");}
			if(f.cboOtherOption != undefined){f.cboOtherOption.selectedIndex = getCookieVal(cookieName,"otherStyle");}
		}
	}
	eatCookies(isbn); // squeezing this in here for now...
}

function eatCookies(isbn) {
	// check to see if there are too many cookies on this domain; if so, eat some.
	var allCookies = document.cookie.split("; ");
	if (allCookies.length > 17) {
		var theUrl = "/olcweb/styles/shared/eatcookies.html?" + isbn;
		window.open(theUrl,"eatCookies","width=300,height=400,resizable,scrollbars");
	}
}

function checkLoginStatus(isbn) {
	var cookieName = getCookie("OLCGroup") ? "OLCGroup" : "OLC"+isbn ;
	if (getCookie(cookieName)) {
		if (getCookie(cookieName) == "expire") {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function instructorViewLink(isbn) {
	// wantsProf is the desired isProf value.
	// These urls are absolute, because there's no way to tell from where in the site
	// the user is linking.
	
	// NOTE:  this function is not being used, because at the last minute some idiot
	// decided he wanted to have unprotected instructor atoms.  Which is stupid, but so it goes.
	// I have a sneaking suspicion we will be bringing this back before too long...
	
	
	var cookieName = getCookie("OLCGroup") ? "OLCGroup" : "OLC"+isbn ;
	var theUrl = "";
	
	if (getCookieVal(cookieName,"isProf") == 1) {
		// logged in instructors
		theUrl = "/sites/" + isbn + "/instructor_view0/index.html";
	} else {
		// students or non-logged-in users
		theUrl = "/sites/" + isbn + "/instructorlogin.html";
	}
	
	// Directly setting the href fails in some browsers, but using
	// window.setTimeout works fine. Go figure.
	window.setTimeout("window.location.href='"+theUrl+"';",100);
}



/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   Glossary
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/


function initGlossaryLayers() {
	if (document.layers) {
		for (strLayer in document.layers) {
			if (strLayer.indexOf("glossarylayer") > -1) {
				oLayer = eval("document.layers." + strLayer);
				oLayer.visibility='hide';
			}
		}
	}
	if (document.all) {
		for (strDiv in document.all) {
			if (strDiv.indexOf("glossarylayer") > -1) {
				oDiv = eval("document.all." + strDiv);
				oDiv.style.visibility='hidden';
			}
		}
	}
}

/* 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   Old stuff
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/


function newWindow() {
        newWindow=window.open(' ', 'newWin', 'width=300, height=300')
        newWindow.document.write("<html><head></head><body>something</body></html>")
        newWindow.document.close()
}

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


function onPollEmailSubmit(){
	var f = document.forms.frmMail;
	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;
	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; 
		
	if (checkFields()){
	      document.frmMail.submit();

	/*
		var URL = "/novella/PollMailServlet?";
		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;};
			
		URL +="&GXHC_gx_session_id_=" + f.sessionId.value;
		window.open(URL,'popup','toolbar=no,scrollbar=no,width=300,height=100');
	*/	
	}
}

	


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 (checkFields()){
		var URL = "/novella/MailServlet?";
		if (!isEmpty(se)) URL += "&sectionid="+se;
		if (!isEmpty(s1)) URL += "&studentName="+s1;
		if (!isEmpty(s2)) {URL += "&studentEmail="+s2+"&studentEmailFormat="+o1;};
		if (!isEmpty(s3)) {URL += "&instructorEmail="+s3+"&instructorEmailFormat="+o2;};
		if (!isEmpty(s4)) {URL += "&taEmail="+s4+"&taEmailFormat="+o3;};
		if (!isEmpty(s5)) {URL += "&otherEmail="+s5+"&otherEmailFormat="+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 convertSpaces(str) {

var out = "",flag=0;
for (i = 0; i < str.length; i++) {

        if (str.charAt(i) != " ") {
                out += str.charAt(i);
        }
        else{
                out += "%20";
        }
}
return out;

}

function checkFields(){
	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;
}

function isEmpty(s){   
	return ((s == null) || (s.length == 0))
}
function isEmail (s){   
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")){ 
        i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != ".")){ 
        i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
function isWhitespace (s){   
    var whitespace = " \t\n\r";
    var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;

}


