//httphostname = "http://localhost/";
//httphostname = 'http://172.16.42.6/';
httphostname = "http://www.snapshock.com/";
serviceUrl = httphostname +"index.php";

function setUserLogin()
{
	localStorage.setItem("user_logged_in", "1"); 
	localStorage.setItem("user_last_login_time", new Date()); 
}

function removeUserLogin()
{
	localStorage.removeItem("user_logged_in");
	localStorage.removeItem("user_last_login_time");
	localStorage.removeItem("user_id"); //xxx does it really needed?
	localStorage.removeItem("session_id"); //xxx does it really needed?
}

function checkUserLogin(){
	try
  	{
	
		var user_logged_in = localStorage.getItem("user_logged_in");
		var user_last_login_time = new Date(localStorage.getItem("user_last_login_time"));
		//console.log('user_last_login_time',user_last_login_time);
		
		var today = new Date();
		//console.log('today',today);
		
		var difference = today - user_last_login_time;
		//console.log('difference',difference);
	
		var days = Math.round(difference/(1000*60*60*24));
		//console.log('days',days);
		
		if (days < 0)  //this shouldn't happen normally, must get hacked, so return false
		{	
			//console.log('day<0');
			return false;
		}
		
		if(user_logged_in == "1" && days < 14){
			
			//console.log('user_logged_in=true');
			return true;
		} else {
			
			//console.log('user_logged_in=false');
			return false;
		}
	
	} catch (err) {
		
		return false;
	}
}

function redirectIfLoggedIn(url){
	if (checkUserLogin()){
		//window.location.href='main.html';
		window.location.href=url;
	}
}

function redirectIfNotLoggedIn(url){
	if (!checkUserLogin()){
		//window.location.href='main.html';
		window.location.href=url;
	}
}

function getUrlVars()
{
    var vars = [], hash;
	var hasHashes = window.location.href.indexOf('#') > 0;
	//var url = hasHashes?  window.location.href.slice(window.location.href.indexOf('#')): window.location.href;
    //alert(url);
	var url = hasHashes? window.location.href.substring(0, window.location.href.indexOf('#')): window.location.href;
	//alert(url);
	var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = decodeURI(hash[1]);
    }
    return vars;
}

function getLastArg(url) {
	return url.substring(url.lastIndexOf("/"));
}

function isNotNull(myVar){
    if(typeof(myVar) !== 'undefined' && myVar !== null && myVar !== '') {
        return true;
    }
    return false;
}
