var pt_ajax_prg_flag = new Array();
var ajax_sct = new Array();
var prayer_time_url = "";

function in_array(hook, stack){
	for(tmp in stack){
		if(tmp == hook)
			return true;
	}
	return false;
}

function ajax_sct_reg(pt_nm){
	if(!in_array(pt_nm, ajax_sct))
		ajax_sct[ajax_sct.length] = pt_nm;
}

function pt_ajax_hnd_err(pt_sct, e){
	pt_ajax_disp_msg(pt_sct, "error", pt_get_error(e));
	pt_ajax_disp_prg(pt_sct, false);
	if(pt_get_error(e).toLowerCase() == "session timeout")
		redirect_parent(pt_url(""));
}

function pt_ajax_disp_msg(pt_sct, pt_type, pt_msg){
	ajax_hide_all_msg();
	var pt_wrn = prayer_time_get_obj("sct_ajax_" + pt_sct + "_msg");
	if(pt_wrn == null)
		return;
	pt_wrn.className = "ajax_message " + pt_type;
	pt_wrn.style.display = "";
	pt_wrn.innerHTML = pt_msg;
}

function pt_ajax_hide_msg(pt_sct){
	var pt_msg = prayer_time_get_obj("sct_ajax_" + pt_sct + "_msg");	
	if(pt_msg == null)
		return;
	pt_msg.style.display = "none";
	pt_msg.innerHTML = pt_msg;
}

function ajax_hide_all_msg(){
	for(pt_sct in ajax_sct){
		pt_ajax_hide_msg(ajax_sct[pt_sct]);
	}
}

function pt_ajax_disp_prg(pt_sct, pt_prg){
	if(typeof pt_prg == "undefined")
		pt_prg = true;
	if(typeof pt_ajax_prg_flag[pt_sct] == "undefined")
		pt_ajax_prg_flag[pt_sct] = false;
	var pt_frm = prayer_time_get_obj("sct_ajax_" + pt_sct);
	var pt_trn = prayer_time_get_obj("sct_ajax_" + pt_sct + "_prg");
	
	if(pt_prg){
		if(pt_ajax_prg_flag[pt_sct])
			return;		
			
		pt_ajax_hide_msg(pt_sct);
		
		if(to_int(pt_frm.offsetHeight) <= 20)
			pt_trn.style.height = "20px";
		else
			pt_trn.style.height = pt_frm.offsetHeight + "px";
		
		pt_trn.style.width = pt_frm.offsetWidth + "px";
		
		pt_frm.style.display = "none";
		pt_trn.style.display = "";
		
		pt_ajax_prg_flag[pt_sct] = true;
	}else{
		if(!pt_ajax_prg_flag[pt_sct])
			return;	
			
		pt_frm.style.display = "";
		pt_trn.style.display = "none";
		pt_ajax_prg_flag[pt_sct] = false;
	}
}
	
function pt_create_http_req() {
	var pt_request;
	if(window.ActiveXObject){
		try{
			pt_request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
			pt_request = false;
		}
	}else{
		try{
			pt_request = new XMLHttpRequest();
		}catch (e){
			pt_request = false;
		}
	}
	if (!pt_request)
		catch_error("Your browser does not support AJAX!");
	else
		return pt_request;
}

function pt_ajax_query(prm_cmd, prm_pt_dat_post, prm_pt_misc){
	var pt_xml = pt_create_http_req();
	if(typeof prm_dat_get == "undefined")
		var prm_dat_get = {};
	if(typeof prm_pt_dat_post == "undefined")
		var prm_pt_dat_post = {};
	if(typeof prm_pt_misc == "undefined")
		var prm_pt_misc = {};
		
	function pt_send_query(){
		if(prm_cmd == ""){
			alert("No act to be send!");
			return false;
		}
		
		if (pt_xml.readyState == 4 || pt_xml.readyState == 0){ 
			req_url = pt_url(prm_cmd);	
			dat_str = "";
			for(tmp in prm_pt_dat_post){
				if(dat_str != "")
				   	dat_str += "&";
				dat_str += tmp + "=" + prm_pt_dat_post[tmp];
			}
			if(dat_str == ""){
				pt_xml.open("GET", req_url, true); 			
				pt_xml.onreadystatechange = pt_response;
				pt_xml.setRequestHeader("Accept-Charset","UTF-8");
				pt_xml.send(null);
			}else{
				pt_xml.open("POST", req_url, true); 			
				pt_xml.onreadystatechange = pt_response;
				pt_xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				pt_xml.setRequestHeader("Accept-Charset","UTF-8");
				pt_xml.send(dat_str); 
			}			
			return true;
		}		
		return false;
	}
	
	function pt_response(){
		try{
			if (pt_xml.readyState == 4) { 
				if (pt_xml.status == 200) {
					try{
						pt_res = pt_xml.responseXML.documentElement;	
						pt_handle_response(pt_res);
									
						if(typeof prm_pt_misc.exec_cpl != "undefined")
							eval(prm_pt_misc.exec_cpl);
						if(typeof prm_pt_misc.exec_done != "undefined")
							eval(prm_pt_misc.exec_done);
					}catch(err){}
				}else{
					catch_error(pt_xml.responseText); 
				}
			}
		}catch (e){
			if(typeof prm_pt_misc.exec_fail != "undefined")
				eval(prm_pt_misc.exec_fail);
			if(typeof prm_pt_misc.exec_done != "undefined")
				eval(prm_pt_misc.exec_done);
			catch_error(e);
		}
	}
	
	function pt_handle_response(pt_reply){	
		try{
			if(typeof prm_pt_misc.exec != "undefined")
				eval(prm_pt_misc.exec + "(pt_reply, prm_pt_misc);");
		}catch (e){
			catch_error(e);
		}
	}
	
	return pt_send_query();
}

