// JavaScript Document

//SET UP USER

function obj_userEntity(){

		this.userID=null;
		this.userName=null;
		this.userPassword=null;
		this.userLevelID=null;
		this.userTypeID=null;
		this.userCountryID=null;
		this.userStatus=null;
		this.areaConversion=null;
		this.lengthConversion=null;
		this.userSelectAction=null;
		this.userSelectGroup=null;
		this.userSelectObject=new obj_userSelectObject;
		
		this.userEntityLoad=userEntityLoad;
		this.userEntityOutput=userEntityOutput;
		this.userEntityStore=userEntityStore;
		
		}

function obj_userSelectObject(){
	
	this.entityID=null;
	this.objectClass=null;
	this.type=null;
	this.weight=null;
	this.color=null;
	this.opacity=null;
	this.markers=new Array();
	this.latLngs=new Array();
	this.overLay=null;
	this.area=null;
	this.xLength=null;
	this.yLength=null;
	
	}

function userEntityLoad() {
  
  var selected_cookie = WM_readCookie("mssbconsulting_userDetails");
  var request = null;	
  
  if(selected_cookie) {
	
	this.userID=parseInt(read_cookie (selected_cookie, 0));
	this.userLevelID=parseInt(read_cookie (selected_cookie, 1));
	this.userCountryID=parseInt(read_cookie (selected_cookie, 4));
	this.userTypeID=parseInt(read_cookie (selected_cookie, 5));
	
	this.userName=read_cookie (selected_cookie, 2);
	this.userPassword=read_cookie (selected_cookie, 3);
	
	this.userSelectGroup=null;
	this.userSelectAction=null;
	this.userSelectObject.objectClass=null;
	
	
	if(userEntity.userCountryID==287) {
		
		this.areaConversion=0.0002471;
		this.lengthConversion=0.9144;
		this.areaUnits="ac";
		this.lengthUnits="yd";
	
		} else{
	
		this.areaConversion=0.0001;
		this.lengthConversion=1;
		this.areaUnits="ha";
		this.lengthUnits="m";
	
		}
	
	var post_string="str_userName=" + userEntity.userName + "&str_passWord=" + userEntity.userPassword + "&int_userLevelID=" + userEntity.userLevelID + "&int_userID=" + userEntity.userID + "&int_countryID=" + userEntity.userCountryID + "&int_userTypeID=" + userEntity.userTypeID;
	var url = "/functions/set_sessionVaribles.php";
	var connect = ajax_session_conn_post(url, post_string);
		
	} else {
		
		if(window.name != "home_page_window") {
			
			self.location = "/index.html";
		
			}
			
		}
  
	}

function userEntityOutput(){
	
	//menuEntity.menuEntityLoad();
	//menuEntity.menuEntityOutput();
	//menuEntity.menuEntitySetClass(1);
	
	bodyEntity.bodyEntityLoad();
	bodyEntity.bodyEntityOutput();
	
	}


function userEntityStore(){
	
	var str_cookieText="int_userID:" + this.userID + "/int_userLevelID:" + this.userLevelID + "/str_userName:" + this.userName + "/str_passWord:" + this.userPassword + "/int_countryID:" + this.userCountryID + "/int_userTypeID:" + this.userTypeID;
	var expiry = new Date();
	expiry.setTime( expiry.getTime() + (7 * 24 * 60 * 60 * 1000)); 
	var cookie_path = "path=/;";
	window.document.cookie="mssbconsulting_userDetails=" + escape(str_cookieText) + ";" + cookie_path + "expires=" + expiry.toGMTString() + ";";
	
	}
	

function WM_readCookie(name) {


	if (document.cookie == '') {

		//alert(1);
		//alert("there's no cookie, so go no further");
		return false;


		} else {

		// there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);


		// find the start of 'name'
		if(firstChar != -1) {
		
		// if you found the cookie
		firstChar += name.length + 1;

		// skip 'name' and '='
		lastChar = theBigCookie.indexOf(';', firstChar);
		
		// Find the end of the value string (i.e. the next ';').
		if(lastChar == -1) lastChar = theBigCookie.length;
		
			return unescape(theBigCookie.substring(firstChar, lastChar));
			
			} else {

			// If there was no cookie of that name, return false.
			return false;

			}

		}

	}


function read_cookie (selected_cookie, array_index) {

	var cookie_info = unescape(selected_cookie);
	var user_info_array = cookie_info.split(";");
	var user_info_data = user_info_array[0].split("=");
	var user_data_array = user_info_data[0].split("/");
	if(user_data_array[array_index]){
			
		var user_data_array=user_data_array[array_index].split(":");
		return(user_data_array[1]);
			
		}else{ 
			
		switch (array_index){
			
			case 4:
				return(183);
			break;
			
			case 5:
				return(1);
			break;
			
			}
		
		}
	
	}


function ajax_session_conn_post(url, post_string) {
 	 
  //alert("int_primaryEntityID " + int_primaryEntityID);	 
  var request = null;
  	
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  } else {
    request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  if (request) {
    request.open("POST", url);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    request.send(post_string);
	
	request.onreadystatechange = function() {

		}
  
  } else {
    alert("Sorry, you must update your browser before seeing" +
      " Ajax in action.");
  }

}
