

function createCookie(name,value,days,path){
	// alert("setting cookie: name["+name+"]value["+value+"]days["+days+"]path["+path+"]");
	if (!path) { path=""; }
	if(days){
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = ";expires="+date.toGMTString();
	}else
		var expires="";
	document.cookie=name+"="+value+expires+";path="+path+";domain=bmgbullion.com";

	return;
}

function readCookie(name){
	var nameEQ=name+"=";
	var value;
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++) {
		var c=ca[i];
		while(c.charAt(0)==' ')
			c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0)
			value=c.substring(nameEQ.length,c.length);
	}
	//alert("reading cookie:"+value);
	if ((name == 'bmgLoginCookie') && (value == undefined)){
		//value ='us_en';
	}
	return value;
}

function checkCookie(value){
        //if (value == '') {
        //   value= readCookie('bmgLoginCookie');
        //}
        //var value2 = readCookie( 'bmgLoginCookie' );
	var text1 ="<p><b>You are currently logged into the ";
	var text2 =" area.</b><br />To log out of your account, <a href='#' onclick=\"delete_cookie_logout('bmgLoginCookie','/','bmgbullion.com'); return false;\">click here</a></p>";
	if (document.cookie.indexOf('bmgLoginCookie')==-1){
                //alert('check_cookie'); 
		var currentPathname = window.location.pathname;
		var pathSplit = currentPathname.split('/');
                
		var my_href = window.location.href;
		var pathSplit2 = my_href.split('&');
		var log_phrase;
		//alert("path name: " +currentPathname + "       pathSplit: "+pathSplit + "          pathSplit[pathSplit.length-2] = "+pathSplit[pathSplit.length-2]);
		//alert("path name: " +currentPathname + "       pathSplit2: "+pathSplit2 + "          pathSpliti2[pathSplit2.length-1] = "+pathSplit2[pathSplit2.length-1]);
                //var current_url = window.location.host + "/" + pathSplit[pathSplit.length-1];
		if (value == 'advisor') {
			path = 'protected_advisor';
			log_phrase = 'advisor only';
		}
		if (value == 'dealer') {
			path = 'protected_dealer';
			log_phrase = 'authorized dealers';
		}
		if ( (pathSplit2[pathSplit2.length-1] == 'pagename=protected_advisor/' && value =='advisor') ||  
		     (pathSplit2[pathSplit2.length-1] == 'pagename=protecteddealer/' &&  value == 'dealer')) {
			createCookie('bmgLoginCookie',value,365);
			if (document.getElementById('logout_option') )   {  
//				document.getElementById('logout_option').innerHTML = text1+value+'s'+text2;
				document.getElementById('logout_option').innerHTML = text1+log_phrase+text2;
				document.getElementById('logout_option').style.display = 'block';   
			}
		}
		var exist = readCookie('bmgLoginCookie');
                if (exist ==  undefined) {
			document.getElementById('logout_option').style.display = 'none';   
                }
	}else{
		//Cookie found so get value ...
		value = readCookie('bmgLoginCookie');
		if (value == 'advisor') {
			log_phrase = 'advisor only';
		}
		if (value == 'dealer') {
			log_phrase = 'authorized dealers';
		}
		//alert('hi cookie:'+value);
		var logout_option2 = text1+log_phrase+text2;
                //alert(logout_option2);
		document.getElementById('logout_option').innerHTML = text1+log_phrase+text2;
		document.getElementById('logout_option').style.display = 'block';   
	}
}
// this deletes the cookie when called
function delete_cookie_logout( name, path, domain ) {
        //alert(name+ path+ domain);
        var value = readCookie( name );
	if ( value ) 
	     //document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
             createCookie(name,"",-1);

	if (document.getElementById('logout_option') )   {  
	     document.getElementById('logout_option').style.display = 'none';
        }
	if (document.getElementById('area') )   {  
	     document.getElementById('area').style.display = 'none';
        }
        var runmode = "http://"+window.location.host+"/"+value+'.pl?rm=logout';
        //alert(runmode); 
        //submit_form(runmode);
         window.location=runmode;
        
}

