// JavaScript Document

// to Getthe value of a cookie
function getCookie(name){	
	if(document.cookie){
		var cookies = document.cookie.split( ';' );		
		for ( i = 0; i < cookies.length; i++ )
		{
			var values = cookies[i].split("=");	
			cookie_name = values[0].replace(/^\s+|\s+$/g, '');				
			if ( cookie_name == name )
			{					
				// to handle case where cookie exists but has no value
				if ( values.length > 1 )
				{
					cookie_value = unescape( values[1].replace(/^\s+|\s+$/g, '') );
				}
				else{
					deleteCookie(name,"/");
					cookie_value = null;
				}
				return cookie_value;
			}
			values = null;
			cookie_name = '';
		}		
	}
	return null;

}

// to delete a cookie
function deleteCookie( name, path, domain ) {
	if ( document.cookie ) {
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 09-Jan-1985 00:00:01 GMT";
	}
}



//stores the font size in cookie to apply to content in other pages also
function setSize(size){	
	deleteCookie("font-select","/");
	document.cookie = "font-select="+size+"; path=/"
	applyFontSize();
}

//retreives the font size in cookie and apply to content pages
function applyFontSize(){
	var fontSize=getCookie("font-select");
	if(fontSize){
		
		document.getElementById("content").style.fontSize = fontSize+"px";
		
	}
	else{
		document.getElementById("content").style.fontSize = "12px";
		
	}
}


//Required Global Variables
var host = window.location.host;
var completeUrl = window.location.href;

var states;

var prgm= getPrgmValue();

function getPrgmValue(){ //check for medicaid/medicare to change swap state name
	if(completeUrl.indexOf("/medicaid/")>1)
		return "mcaid";
	else if(completeUrl.indexOf("/medicare/")>1)
		return "mcare";
	else
	return getUrlParam("prgm");        
}

if(prgm=='mcaid'){
	states = { 	
				'ca'	:	'California', 
				'mi' 	: 	'Michigan',
				'mo'	:	'Missouri',
				'fl'	:	'Florida', 
				'oh'	:	'Ohio',				
				'nm'	:	'New Mexico',
				'tx'	:	'Texas',
				'ut'	:	'Utah',
				'wa'	:	'Washington'
		}
}
else{
	states = { 	
				'ca'	:	'California',				
				'mi' 	: 	'Michigan',					
				'nv'	:	'Nevada',
				'nm'	:	'New Mexico',
				'tx'	:	'Texas',
				'ut'	:	'Utah',
				'wa'	:	'Washington'
			}
}
var currentStateId;

//Sets the State value in a cookie
function setStateCookie(selectedStateId){
	deleteCookie("state","/");
	document.cookie = "state="+selectedStateId+"; path=/"
}

function getStateValue(){
	var i;
	for(i in states)
	{
		if(completeUrl.indexOf("/"+i+"/")>1 || completeUrl.indexOf("/"+i+"_")>1)
		{
			return i;
			break;
		}
	}
	return getUrlParam("state");	
}

function getUrlParam(variable){
	 
	  if ( completeUrl.indexOf("?") > -1 ){
			var paramString = completeUrl.substr(completeUrl.indexOf("?")).toLowerCase();
			var params = paramString.split("&");
			for ( var i = 0; i < params.length; i++ ){
				  if (params[i].indexOf(variable.toLowerCase() + "=") > -1 ){
						var values = params[i].split("=");
						return values[1];
			 	 }
			}
	  }
	  return null;
}


//Shows the current state in state Selection menu
function setState()
{
	var i=getStateValue();
	document.getElementById('currentState').innerHTML=(i)?states[i]:"Select State";
	populateStatesMenu();
}