function str_replace(str,sr,val){
	return str.replace(sr, val, "g");	
}

function pt_parse_html(html){
	var pt_res = html;
	
	pt_res = str_replace(pt_res,"&gt;",">");	
	pt_res = str_replace(pt_res,"&lt;","<");	
	pt_res = str_replace(pt_res,"&quot;","\"");
	pt_res = str_replace(pt_res,"&brvbar;","|");
	pt_res = str_replace(pt_res,"&amp;","&");
	return pt_res;
}

function to_int(str){
	pt_res = parseInt(to_num(str));
	if(isNaN(pt_res))
		pt_res = 0;
	return pt_res;
}

function to_num(str){
	pt_res = parseFloat(str);
	if(isNaN(pt_res))
		pt_res = 0;
	return pt_res;
}

function pt_is_ie(){
	if(navigator.appName == "Microsoft Internet Explorer")
		return true;
	else
		return false;
}

function is_opera(){
	if(navigator.appName == "Opera")
		return true;
	else
		return false;
}

function pt_get_error(e){
	return e.message
}

function prayer_time_get_obj(object_name){
	return document.getElementById(object_name);
}

function catch_error(str){
	if(str != "")
		throw new Error(str);
}

/* URL */

function pt_url(pt_nm){		
	return prayer_time_url + "/ajax/" + pt_nm;
}

/* End of URL */

/* pt_xml */

function pt_xml_nd_val(pt_nd){
	try{
		return pt_parse_html(pt_nd.firstChild.data);
	}catch(e){
		return "";
	}
}

function xml_nds_by_tag(pt_xml, pt_tag){
	pt_res = new Array();
	pt_nds = pt_xml.getElementsByTagName(pt_tag);
	pt_len = pt_nds.length;
	for(pt_ai=0;pt_ai<pt_len;pt_ai++)
		pt_res[pt_ai] = pt_nds[pt_ai];
	return pt_res;
}

function pt_xml_nd_chd(pt_nd){
	pt_res = new Array();
	pt_nds = pt_nd.childNodes;
	pt_len = pt_nds.length;	
	for(pt_bi=0;pt_bi<pt_len;pt_bi++)
		pt_res[pt_bi] = pt_nds[pt_bi];
	return pt_res;
}

function pt_xml_msg(pt_xml, pt_tag){
	if(typeof pt_tag == "undefined")
		var pt_tag = "error";
	
	lst_msg = new Array();
	nds_msg = xml_nds_by_tag(pt_xml, pt_tag);
	if(nds_msg.length == 0)
		return "";
	for(nd_msg in nds_msg)
		lst_msg[lst_msg.length] = pt_xml_nd_val(nds_msg[nd_msg]);	
	return lst_msg.join("<br/>");
}

/* End of pt_xml*/

/* Table */

function tbl_rst_row(pt_nm, skip_first){
	if(typeof(skip_first) == "undefined")
		skip_first = false;

	var tbl = prayer_time_get_obj(pt_nm);
	tbl = tbl.getElementsByTagName("tbody")[0];
	var pt_nds = pt_xml_nd_chd(tbl);
	var is_first = true;
	for(pt_nd in pt_nds){
		if(typeof pt_nds[pt_nd].nodeName != "undefined"){
			if(pt_nds[pt_nd].nodeName.toLowerCase() == "tr"){	
				if(skip_first == true && is_first == true){
					is_first = false;
					continue;
				}else{
					tbl.removeChild(pt_nds[pt_nd]);		
				}
			}
		}
	}
}

