//var url ="http://{$host_url}/modules/FrontEndUsers/action.ajax.php?action=prof_exp";
var	action='';
var url ='';
var send_params;
var con_obj; // Connection object for Ajux request
var func_name; // the function name to execute after Ajux request
var	form;	// THe html form the input fields are in

/*
/ open Ajux connection to server and execute URL with the params
/
*/
function	send_server_request	(method)
{
//	var obj = document.getElementById(id);
	if (window.XMLHttpRequest)
	{
		//var XmlHttp = new XMLHttpRequest();
		con_obj = new XMLHttpRequest();
	}   else    {
//		var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		con_obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// for GET method
	if (method == 'GET')
	{
		//var send_url = url + "?" + send_params ;
		var send_url = url + send_params ;
		//alert(send_url);
		con_obj.open("GET", send_url, false);
		con_obj.setRequestHeader("Content-type", "text/html");
		con_obj.setRequestHeader("charset", "UTF-8");
		con_obj.send(null);
	} else if (method == 'POST') {
//		alert(url);
		con_obj.open("POST", url, false);
		con_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//		con_obj.setRequestHeader("Content-type", "text/html");
		con_obj.setRequestHeader("charset", "UTF-8");
//		alert(send_params);
		con_obj.send(send_params);
		//con_obj.send(null);
	}
	res = con_obj.getAllResponseHeaders();
	res = con_obj.responseText;

	// process the allowed exp id's
//	process_exp_ids(res);
	return res;
/*
	alert('res = '+res);
	con_obj.onreadystatechange=check_change;
	res = con_obj.getAllResponseHeaders();
	res = con_obj.responseText;
	con_obj.onreadystatechange=check_change;
//	alert('1');
//	con_obj.setRequestHeader("Content-length", parameters.length);
//	this.request.setRequestHeader("Connection", "close");
*/
}

function	process_exp_ids	(exp_ids)
{
//	alert(exp_ids);
	// first we disable all experties
	var	exps	=	document.getElementsByName('exps[]');
	var i=0;
	for (i=0;i < exps.length;i++)
	{
		exps[i].disabled=true;
	}

	var	exp_ids_arr	=	new	Array();
	exp_ids_arr	=	exp_ids.split(',');
	var exp_id_obj
	for (key in exp_ids_arr)
	{
		// Enable the experty checkbox
		exp_id_obj	=	document.getElementById(exp_ids_arr[key]);
		exp_id_obj.disabled=false;
	}
}
/*
/ when the request status is Done and the server response is OK (200) then execute the function with the response text
/
*/
function	check_change	()
{
	try
	{
		// 
	if (con_obj.readyState == 4)
		{
			if (con_obj.status == 200)
			{
		//		var res = obj.getAllResponseHeaders();
				var res = unescape(con_obj.responseText);
				alert(res);
				//var str_eval = func_name + "(\"" + res +"\");";
	//			alert("str eval = " + str_eval);
				eval(str_eval);
			} else {
				var res = con_obj.getAllResponseHeaders();
				var status_text = con_obj.statusText;
				alert('status = ' + status_text);
			}
		} 
	} 
	catch (er)
	{
		alert(er);
	}
}

function	ajux_request_manager(method,action_type,ext_params)
{
//	alert('method = ' + method + ' -- ' + action_type);
	switch	(action_type)
	{
		case	"admin_exps":
			var	profs_obj	=	document.getElementsByName('profs[]');
			var i=0;
			var	prof_ids='';
			for(i=0;i < profs_obj.length;i++)
			{
				if (profs_obj[i].checked == 1)
				{
					prof_ids += profs_obj[i].value + ',';
				}
			}

			send_params = '&prof_ids=' + prof_ids;
			//alert(send_params);
			var	exp_ids=send_server_request(method);
			process_exp_ids(exp_ids);
			break;
			case	"search_exps": // experties belong to profession for index search
//				alert('1');
				var	prof_obj	=	document.getElementById('prof');
				var	selected	=	prof_obj.selectedIndex;
				var	exp_obj	=	document.getElementById('exp');
				//alert("option value = " + prof_obj.options[selected].value);
				if (prof_obj.options[selected].value == 'all')
				{
					return;
				}
				send_params	=	'&prof_ids='+prof_obj.options[selected].value;
				var	exp_ids=send_server_request(method);
				//alert('exp ids = ' + exp_ids);

				var exp_opt_arr	=	new	Array();
				var opt_val_text_arr	=	new	Array();
				exp_opt_arr=exp_ids.split(",");
				var	opt_val_text;
				var	exp_id;
				var	exp_name;
				var opt_index=1;

				// Clear the experties select options first
				exp_obj.options.length=0;

				// Setting the All option
				exp_obj.options[0] = new Option('הכל','all');

				// Adding the experties belong to the profession
				for (i=0; i < exp_opt_arr.length;i++)
				{
					//alert(exp_opt_arr[i]);
					opt_val_text	=	exp_opt_arr[i];
					opt_val_text_arr	=	opt_val_text.split("=");
					exp_id	=	opt_val_text_arr[0];
					exp_name	=	opt_val_text_arr[1];
					exp_obj.options[opt_index] = new Option(exp_name,exp_id);
					opt_index++;
				}
				break;
			case	"check_alias": // check that the URL alias do not exists
				// check that URL alias has been updated
				var	curr_alias	=	document.getElementById('curr_alias').value;
				var	alias	=	document.getElementById('url_alias').value;
				if (curr_alias	==	alias)
				{
					return true;
				}
//				alert('alias = ' + alias);
				if (alias	==	'')
				{
					alert('חובה להזין שם ידידותי למנועי החיפוש !');
					return false;
				}
				send_params	=	'action=check_alias&url_alias=' + alias;
				var	alias_count	=	send_server_request(method);
//				alert(alias_count);
				if (alias_count != '0')
				{
					alert('קיים כבר שם כזה במערכת !!! נא לבחור שם אחר');
					return false;
				}

				return true;
				break;
			case	"update_user_stat": // Update the user "Show phone" or "Show email" counter
				// Checking if user ID was sent by ext_params
				if (/[,]/.test(ext_params))
				{
					var	ext_params_arr	=	ext_params.split(',');
					var	user_id	=	ext_params_arr[1];
					ext_params	=	ext_params_arr[0];
//					alert(ext_params_arr[1]);
				} else { // User id set as hidden params in HTML form
					var	user_id	=	document.getElementById('user_id').value ;
				}
//				alert(ext_params);
				switch	(ext_params)
				{
					case	"show_phone":
						document.getElementById('user_phones').style.display = 'block';
						// Hide the "Show phones button"
						document.getElementById('show_phone').style.display = 'none';
						break;
					case	"show_email":
						document.getElementById('user_email').style.display = 'block';
						// Hide the "Show email button"
						document.getElementById('show_email').style.display = 'none';
						break;
					case	"show_url":
						document.getElementById('user_url').style.display = 'block';
						// Hide the "Show email button"
						document.getElementById('show_url').style.display = 'none';
						break;
					case	"show_phone_by_id": // Show the user phone in the user card index
						document.getElementById('user_phones_' + user_id).style.display = 'block';
						// Hide the "Show phones button"
						document.getElementById('show_phone_' + user_id).style.display = 'none';
						ext_params	=	'show_phone';
						break;
				}

				// Add the type to user counter
				send_params = '&show_type=' + ext_params + "&user_id=" + user_id;
//				alert(send_params);
				var	res	=	send_server_request(method);
//				alert(res);
				break;
	}
}

/*
/	Show or hide object by ID
*/
function	show_or_hide	(id,action)
{
	var	obj	=	document.getElementById(id);
	switch	(action)
	{
		case	"show":
			obj.style.display	=	'block';
			break;
		case	"hide":
			obj.style.display	=	'none';
			break;
	}
}
