/*
	#############################################################################
	# eLuminous Technologies - Copyright (C)  http://eluminoustechnologies.com 
	# This code is written by eLuminous Technologies, Its a sole property of 
	eLuminous Technologies and cant be used / modified without license.  
	Any changes/ alterations, illegal uses, unlawful distribution, copying is strictly
	prohibhited #
	#############################################################################
	
	# Name: functions.js
	# Usage: included ( WRITE USAGE DETAILS ) 
	  {
	   -- It is called from header.php
	   -- Contains javascript functions for Ajax handling.
	  } 
	# Update: created on 1-Feb-2007 by swati patel
	# Update: Abhijeet Bagul
	# Status: production
	# Purpose: Ajax handling function
	 
	#############################################################################
	ALSO STRICTLY MAINTAINING THE LOGS OF CHANGES AND PERSON NAME 
	#############################################################################

	1. function checkOthers(val)
	2. function hideNshowSections(key)
	3. function makeFieldEmpty(obj)
	4. function change_cursor(obj)
	5. function applyDefaultText(obj)
	6. function processLogin()
	7. function show_popup(page_url)
	8. function processAgentLogin()
	
	/**
	* ###############################################
	* USER PANEL FUNCTIONS
	* ###############################################
	*/
	
	function checkOthers(val, key)
	{
		switch(key)
		{
			case "state" :
							obj = document.getElementById("txt_company_state");
							
							if(val == "others")
							{
								obj.style.visibility="visible";
								obj.style.display="inline";
								obj.value="Please enter state.";
							}//-- if.
							else
							{
								obj.style.visibility="hidden";
								obj.style.display="none";
							}
							
							break;
			
			case "city" :
							obj = document.getElementById("txt_company_city");
							
							if(val == "others")
							{
								obj.style.visibility="visible";
								obj.style.display="inline";
								obj.value="Please enter city.";
							}//-- if.
							else
							{
								obj.style.visibility="hidden";
								obj.style.display="none";
							}
							
							break;
		}//-- switch.
	}
	
	function hideNshowSections(key, currObj, divVal)
	{
		obj = document.getElementById(divVal);
	
		if(key == 'show')
		{
			obj.style.visibility='visible';
			obj.style.display='inline';
			
			currObj.innerHTML = "-";
			currObj.onclick=
			function()
			{
				hideNshowSections('hide', currObj, divVal)
			}
			;
		}
		else
		if(key == 'hide')
		{
			obj.style.visibility='hidden';
			obj.style.display='none';
			
			currObj.innerHTML = "+";
			currObj.onclick=
			function()
			{
				hideNshowSections('show', currObj, divVal)
			}
			;
		}
	}
	
	function makeFieldEmpty(obj)
	{
		if(obj.value == obj.title)
			obj.value = "";
		else
			obj.select();
	}
	
	function change_cursor(obj)
	{	 
		obj.style.cursor='pointer';
	}
	
	function applyDefaultText(obj)
	{
		if(obj.value == obj.title || obj.value == '')
			obj.value = obj.title;
	}
	
	function processLogin()
	{
		if(validateLogin(document.frmLogin))
		{
			//show loading image
			var obj_img_div = browserObject('div_progressbar_img');
			obj_img_div.style.visibility = "visible";
			obj_img_div.style.display = "block";
				
			//hide msg div
			var obj_msg_div = browserObject('div_msg');
			obj_msg_div.style.visibility = "hidden";
			obj_msg_div.style.display = "none";
			
			//send ajax request
			var url = 'user_login_ajx.php?mXYef=' + doRand();
			var actionName = 'usr_processLogin';
			var targDiv = obj_msg_div;
			var parameters = "btn_login=Login&txt_username=" + document.frmLogin.txt_username.value + 
							  "&txt_password=" + document.frmLogin.txt_password.value + 
							  "&chk_remember=" + (document.frmLogin.chk_remember.checked)*1;

			load_ajax_request(url, targDiv, actionName, parameters);
		}
		else
		{
			return false;	
		}
	}
	
	function show_popup(page_url)
	{
		var width = 700;
		var height = 370;
		var left = (screen.width - width) / 2
		var top = (screen.height - height) / 2
		
		winPopup = window.open(page_url, "", "left=" + left + ",top=" + top + ",height=" + height + ",width=" + width + ",scrollbars=0,directories=0,location=0,menubar=0,resizable=1,status=0,titlebar=0,toolbar=0");
	}
	

