var accessUrl= '';
//TODO to be replaced with proper value while building
//*** These are for qa live (perf env)
// var webserverUrl = 'http://sparkqa.mcgraw-hill.com:80';
//var registrationUrl = 'http://sparkqa.mcgraw-hill.com:80';

//*** These are for qa staging
// var webserverUrl = 'http://connectqastaging.mhhe.com:80';
//var registrationUrl = 'http://novellaqastaging.mhhe.com:80';

//*** These are for dev1 environment
// var webserverUrl = 'http://nvldev01m.eppg.com:8080';
//var registrationUrl = 'http://novellaqastaging.mhhe.com:80';

/*
	The values for the webserver URL and registration URL
	will be coming from the properties file and the values in
	js should always be same as below
	var webserverUrl = 'https://connect.mcgraw-hill.com';
	var registrationUrl = 'http://connect.mcgraw-hill.com:80';
*/

var webserverUrl = 'http://'+'connect.mcgraw-hill.com';
var registrationUrl = 'http://connect.mcgraw-hill.com:80';
var showErrorInstructions = false;
$(document).ready(function() {

	var url = document.location.href;
	var index = url.indexOf('invalidLogin=true');
	var index1 = url.indexOf('invalidLogin=false');
	var index2 = url.indexOf('invalidRAAccount=true');
	var index3 = url.indexOf('licenseExpired=true');
	var index4 = url.indexOf('serverDown=true');
	var index5 = url.indexOf('invalidEmail=true');
	var index6 = url.indexOf('logout=true');
	var index7 = url.indexOf('accessible=false');
	var index8 = url.indexOf('ra=true');
	var index9 = url.indexOf('isSessionTimeout=true');
	var index10 = url.indexOf('invalidUrl=true');
	var index11 = url.indexOf('cachemode=true');

	var msgDisplayed = 0;
	var errorMessage = "";
	if(index!=-1) {
	    msgDisplayed = 1;
	    errorMessage="Invalid email address or password. Please check your entry and try again.";
	    showErrorInstructions = true;
	}
	if(index1 != -1) {
	    msgDisplayed = 1;
	    errorMessage="Invalid email address or password. Please check your entry and try again.";
	     showErrorInstructions = true;
	}

	if(index2!=-1) {
	    msgDisplayed = 1;
	    errorMessage="You have no valid license for the requested site.";
	}
	if(index3!=-1) {
	    msgDisplayed = 1;
	    errorMessage="Your license for this section is Expired.";
	}
	if(index4!=-1) {
	    msgDisplayed = 1;
	    if(index11!=-1) {
 	       errorMessage="We are unable to sign you in at this time. Please try again later.";
 	    } else {
 	       errorMessage="All servers are down!";
 	    }
	}
	if(index5!=-1) {
	    msgDisplayed = 1;
	    errorMessage="Enter your full email address, including everything after the \"@\" symbol. Also, the address can't include unconventional special characters or whitespace.";
	}
	if(index6!=-1) {
	    msgDisplayed = 1;
	   //errorMessage="You have successfully logged out of the system.";
	}
	if(index7!=-1) {
	    msgDisplayed = 1;
	    errorMessage="You do not have access to the given book. Please choose a book you have access to.";
	}
	if(index8!=-1) {
	    msgDisplayed = 1;
	    errorMessage="This is not a valid user account for this book.";
	}
	if(index9!=-1) {
	    msgDisplayed = 1;
	    errorMessage="Your session has expired. Please login again.";
	}
	if(index10 !=-1 && msgDisplayed == 0) {
	    errorMessage="This Web Address does not exist. Please ask your instructor for the correct Web Address for your section.";
	}
	if(getCookie("ACCESSURL")) {
	    document.cookie = deleteCookie('ACCESSURL');
	    var cookieVal = getCookie("ACCESSURL");
	    var exp = new Date();
	    exp.setDate (exp.getDate() - 1);
	    document.cookie = "ACCESSURL" + "=" + cookieVal + "; expires=" + exp.toGMTString();
	}
	if(errorMessage!="") {
		displayErrorInfo(errorMessage);
		/*if($.browser.safari) {
		$("#utlErrs").css({"top":0,"left":$("#loginContainer").offset().left-4});
		} else {
		$("#utlErrs").css({"top": $("#loginContainer").offset().top,"left":$("#loginContainer").offset().left-4});
		}*/
		$("#utlErrs").css("display","block");
	}
    if(!getCookie('JSESSION_SPARK_BRANDING_VARIANT_KEY')) setCookie('JSESSION_SPARK_BRANDING_VARIANT_KEY',getParameter('BRANDING_VARIANT_KEY'));
    if(!getCookie('JSESSION_SPARK_BRAND_ID')) setCookie('JSESSION_SPARK_BRAND_ID',getParameter('BRAND_ID'));
    if(!getCookie('JSESSION_SPARK_UNIV_ID')) setCookie('JSESSION_SPARK_UNIV_ID',getParameter('UNIV_ID'));
    
});

function getParameter(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}


function forgotPassword() {
	var f = document.forgotPasswordForm;
	f.gotourl.value = webserverUrl + f.gotourl.value;
	f.cancelurl.value = webserverUrl + f.cancelurl.value;
	submitForm(document.forgotPasswordForm, registrationUrl + "/paris/forgotpasswordview.do");
}

function submitUrl(linkNameVal) {
	if(linkNameVal == 'newUser') {
		window.open('/getstarted','getstarted','')
	} else {
		submitForm(document.linkForm,"/connect/shortUrl.do?linkName="+linkNameVal);
	}
}
function displayErrorInfo(errorMessage) {
	var offset = $("#submitBtn").offset();
	$("#utilErrMsg").text("");
	$("#utilErrMsg").text(errorMessage);
	$(".utlInnerCntr .utlErrsClose").removeClass("utlCloseIcon");
	if(showErrorInstructions) {
		$(".utlInstruction").css("display","block");
	} else {
		$(".utlInstruction").css("display","none");
	}
	$("#utlErrs").css("display","block");
}