// populates state selection menu in header			
function populateStatesMenu()
{		
	document.getElementById('stateMenu').innerHTML="";	
	for (i in states){
		if(document.getElementById('currentState').innerHTML.toString()==states[i].toString()){
			currentStateId=i;
			continue;
		}
		else if(document.getElementById('currentState').innerHTML.toString()=="Select State"){			
			currentStateId="HomePage";
		}
			
		var menuItemDiv = document.createElement('DIV');
		menuItemDiv.id = i;		
		menuItemDiv.onmouseover=function(){setBackground(this)};
		menuItemDiv.onmouseout=function(){removeBackground(this)};
		menuItemDiv.onclick=function(){changeState(this)};
		//menuItemDiv.onmouseover=showStateMenu;
		var menuItemText = document.createTextNode(states[i]);
		menuItemDiv.appendChild(menuItemText);
		document.getElementById('stateMenu').appendChild(menuItemDiv);
	}
	
}


function setBackground(menuItemDiv)
{
	menuItemDiv.style.backgroundColor="#e5f4f7";	
	showStateMenu();
}
function removeBackground(menuItemDiv)
{
	menuItemDiv.style.backgroundColor="#ffffff";
	showStateMenu();
}
//changes the state context and opens appropriate state home page
function changeState(selectedState)
{	
	selectedState.style.backgroundColor="#0098aa";
	var selectedStateId = selectedState.id;
	setStateCookie(selectedStateId);
	hideStateMenu();
	host="";
	if(completeUrl.indexOf("/common/")>=0){
		host=completeUrl.slice(0,completeUrl.indexOf("/common/"));
	}
	else if(completeUrl.indexOf("/"+currentStateId+"_")>1){
		host=completeUrl.slice(0,completeUrl.indexOf("/"+currentStateId+"_"));	
	}
	else if(completeUrl.indexOf("mmcare_home.html")>=0) {
		host=completeUrl.slice(0,completeUrl.lastIndexOf("/"));
	}			
	else{		
		host=completeUrl.slice(0,completeUrl.indexOf("/"+currentStateId+"/"));		
	}
	window.location.href=host +"/"+selectedStateId+"/"+"home.html";
}

function goToState(goButton)
{		
	var selectedStateId = document.getElementById('state')[document.getElementById('state').selectedIndex].value;
	//Alert message if state is not selected
	var sltuseRadio = document.getElementsByName('sltuse');
	var sltuse='';
	for(var i=0; i<sltuseRadio.length; i++)
	{
		if(sltuseRadio[i].checked){
			sltuse = sltuseRadio[i].value;
		}
	}
	if(sltuse=='medicaid') {
		if(selectedStateId=='0'){
		alert("Please select any state to navigate.");
		return false;
		}
	}
	setStateCookie(selectedStateId);
	host=completeUrl.slice(0,completeUrl.lastIndexOf("/"));
	//check if the selection is for providers
	var query = window.location.search.substring(1);	
	var val = query.substring(query.indexOf('=')+1);
	if(val=='true')
	goButton.href=host +"/"+sltuse+"/"+"providers"+"/"+selectedStateId+"/"+"home.html";
	else if(sltuse=='medicaid')	
	goButton.href=host +"/"+sltuse+"/"+"members"+"/"+selectedStateId+"/"+"home.html";
	else
	goButton.href="http://www.molinamedicare.com";	
	//goButton.href=host +"/"+sltuse+"/"+selectedStateId+"/"+"home.html";	
	
}


//hides state menu 
function hideStateMenu()
{
	document.getElementById('stateMenu').style.visibility="hidden";
	document.getElementById('stateSelection').style.backgroundColor="#FFFFFF";
}

//shows state menu 
function showStateMenu()
{
	document.getElementById('stateMenu').style.visibility="visible";
	document.getElementById('stateSelection').style.color="#0098aa";

}

//Redirects url to appropriate state from common pages and home page
function redirectToState(currentMenuItem)
{
	var currentUrl = currentMenuItem.href;
	var redirectedUrl;
	if(currentUrl.indexOf("/common/")>=0){					
		redirectedUrl=currentUrl.replace("/common/","/"+currentStateId+"/");
	}
	else if(completeUrl.indexOf("home.html")>=0){
		redirectedUrl=currentUrl.replace("/ca/","/"+currentStateId+"/");		
	}
	else{
		redirectedUrl=currentUrl;
	}
	currentMenuItem.href=redirectedUrl;
}


