// Function for retrieving cookie values
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin !== 0) {
		 return null;
		 }
	} else {
		begin += 2;
		}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
		}
	return unescape(dc.substring(begin + prefix.length, end));
}

// Set up a variable to be used to see if the cookie has been set
var userSave = getCookie("homeSave");

// Function called when the login form is submitted
function submitLogin(){

	// Declare variable to hold form action and target values for each portal
	
	//*************** Acceptance **********************
	//var cons_action = 'https://a-memberportal.cigna.com/corp/sso/member/forms/memberlogin.fcc';
	//var cons_target = 'https://a-memberportal.cigna.com/corp/sso/ci/member/eai.do';
	//var prov_action = 'https://sso.cigna.com/corp/sso/dlgprovider/forms/providerlogin.fcc';
	//var prov_target = 'https://sso.cigna.com/corp/sso/dlg/provider/secure/controller?command=homepage';
	//var empl_action = 'https://sso.cigna.com/corp/sso/prof/forms/professionallogin.fcc';
	//var empl_target = 'https://portal-accept2.cigna.com/corp/portal/app/employer/secure/jsp/myCIGNAforEmployers.jsp';
	
	//*************** Production **********************
	var cons_action = 'https://my.cigna.com/corp/sso/member/forms/memberlogin.fcc';
	var cons_target = 'https://my.cigna.com/corp/sso/ci/member/eai.do';
	var prov_action = 'https://sso.corp.cigna.com/corp/sso/dlgprovider/forms/providerlogin.fcc';
	var prov_target = 'https://sso.corp.cigna.com/corp/sso/dlg/provider/secure/controller?command=homepage';
	var empl_action = 'https://sso.corp.cigna.com/corp/sso/prof/forms/professionallogin.fcc';
	var empl_target = 'https://sso.corp.cigna.com/corp/sso/professional/secure/controller?command=homepage';
	
	
	// Set the default portal value
	var user_portal = '1';

  // hide error messages if visible
	document.getElementById('userID_errorMessage').style.display = "none";
	document.getElementById('password_errorMessage').style.display = "none";

	// Check to see that a user id was entered
	if (document.frmLogin.USERNAME.value === "") {
		document.getElementById('userID_errorMessage').style.display = "inline";
		return false;
	}

	// Check to see that a password was entered
	if (document.frmLogin.PASSWORD.value === "") {
		document.getElementById('password_errorMessage').style.display = "inline";
		return false;
	}
	
	// Set form action and target value based on which portal the user selected
	if (document.frmLogin.selPortal.value == "1") {
		document.frmLogin.action = cons_action;
		document.frmLogin.TARGET.value = cons_target;
		user_portal="1";
	} else if (document.frmLogin.selPortal.value == "2") {
		document.frmLogin.action = prov_action;
		document.frmLogin.TARGET.value = prov_target;
		user_portal="2";
	} else if (document.frmLogin.selPortal.value == "3") {
		document.frmLogin.action = empl_action;
		document.frmLogin.TARGET.value = empl_target;
		user_portal="3";
	} else {
		alert('A form error was encountered.  Please contact us.');
		return false;
	}
	
	// Set the expiration date for the cookies
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = date.toGMTString();
	
	// Commented out if test and always saving the cookie
	// Write the cookies if the user selected save info or delete cookies if not
	//if (document.frmLogin.saveInfo.checked === true){
		document.cookie = "homeSave=Y; expires="+expires;
		document.cookie = "homePortal="+user_portal+"; expires="+expires;
	//} else {
	//	document.cookie = "homeSave=N; expires=-1";
	//	document.cookie = "homePortal=1; expires=-1";
	//}
	
	// Submit the form	
	document.frmLogin.submit();
	return true;
}

// Function to set the default portal based on the info in the cookie
function setTarget(){
	if (userSave == 'Y'){
		document.frmLogin.selPortal.value = getCookie("homePortal");
		// document.frmLogin.saveInfo.checked = true;
	} else {
		document.frmLogin.selPortal.value = "1";
		// document.frmLogin.saveInfo.checked = false;
	}
}


// Function called when the Provider Directory search form is submitted
function submit_prov_form() {
	// hide error message if visible
	document.getElementById('providerSearch_errorMessage').style.display = "none";
	
	// Check to see if default location and state and zip were never changed
	if (document.dataform.city.value == "" && document.dataform.zip.value == "" && document.dataform.state.value == "0"  ) {
		document.getElementById('providerSearch_errorMessage').style.value = "enter city/state -or- zip code";
		document.getElementById('providerSearch_errorMessage').style.display = "inline";
		return false;
	}
	
		// Check to see if location and state and zip are all empty
	if (document.dataform.city.value == "City" && document.dataform.zip.value == "ZIP Code" && document.dataform.state.value == "0"  ) {
		document.getElementById('providerSearch_errorMessage').style.value = "enter city/state -or- zip code";
		document.getElementById('providerSearch_errorMessage').style.display = "inline";
		return false;
	}


	// Check to see if default state and zip code values were never changed
	if (document.dataform.zip.value == "ZIP Code" && document.dataform.state.value == "0"  ) {
		document.getElementById('providerSearch_errorMessage').style.value = "enter city/state";
		document.getElementById('providerSearch_errorMessage').style.display = "inline";
		return false;
	}

	// Check to see if location and zip code are both empty
	if (document.dataform.zip.value == "" && document.dataform.state.value == "0"  ) {
		document.getElementById('providerSearch_errorMessage').style.value = "enter city/state";
		document.getElementById('providerSearch_errorMessage').style.display = "inline";
		return false;
	}

  //Scrub the default text values
	if (document.dataform.zip.value == "ZIP Code") {
		document.dataform.zip.value = "";
	}
	
	if (document.dataform.city.value == "City") {
		document.dataform.city.value = "";
	}
	
	if (document.dataform.searchname.value == "Last Name") {
		document.dataform.searchname.value = "";
	}	

	// Submit the form	
	document.dataform.submit();
	return true;
}