// JavaScript Document
var popUpWin=0;
var mouse_x = 0;
var mouse_y = 0;
var refresh_floating_window_pos = true;
var breadcrump_array = new Array();
var breadcrump_placeholder = "top_horizontal_2";

function activate(object_name){		
	document.getElementById(active_id).className="hidden";
	document.getElementById(active_id + '_p').className="home_header_1_item";
	active_id = object_name;
	document.getElementById(active_id).className="visible";
	document.getElementById(active_id + '_p').className="home_header_1_item_active";
}

function switch_search_box(){
	var w_obj = document.getElementById("search_box_wrapper");
	if (w_obj.style.display == 'none') {
		w_obj.style.display = '';
	} else {
		w_obj.style.display = 'none';
	}
}

function email_to_friend(url_to_visit){
	popUpWindow	("/ws_dir/send_mail/send_mail.php?link=" + escape(url_to_visit), screen.width / 2 - 325, screen.height / 2 - 155, 400, 200);
}

function join_mailing_list(yp_id, source_flag){
	popUpWindow	("/ws_dir/mailing_list/mailing_list.php?yp_id=" + yp_id + "&source_flag=" + source_flag, screen.width / 2 - 325, screen.height / 2 - 155, 400, 200);
}

function popUpWindow(URLStr, left, top, width, height, has_scroll){
	if (!has_scroll) {
		has_scroll = "no" 
	} else {
		has_scroll = "yes"; 
	}
	if(popUpWin){
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + has_scroll + ',resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function empty_text_box(obj_box, default_text){
	if (obj_box.value == default_text){
		obj_box.value = '';
	}
}

function complete_default_text(obj_box, default_text){
	if (obj_box.value == ''){
		obj_box.value = default_text;
	}
}

function normalize_search_fields(field_1, default_text_1, field_2, default_text_2){
	ret_val = true;
	if (field_1.value == default_text_1) { field_1.value = ''; }
	if (field_2.value == default_text_2) { field_2.value = ''; ret_val = false; }
	if (!ret_val) { alert("Please tell us where to search for business !"); }
	document.getElementById('category_ph').value = field_1.value;  
	document.getElementById('location_ph').value = field_2.value;	
	return ret_val;
}

var floating_window = {	
	_handler : function() {
		return document.getElementById('floating_window');
	},
	
	_write : function(_new_content) {
		// Force visibility
		if (this._handler().className == "hidden") this._handler().className = "visible";
		floating_window._handler().innerHTML = _new_content;
		if (refresh_floating_window_pos) {
			this._handler().style.top = parseInt(mouse_y + 10) + "px";
			var left_pos = mouse_x - this._handler().offsetWidth / 2;
			this._handler().style.left = parseInt((left_pos >= 0)?left_pos:0) + "px";
		}
		refresh_floating_window_pos = true;
	},
	
	_clear : function(){
		this._handler().innerHTML = "";
	},
	
	_hide : function(){
		_set_class("hidden");
	},
	
	_set_class : function (_new_class_name){
		this._handler().className = _new_class_name;
	},
	
	_switch_on_loading_state : function (){
		this._write("<img src='/images/loader.gif' border='0'>");
		left_pos = mouse_x - this._get_width() / 2;
		this._set_left(parseInt((left_pos >= 0)?left_pos:0));

		refresh_floating_window_pos = false;
	},
	
	_get_width : function(){
		return this._handler().offsetWidth;
	},

	_get_left : function(){
		return this._handler().style.left;
	},
	
	_set_left : function (_new_left){
		this._handler().style.left = _new_left + "px";
	},
	
	_destroy : function(){}
}

function getMouseXY(e){ 
	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
	if (e)  { 
		if (e.pageX || e.pageY)  { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mouse_x = e.pageX;
			mouse_y = e.pageY;
			algor = '[e.pageX]';
			if (e.clientX || e.clientY) algor += ' [e.clientX] '
		} else if (e.clientX || e.clientY)  { // works on IE6,FF,Moz,Opera7
			mouse_x = e.clientX + document.body.scrollLeft;
			mouse_y = e.clientY + document.body.scrollTop;
			algor = '[e.clientX]';
			if (e.pageX || e.pageY) algor += ' [e.pageX] '
		}  
	}
}

function get_states_list(refresh_window_position){
	refresh_floating_window_pos = refresh_window_position;
	floating_window._switch_on_loading_state();
	jx.load("/ws_dir/autocomplete_ws/autocomplete_ws.php?op=gsl&at=y", write_result);
}

function get_cities_list(refresh_window_position, state_id){
	refresh_floating_window_pos = refresh_window_position;
	floating_window._switch_on_loading_state();
	jx.load("/ws_dir/autocomplete_ws/autocomplete_ws.php?op=gct&pid=" + state_id + "&at=y", write_result);
}

function get_categories_list(refresh_window_position){
	refresh_floating_window_pos = refresh_window_position;
	floating_window._switch_on_loading_state();
	jx.load("/ws_dir/autocomplete_ws/autocomplete_ws.php?op=gtc&at=y", write_result);
}

function write_result(data){
	floating_window._write(data);
}

function refresh_breadcrump(){
	placeholder = document.getElementById(breadcrump_placeholder);
	tmp_content = "";
	for (i = 0; i < breadcrump.length; i++){
		if (i > 0) tmp_content += " &gt;&gt; "
		tmp_content += breadcrump.pop();
	}
}

function set_cookie(c_name,value,expiredays){
	var breadcrump = GetCookie('breadcrump');
//alert (breadcrump);	
	if (!breadcrump ) breadcrump = "";	
	
	breadcrump_array = breadcrump.split('|');
	
	var existing_index = breadcrump_array.indexOf(c_name);
	if (existing_index > -1) {
		breadcrump_array.splice(existing_index);
	}
	breadcrump_array.push(c_name);	
	breadcrump = breadcrump_array.join('|');
//alert (breadcrump);	
	SetCookie('breadcrump', breadcrump, expiredays);
	SetCookie(c_name,value,expiredays);
}
 
function get_cookie(c_name) {
	return GetCookie(c_name);
}

function SetCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);	
	document.cookie=c_name+ "=" + escape(value) + ";path=/" + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());	
}

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 clear_cookie(){
	var breadcrump = GetCookie('breadcrump');
	breadcrump_array = breadcrump.split('|');
	for (i = 0; i < breadcrump_array.length; i++){
		SetCookie(breadcrump_array[i], '', 0);
	}
	SetCookie('breadcrump', '', 0);
}

