// JavaScript Document

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function validateEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1)  return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)   return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)  return false;
	if (str.indexOf(at,(lat+1))!=-1)   return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)   return false;
	if (str.indexOf(dot,(lat+2))==-1)    return false;	
	if (str.indexOf(" ")!=-1)    return false;
 	return true;					
 }
 
function createCookie(name,value,days,domain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	if (domain) {
		var domainstr = "; domain="+domain;
	}
	else var domainstr = "";
	document.cookie = name+"="+value+domainstr+expires+"; path=/";
	//alert('cookieset');
}

function getCookie(c_name) {
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + "; path=/";
}

function checkTheme() {
	theme = getCookie("THEME");
	alert(theme);
}


function ajaxKeepLoggedIn(){
	
	var s = '&ajax=1';
  	$("#keeper").html("Keeping you logged in");
	$.ajax({
		type: "POST",
		url: "/assets/ajax/v3/keeploggedin.cfm",
		data: s,
		success: function(data){
			KeepLoggedInSuccess(data);
		},
		error: function(data){
			KeepLoggedInError(data);
		},
		timeout: 30000
	});
};

function KeepLoggedInSuccess(data) {
  r = jQuery.trim(data);
  //$("#keeper").html(r);
  $("#keeper").html('');
  //$.dump(data);
}

function KeepLoggedInError(data) {
  //r = data.trim();
  //$("#loginResponse").html(r);
  //$.dump(data);
}

// prepare the form when the DOM is ready 
$(document).ready(function() { 
	
	if (getCookie("KEEPLOGGEDIN") == 1) {
		
		//alert('keeping you logged in');
		KLItimer = function() {
			ajaxKeepLoggedIn();
			setTimeout(KLItimer,300 * 1000);
		}
		setTimeout(KLItimer,300 * 1000);
	}
	
}); 