function tbl_add_row(pt_nm, col_def, atr){
	if(typeof atr == "undefined")
		atr = new Array();
	
	var tbl = prayer_time_get_obj(pt_nm);
	tbl = tbl.getElementsByTagName("tbody")[0];
	
	row = document.createElement("tr");
	
	for(tmp_atr in atr){
		if(tmp_atr == "class")
			row.className = atr[tmp_atr];
		else
			row.setAttribute(tmp_atr, atr[tmp_atr]);
	}
	
	for(pt_ti=0;pt_ti<col_def.length;pt_ti++){
		col = document.createElement("td");
		tmp_col_ctt = col_def[pt_ti];
		
		if(typeof tmp_col_ctt["atr"] == "undefined")
			tmp_col_ctt_atr = new Array();
		else
			tmp_col_ctt_atr = tmp_col_ctt["atr"];
		for(tmp_atr in tmp_col_ctt_atr){
			if(tmp_atr == "class")
				col.className = tmp_col_ctt_atr[tmp_atr];
			else
				col.setAttribute(tmp_atr, tmp_col_ctt_atr[tmp_atr]);
		}
		
		col.innerHTML = tmp_col_ctt["val"];		
		row.appendChild(col);
	}	
	
	tbl.appendChild(row);
}

/* End of Table*/

/* Element */
function prayer_time_cbo_reset(id)
{
	prayer_time_get_obj(id).options.length = 0;
}

function prayer_time_cbo_add(id, prm_text, prm_val) {
	var optn = document.createElement("OPTION");
	optn.text = prm_text;
	optn.value = prm_val;
	prayer_time_get_obj(id).options.add(optn);
}
/* End of Element */

/* Widget Load Prayer Time */
function prayer_time_widget_time_load(){
	try{
		pt_ajax_disp_prg("prayer_time_widget_time");
		
		pt_misc = {
			"exec": "prayer_time_widget_time_load_exec",
			"exec_done": "pt_ajax_disp_prg('prayer_time_widget_time', false);"
		};
		
		pt_dat_post = {
			"country": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_country").value),
			"city": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_city").value),
			"date_dd": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_date_dd").value),
			"date_mm": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_date_mm").value),
			"date_yy": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_date_yy").value)
		};

		if(!pt_ajax_query("get_daily_time.php", pt_dat_post, pt_misc))
			setTimeout("prayer_time_widget_time_load()", 1000);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_widget_time", e);
	}
}

function prayer_time_widget_time_load_exec(pt_reply){	
	try{
		catch_error(pt_xml_msg(pt_reply, "error"));
		pt_nds_res = pt_xml_nd_chd(pt_reply);

		if(pt_nds_res.length > 0){
			for(var i=0;i<pt_nds_res.length;i++){	
				pt_tmp_name = pt_nds_res[i].nodeName;
				pt_tmp_time = pt_xml_nd_val(pt_nds_res[i]);			
				pt_tmp_elm = prayer_time_get_obj("prayer_time_widget_time_" + pt_tmp_name);
				if(pt_tmp_elm != null)
					pt_tmp_elm.innerHTML = pt_tmp_time;
			}
		}
		pt_ajax_disp_prg('prayer_time_widget_time', false);
	}catch(e){		
		pt_ajax_hnd_err("prayer_time_widget_time", e);
	}	
} 

/* End of Widget Load Prayer Time */

/* Widget Load City */
function prayer_time_widget_city_load(){
	try{
		pt_ajax_disp_prg("prayer_time_widget_city");
		
		pt_misc = {
			"exec": "prayer_time_widget_city_load_exec",
			"exec_done": "pt_ajax_disp_prg('prayer_time_widget_city', false);"
		};

		pt_dat_post = {
			"country": encodeURIComponent(prayer_time_get_obj("prayer_time_widget_country").value)
		};
		
		if(!pt_ajax_query("get_city.php", pt_dat_post, pt_misc))
			setTimeout("prayer_time_widget_city_load()", 1000);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_widget_city", e);
	}
}

function prayer_time_widget_city_load_exec(pt_reply){
	try{
		catch_error(pt_xml_msg(pt_reply, "error"));		
		pt_nds_res = xml_nds_by_tag(pt_reply, "list");	
		
		prayer_time_cbo_reset("prayer_time_widget_city");
		if(pt_nds_res.length > 0){
			for(var i=0;i<pt_nds_res.length;i++){				
				pt_city = pt_xml_nd_val(pt_nds_res[i]);	
				prayer_time_cbo_add("prayer_time_widget_city", pt_city, pt_city);
			}
		}
		prayer_time_widget_time_load();
		pt_ajax_disp_prg('prayer_time_widget_city', false);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_widget_city", e);
	}	
} 
/* End of Widget Load City */