//expand and collapse functionality
function showhide(paraobj,sign)
{   var relPath=buildRelativePath();
	var flag = document.getElementById(paraobj).style.display;	
	if (flag == 'none'){
	document.getElementById(paraobj).style.display = "";
	document.getElementById(sign).src=relPath+"images/treeminus.gif";
	}
	else{
	document.getElementById(paraobj).style.display = "none";
	document.getElementById(sign).src=relPath+"images/treeplus.gif";
	}
} 

//to buid relative path depending up on depth of path from root folder("Medicare")
function buildRelativePath(i)
{
	//when hosted on server	
	var relPath = "";	
	//relPath="http://mhcalbiwebdv01:8090/";
   	//relPath="http://www.molinahealthcare.com/";	
	relPath="http://"+host+"/";
   	return relPath; 	
	
	//when hosted on file system (local machine)	
	/*
	var relPath = "";
	if(!i){
		i=getDepthFromParent();
	}
	for(;i>1;i--){
		relPath = relPath+"../";
	}
	return relPath;*/
	
}

// to get depth of path from the root folder("Medicare")
function getDepthFromParent()
{
	var completeUrl = window.location.href;
	var splitUrl=completeUrl.split("/");
	for(i=0;i<splitUrl.length;i++){
		if(splitUrl[i]=="Medicare" || splitUrl[i]=="Molina"){
			break;
		}
	}
	return splitUrl.length-i-1;
}

function confirmLink()
{
	 bConfirm=confirm("You are now leaving the Molina Healthcare Website.  Any links to other non-Molina web sites are solely for your convenience in obtaining related information. Such inclusion or mention does not constitute or imply a recommendation or endorsement by Molina. Molina does not control the contents of these web sites and does not guarantee the accuracy or completeness of any such information.")
	 return (bConfirm);
}

function goPharm(){ // Change find pharmacy link in header, based on state for Medicare	
host=completeUrl.slice(0,completeUrl.indexOf("/"+currentStateId+"/"));
window.open(host +'/common/find_pharm.html','FindPharm','width=800,height=600,scrollbars=yes');		
}

function openPharm(){
	var pharm = document.getElementById('pharm').selectedIndex;	
	switch (pharm){
	case 0:
	alert("Please select state.");
	break;
	case 1:	
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL1&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');	
	break;
	case 2:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL1&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 3:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC4&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 4:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL4&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');		
	break;
	case 5:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 6:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');	
	break;
	case 7:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 8:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 9:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 10:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 11:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 12:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL2&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;
	case 13:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINALTC3&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;	
	case 14:
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL3&amp;page=pharmacyonly','','width=800,height=600,scrollbars=yes');
	break;	
	default:		
	}
}

function findPharm(){ // Change find pharmacy link in header, based on state
	var state = document.getElementById('currentState').innerHTML;
	host=completeUrl.slice(0,completeUrl.indexOf("/"+currentStateId+"/"));	
	if(state=="New Mexico")
	{
	if (confirmLink())
	window.open('https://www.catalystrx.com/PharmacyDirectory/pharmacies.jsp?state=NM','','width=800,height=600,scrollbars=yes');	
	}
	else
	{
	if (confirmLink())
	window.open('https://butch.rxamerica.com/RxWeb/autoLogin.do?name=MOLINAGENERAL1&page=pharmacyonly','','width=800,height=600,scrollbars=yes');	
	}
}

function strt(tname) //For AbtMolina contact us left Nav
{
	cnt=tname;
	test=cnt;
}
function table_change(tab_name)
{
	t_prev=document.getElementById("table_"+cnt);
	t_now=document.getElementById("table_"+tab_name);
	prev=document.getElementById(cnt);
	now=document.getElementById(tab_name);
	t_prev.style.display="none";	
  if(test=="CO")
	{
    prev.className="leftmenu_inactive";
	now.className="leftmenu_active";
	}
	t_now.style.display="block";
	cnt=tab_name;	
}

function addOption(selectbox,text,value ) //this function populate the dropdown values
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}


// This function swap the state of Nevada & Ohio while selecting Medicare & Medicaid members