function browserDetection()
{
		var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();
	//alert(BrowserDetect.browser + ' ' + BrowserDetect.version + '  ' + BrowserDetect.OS);
	return BrowserDetect.browser;
}
function getOuterHTML_moz(id)
{
	obj = document.getElementById(id);
   if (document.body.__defineGetter__)
   {
    	if (HTMLElement)
		{
			  var element = HTMLElement.prototype;
			  
			  if (element.__defineGetter__)
			  {
				  var t  = element.__defineGetter__("outerHTML",
				  function () {
								  var parent = obj.parentNode;
								  //alert(obj.parentNode +" "+ parent.tagName)
								  var el = document.createElement(parent.tagName);
								  el.appendChild(obj);
								  var shtml = el.innerHTML;
								  
								  //parent.appendChild(this);
								  return shtml;
				   
				  }
				   
				  );
				   
			  }
		   
		}
       
   }
   
 }
function loadCounty(type, val, name,div_name, str_url, obj, selVal)
{
	if(obj == true)
	{
		//alert(div_name);
		obj_County = document.getElementById("id_county");
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id',div_name);
		//newdiv.innerHTML = "id_cities" + subArr[i];
		obj_County.appendChild(newdiv);
		
str_url= str_url + "?id=" + val + "&name=" + name + "&type=" + type + "&chk_commasep_desired_county=" + selVal;							
{
			var status = AjaxRequest.submit
			(
				str_url
				,{
					'onSuccess':function(req)
						{
							//alert(req.responseText);
							var resp = trim(req.responseText);
							
							document.getElementById(div_name).innerHTML = resp;
							
						}
				}
			);	
}
		
		
		//getData('POST',str_url,"id=" + val + "&name=" + name + "&type=" + type, div_name,0);
	}
	else
	{
		document.getElementById(div_name).innerHTML = "";
	}
}
 
function loadDesiredCounty(type, val, name,div_name, str_url, str_SelectedCounties, str_SelectedCities, str_SelectedCountiesNames)
{
	var pureFileName = str_url;
	// -- made display cities count to '0' and hide the select all checkox.
	document.getElementById("hid_tot_displed_cities").value="0";
	document.getElementById("id_desired_cities_all").style.visibility = "hidden";
	document.getElementById("id_desired_cities_all").style.display = "none";
	
	// -- Make empty all cities.
	obj_City = document.getElementById("id_city");
	obj_City.innerHTML = "";
	
	// -- Make empty all county.
	obj_City = document.getElementById("id_county").innerHTML = "";
	
if(trim(str_SelectedCounties))
{
// -- Comama sep couties
var str_SelectedCommaSepCoutiesIds = base64_decode(str_SelectedCounties);
arr_SelectedCounties =  str_SelectedCommaSepCoutiesIds.split(",");
var int_SelectedCountiesCnt = arr_SelectedCounties.length;

// -- Comama sep couties names
arr_SelectedCountiesNames =  str_SelectedCountiesNames.split(",");
}

	
	if(val)
	{
		document.getElementById("id_city").innerHTML= "";
		str_url= str_url + "?id=" + val + "&name=" + name + "&type=" + type + "&selectedvals=" + str_SelectedCounties;
		//alert(str_url);
		{
			var status = AjaxRequest.submit
			(
				str_url
				,{
					'onSuccess':function(req)
						{
							//alert(req.responseText);
							var resp = trim(req.responseText);
							var arr = resp.split("##########@@@@@$@@@@@##########");
							var listLen = arr.length;
							
							document.getElementById(div_name).innerHTML = arr[0];
							
							if(trim(arr[1]) != "")
							{
								obj_City = document.getElementById("id_city");
								var subArr = arr[1].split(",");
								var listSubLen = subArr.length;
								
								//alert(listSubLen);
								for(var i=0; i<listSubLen; i++)
								{
									//alert(subArr[i]);
									var newdiv = document.createElement('div');
									newdiv.setAttribute('id',"id_cities" + subArr[i]);
									//newdiv.innerHTML = "id_cities" + subArr[i];
									obj_City.appendChild(newdiv);
								}//-- for.
								

for(var k=0;k<int_SelectedCountiesCnt; k++)
{
	//alert(arr_SelectedCounties[k] + " " + str_SelectedCities);
	loadSelectedDesiredcities('cities', trim(arr_SelectedCounties[k]), arr_SelectedCountiesNames[k], 'id_cities' + trim(arr_SelectedCounties[k]), pureFileName, str_SelectedCities);
}
							}
							
						}
				}
			);	
		}
	}//-- if
}
//str_MyBrowserType = browserDetection();
function loadDesiredcities(type, val, name,div_name, str_url, obj)
{
	var obj_TotCities = document.getElementById("hid_tot_displed_cities");
	var val_Tot_Cities = parseInt(obj_TotCities.value);
	
	if(obj.checked == true)
	{
		document.getElementById("id_desired_cities_all").style.visibility = "visible";
		document.getElementById("id_desired_cities_all").style.display = "inline";
		
		obj_TotCities.value = val_Tot_Cities + 1;

		//getData('POST',str_url,"id=" + val + "&name=" + name + "&type=" + type, div_name,0);
		str_url= str_url + "?id=" + val + "&name=" + name + "&type=" + type;
		{
					var status = AjaxRequest.submit
					(
						str_url
						,{
							'onSuccess':function(req)
								{
									//alert(req.responseText);
									var resp = trim(req.responseText);
									document.getElementById(div_name).innerHTML = resp;
								}
						}
					);	
		}
	}
	else
	{
		document.getElementById(div_name).innerHTML = "";
		if(val_Tot_Cities > 0)
			obj_TotCities.value = val_Tot_Cities - 1;
	}
	
	if(obj_TotCities.value < 1)
	{
		document.getElementById("id_desired_cities_all").style.visibility = "hidden";
		document.getElementById("id_desired_cities_all").style.display = "none";
	}
}