document.onmousemove = getMouseXY;

/* START USER LOGIN WINDOW SCROLL CODE*/
var usr_original_window_content;
var usr_floating_login_ph;
var usr_back_layer;
var usr_login_layer;
var scroll_top = 0;

function show_login_window(){
	try{		
		usr_floating_login_ph = document.getElementById('floating_login_ph');
		usr_back_layer = document.getElementById('usr_background_layer');
		usr_login_layer = document.getElementById('usr_login_window');
		if (document.getSelection) {
			usr_back_layer.style.width = document.documentElement.clientWidth + "px";
			usr_back_layer.style.height = document.documentElement.clientHeight+ "px";
		}else{
			usr_back_layer.style.width = document.body.clientWidth + "px";
			usr_back_layer.style.height = document.body.clientHeight+ "px";
		}
		
		usr_login_wnd_h = usr_login_layer.clientHeight;
		usr_login_wnd_w = usr_login_layer.clientWidth;
		usr_floating_login_ph.className = "visible";
		usr_login_layer.style.left = "300px";
		update_usr_login_window_pos();
	}catch(e){
		//alert(e);
	}
}

function update_usr_login_window_pos(){
	// Get current scroll
	try{
		if (document.getSelection) {
			scroll_top = document.documentElement.scrollTop;
		}else{
			scroll_top = document.body.scrollTop;
		}
		usr_login_layer.style.top = (200 + scroll_top) + "px";
		usr_back_layer.style.top = scroll_top + "px";
	}catch(e){
	}
}

function hide_login_window(){
	usr_login_layer.innerHTML = usr_original_window_content;
	usr_floating_login_ph.className = "hidden";
}

function cancel_login_window(){
	usr_floating_login_ph.className = "hidden";
}
/* END USER LOGIN WINDOW SCROLL CODE*/

function switch_login_window_on_loading(){
	usr_original_window_content = usr_login_layer.innerHTML
	usr_login_layer.innerHTML = "<img src='/images/loader.gif' border='0'>";
}

function try_login(_user_name_id, _user_password_password, _business_id, _source_flag){
	_user_name = document.getElementById(_user_name_id).value;
	_user_password = document.getElementById(_user_password_password).value;
	switch_login_window_on_loading();
	jx.load("/users/users.php?op=login&bid=" + _business_id + "&user_name=" + escape(_user_name) + "&user_password=" + escape(_user_password) + "&sf=" + _source_flag, function (data){
		if (data == '1') {
			top.location.reload(true);
		}
		if (data == '0') {
			alert("Wrong username or password !");
			hide_login_window();
		}
		if (data == '-1'){
			alert("The business has allready been claimed !");
			hide_login_window();
		}
	});
}

function log_out(){
	jx.load("/users/users.php?op=logout", function (data){
		if (data == '1') {
			top.location.reload(true);
		}
	});
}

function try_register(r_email, r_pwrd1, r_pwd2){
	_r_email = document.getElementById(r_email).value;
	_r_pwrd1 = document.getElementById(r_pwrd1).value;
	_r_pwrd2 = document.getElementById(r_pwd2).value;
	
	if (_r_pwrd1 == _r_pwrd2) {
		
		switch_login_window_on_loading();
	
		jx.load("/users/users.php?op=register&r_user_email=" + escape(_r_email) + "&r_password1=" + escape(_r_pwrd1) + "&r_password2=" + escape(_r_pwrd2), function (data){
			if (data == '1') {
				//top.location.reload(true); 
				alert("You have successfully registered with MerchantSpan.com.\n\nPlease check your email and activate your account !");
			}else{
				alert("Registration failed !");
			}
			hide_login_window();			
		});	
	}else{
		alert('Password not match !');
	}
}

function reset_password(fyp_email){
	_fyp_email = document.getElementById(fyp_email).value;
	switch_login_window_on_loading();

	jx.load("/users/users.php?op=fyp&email=" + escape(_fyp_email), function (data){
		alert(data);
		hide_login_window();			
	});	
}

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