function swapState(){	
	
	var sltuseRadio = document.getElementsByName('sltuse');
	var sltuse='';	
	for(var i=0; i<sltuseRadio.length; i++)
	{
		if(sltuseRadio[i].checked){
			sltuse = sltuseRadio[i].value;
		}
	}
	// set default
	//document.getElementById('state').options.selectedIndex=0;
	//set for providers
	var query = window.location.search.substring(1);	
	var val = query.substring(query.indexOf('=')+1);	
	if(val=='true'){		
	document.getElementById('heading').innerHTML="Select State for Providers";	
	document.getElementById('prgm').style.display="none";
	}	
	//populate state dropdown based for medicaid & medicare
	if(sltuse =='medicaid'){	
		document.getElementById('stateRow').style.visibility = "visible";
			document.getElementById('state').options.length=1; //clear options before populating
		var stateVal=new Array('ca','fl','mi','mo','oh','nm','tx','ut','wa');		
		var stateTxt = new Array('California','Florida','Michigan','Missouri','Ohio','New Mexico','Texas','Utah','Washington');			
		//var stateVal=new Array('ca','mi','mo','oh','nm','tx','ut','wa');		
		//var stateTxt = new Array('California','Michigan','Missouri','Ohio','New Mexico','Texas','Utah','Washington');			
		
		for(var i=0; i<stateVal.length; ++i){			
		addOption(document.getElementById('state'), stateTxt[i], stateVal[i]);}		
	}
	else{
		document.getElementById('state').options.length=1;//clear options before populating
		var stateVal=new Array('ca','mi','nv','nm','tx','ut','wa');		
		var stateTxt = new Array('California','Michigan','Nevada','New Mexico','Texas','Utah','Washington');			
		for(var i=0; i<stateVal.length; ++i){			
		addOption(document.getElementById('state'), stateTxt[i], stateVal[i]);}	
		document.getElementById('stateRow').style.visibility = "hidden";
	}
	
}

//to append state param in all edi files
function appendState(path){
	var query = window.location.search.substring(1);	
	var val = query.substring(query.indexOf('=')+1);		
	path.href=path+"?state="+val;	
}

function memberLink(){
	 alert("You would now be navigating to Molina Members section.");
}
function providerLink(){
	 alert("You would now be navigating to Molina Providers section, to get more information on HIPAA.");
}


function chkval(form,reqFields) { // Required fields validation
var length=reqFields.length;
for (var i=0; i<length; i++) {
	if(document.getElementById(reqFields[i]).value ==''){
		alert("Please enter value for all required fields");
		document.getElementById(reqFields[i]).focus();
		return false;
	}
 }
 return validateForm();
}



function randomString() {
	        var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	        var string_length = 5;
	        var randomstring = '';
	        for (var i=0; i<string_length; i++) {
		        var rnum = Math.floor(Math.random() * chars.length);
		        randomstring += chars.substring(rnum,rnum+1);
	    }
	   
	    //document.form.Captcha.value = randomstring;
	    var ctlCap = document.getElementById("spnCaptcha");
	    ctlCap.innerText = randomstring;
	    //alert(document.form.Captcha.value);
	    
	    }
        function validateForm() {
            var ctlCap = document.getElementById("spnCaptcha");
	        var ctlInputCap = document.getElementById("txtInputCap");
	        
            if (ctlCap.innerText == ctlInputCap.value)
            return true;
            else
                alert("The code entered doesn't match.Enter the code as shown ");
            return false;
        }
        
        


function findState(){
	var i;
	for(i in states){
		if(completeUrl.indexOf("/"+i+"/")>1 || completeUrl.indexOf("/"+i+"_")>1)
		{   return i;
			break;	}
	}	
}



function changeText(obj) {
	
	obj.value='';
	
}
function HideFacilityReview()
{
var j=getStateValue();
	document.getElementById('currentState').innerHTML=(j)?states[j]:"Select State";
	for (i in states){ 
		if(document.getElementById('currentState').innerHTML.toString()==states[i].toString()){
			currentStateId=i;
			continue;
		}
		else if(document.getElementById('currentState').innerHTML.toString()=="Select State"){			
			currentStateId="HomePage";
		}
		
		}
		return currentStateId;
        }