function loadSelectedDesiredcities(type, val, name,div_name, str_url, str_SelectedCounties)
{
	
	//alert(str_url);
	var obj_TotCities = document.getElementById("hid_tot_displed_cities");
	var val_Tot_Cities = parseInt(obj_TotCities.value);
	
	{
		document.getElementById("id_desired_cities_all").style.visibility = "visible";
		document.getElementById("id_desired_cities_all").style.display = "inline";
		
		obj_TotCities.value = val_Tot_Cities + 1;
		
		//getData('POST',str_url,"id=" + val + "&name=" + name + "&type=" + type + "&selectedvals=" + str_SelectedCounties, div_name,0);
str_url= str_url+ "?id=" + val + "&name=" + name + "&type=" + type + "&selectedvals=" + str_SelectedCounties;	
{
			var status = AjaxRequest.submit
			(
				str_url
				,{
					'onSuccess':function(req)
						{
							//alert(req.responseText);
							var resp = trim(req.responseText);
							
							document.getElementById(div_name).innerHTML = resp;
							
							if(document.getElementById("name_gmap"))
								addCitiesToGoogleMap(document.frmClient.chk_advance_desired_citites);
							
						}
				}
			);	
}
		
	}
	var str_url = "";
}

function base64_decode( data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
    
    // mozilla has this native 
    // - but breaks in 2.0.0.12!
    //if (typeof window['btoa'] == 'function') {
    //    return btoa(data);
    //}
    
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
    
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
    
    return dec;
}

function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c = c1 = c2 = 0;
 
    while ( i < str_data.length ) {
        c = str_data.charCodeAt(i);
        if (c < 128) {
            tmp_arr[ac++] = String.fromCharCode(c); 
            i++;
        } else if ((c > 191) && (c < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    
    return tmp_arr.join('');
}

function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function processAgentLogin()
{
	if(validateLogin(document.frmLogin))
	{
		//show loading image
		var obj_img_div = browserObject('div_progressbar_img');
		obj_img_div.style.visibility = "visible";
		obj_img_div.style.display = "block";
			
		//hide msg div
		var obj_msg_div = browserObject('div_msg');
		obj_msg_div.style.visibility = "hidden";
		obj_msg_div.style.display = "none";
		
		//send ajax request
		var url = 'agent-login-ajx.php?mXYef=' + doRand();
		
		var actionName = 'processAgentLogin';
		var targDiv = obj_msg_div;
		var parameters = "btn_login=Login&txt_email=" + document.frmLogin.txt_email.value + 
						"&txt_password=" + document.frmLogin.txt_password.value + 
						"&chk_remember=" + (document.frmLogin.chk_remember.checked)*1;

		load_ajax_request(url, targDiv, actionName, parameters);
	}
	else
	{
		return false;	
	}
}

//the purpose of this function is to allow the enter key to 
function doClick(buttonName, e)
{
	//point to the correct button to click.

	var key;

	 if(window.event)
	 {
		  key = window.event.keyCode;     //IE
	 }
	 else
	 {
		  key = e.which;     //firefox
	 }

	if (key == 13)
	{

		//Get the button the user wants to have clicked

		var btn = document.getElementById(buttonName);

		//alert(btn);

		if (btn != null)
		{ //If we find the button click it

			if(window.event || (btn.type != 'undefined' && btn.type == 'button'))
			{
				btn.click();
			}
			else 
			{	
				if(btn.id == 'img_login')
				{
					processAgentLogin();
				}
			}
		
			event.keyCode = 0
		}
	}
}

