﻿var ckeNameS = 'itzbigseekcke';
var ckeNameR = 'itzbigreccke';
var daysCke = 15;
var sepCke = '<&>';
//var flagAutoCke = false;

function chkCkeOnLoad(from,ckeName,formName)
{
    var f = document.forms[formName];
    
    var ckeValue = '';		
	if(from == 'S')
	{
	    ckeValue = readCke(ckeName);	
	    if(ckeValue != '' )
	    {
	        var splitCke = ckeValue.split(sepCke);
	        email = splitCke[0]; 
	        f.email.value = email;   	    
            pwd = splitCke[1];   
            f.password.value = pwd;   
            
            return f;  
	    }
	    
	 }
	 else if(from == 'R')
	 {
	    ckeValue = readCke(ckeName);	
	    if(ckeValue != '' )
	    {
	        var splitCke = ckeValue.split(sepCke);
	        email = splitCke[0];  
	        f.email.value = email;   	      	    
            pwd = splitCke[1];  
            f.password.value = pwd;  
            return f;      
	    }
	    
	 }
	 
	 return null;
}	 
	

function checkCke(ckeName,chkID,email,pwd)
{
    var ckeValue = readCke(ckeName);
    if(ckeValue == '' )
	{
	        setCke(ckeName,email + sepCke + pwd,daysCke);	  
	}else
	{	
	    //alert('cookie Found');    
	    if($(chkID)!= null && $(chkID).checked == false  )
	    {
	        removeCke();	        
	    }else
	    {
	        var splitCke = ckeValue.split(sepCke);
	        var emailCke = splitCke[0];  
	        if(emailCke != email)
	        {
	            setCke(ckeName,email + sepCke + pwd,daysCke);	            
	        }
	        
	    }
	}
}

function readCke(name) 
{
      var nameSG = name + "=";
      var nuller = '';
      if (document.cookie.indexOf(nameSG) == -1)
        return nuller;

      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(nameSG) == 0) return decodeCke(c.substring(nameSG.length,c.length)); 
      }
    return nuller; 
}

function setCke(name,value,days) 
{             
  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 +"="+ encodeCke(value) + expires+";domain=itzbig.com; path=/"; 
   //alert('cookie Set');
 }
 
 function removeCke()
 {
    setCke(ckeName,'',-1);
    //alert('cookie Deleted');
 }
 
 function encodeCke(ckevalue) {

  // This variable holds the encoded cookie characters
  var coded_string = ""
  
  // Run through each character in the cookie value
  for (var counter = 0; counter < ckevalue.length; counter++) {
  
    // Add the character's numeric code to the string
    coded_string += ckevalue.charCodeAt(counter)
    
    // Separate each code with a plus sign (+)
    if (counter < ckevalue.length - 1) {
      coded_string += "+"
    }
  }
  return coded_string
}


function decodeCke(coded_string) {

  // This variable holds the decoded cookie value
  var ckevalue = ""
  
  // Use + to split the coded string into an array
  var code_array = coded_string.split("+")
  
  // Loop through the array
  for (var counter = 0; counter < code_array.length; counter++) {

    // Convert the code into a character and 
    // add it to the cookie value string
    ckevalue += String.fromCharCode(code_array[counter])
  }
  return ckevalue
}