/* Page Load Prayer Time */
function prayer_time_page_time_load(){
	try{
		pt_ajax_disp_prg("prayer_time_page_time");
		
		pt_misc = {
			"exec": "prayer_time_page_time_load_exec",
			"exec_done": "pt_ajax_disp_prg('prayer_time_page_time', false);"
		};
		
		pt_dat_post = {
			"country": encodeURIComponent(prayer_time_get_obj("prayer_time_page_country").value),
			"city": encodeURIComponent(prayer_time_get_obj("cbo_prayer_time_page_city").value),
			"date_mm": encodeURIComponent(prayer_time_get_obj("prayer_time_page_date_mm").value),
			"date_yy": encodeURIComponent(prayer_time_get_obj("prayer_time_page_date_yy").value)
		};

		prayer_time_get_obj("lnk_prayer_time_print").href = prayer_time_url + "/prayer_time.print.php?country=" + pt_dat_post.country + "&city=" + pt_dat_post.city + "&date_mm=" + pt_dat_post.date_mm + "&date_yy=" + pt_dat_post.date_yy;
		
		if(!pt_ajax_query("get_monthly_time.php", pt_dat_post, pt_misc))
			setTimeout("prayer_time_page_time_load()", 1000);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_page_time", e);
	}
}

function prayer_time_page_time_load_exec(pt_reply){
	try{
		catch_error(pt_xml_msg(pt_reply, "error"));		
		pt_nds_res = xml_nds_by_tag(pt_reply, "list");
		
		tbl_rst_row("prayer_time_monthly", true);	
		
		if(pt_nds_res.length > 0){		
			for(pt_i=0;pt_i<pt_nds_res.length;pt_i++){
				ndc_res = pt_xml_nd_chd(pt_nds_res[pt_i]);
				rw = new Array();
				
				ret_date = pt_i.toString();		
				ret_fajr = "";
				ret_sunrise = "";
				ret_zuhr = "";
				ret_asr = "";
				ret_maghrib = "";
				ret_isha = "";
				
				if(ndc_res.length > 0){
					for(pt_j=0;pt_j<ndc_res.length;pt_j++){
						pt_nd = ndc_res[pt_j];
						val = pt_xml_nd_val(pt_nd);
						switch(pt_nd.nodeName){
							case "date":
								ret_date = val;							
								break;
							case "fajr":
								ret_fajr = val;							
								break;
							case "sunrise":
								ret_sunrise = val;							
								break;
							case "zuhr":
								ret_zuhr = val;							
								break;
							case "asr":
								ret_asr = val;							
								break;
							case "maghrib":
								ret_maghrib = val;							
								break;
							case "isha":
								ret_isha = val;							
								break;
						}
					}							
		
					rw[rw.length] = {"val": ret_date};
					rw[rw.length] = {"val": ret_fajr};
					rw[rw.length] = {"val": ret_sunrise};
					rw[rw.length] = {"val": ret_zuhr};
					rw[rw.length] = {"val": ret_asr};
					rw[rw.length] = {"val": ret_maghrib};
					rw[rw.length] = {"val": ret_isha};
					
					tbl_add_row("prayer_time_monthly", rw);
				}
			}		
		}else{
			rw = {0:{"val": "Nothing to add"}};
			tbl_add_row("prayer_time_monthly", rw);
		}
		pt_ajax_disp_prg('prayer_time_page_time', false);
	}catch(e){
		alert(e);
		pt_ajax_hnd_err("prayer_time_page_time", e);
	}	
} 

/* End of Page Load Prayer Time */

/* Page Load City */
function prayer_time_page_city_load(){
	try{
		pt_ajax_disp_prg("prayer_time_page_city");
		
		pt_misc = {
			"exec": "prayer_time_page_city_load_exec",
			"exec_done": "pt_ajax_disp_prg('prayer_time_page_city', false);"
		};

		pt_dat_post = {
			"country": encodeURIComponent(prayer_time_get_obj("prayer_time_page_country").value)
		};
		
		if(!pt_ajax_query("get_city.php", pt_dat_post, pt_misc))
			setTimeout("prayer_time_page_city_load()", 1000);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_page_city", e);
	}
}

function prayer_time_page_city_load_exec(pt_reply){
	try{
		catch_error(pt_xml_msg(pt_reply, "error"));		
		pt_nds_res = xml_nds_by_tag(pt_reply, "list");	
		
		prayer_time_cbo_reset("cbo_prayer_time_page_city");		
		if(pt_nds_res.length > 0){
			for(i=0;i<pt_nds_res.length;i++){	
				pt_city = pt_xml_nd_val(pt_nds_res[i]);
				prayer_time_cbo_add("cbo_prayer_time_page_city", pt_city, pt_city);
			}
		}
		prayer_time_page_time_load();
		pt_ajax_disp_prg('prayer_time_page_city', false);
	}catch(e){
		pt_ajax_hnd_err("prayer_time_page_city", e);
	}	
} 
/* End of Page Load City */
