var ajaxrunning = 0;
function ajaxRunning(){
	if(ajaxrunning == 1 && ajaxRequest.readyState != 4){
		return false;
	}else{
		return true;
		ajaxrunning = 0;
	}
}
function simulatecheckbox(fieldtocheck) {
	var thecheck = document.getElementById(fieldtocheck);
	var thebutton = document.getElementById(fieldtocheck + "button");
	if (thecheck.checked) {
		thecheck.checked="";
		thebutton.className = "b0";
	} else {
		thecheck.checked="checked";
		thebutton.className = "b1";
	}
}
function productDetailsPrompt(title, message){
		try{
			var p = document.getElementById("prompt");
			var p_bg = document.getElementById("prompt_bg");
			document.body.removeChild(p);
			document.body.removeChild(p_bg);
		}catch(e){
		}
		var prompt_bg = document.createElement("div");
		prompt_bg.setAttribute("id","prompt_bg");
		document.body.appendChild(prompt_bg);
		//var innertext = '<div id="prompt_title">Email A Friend</div><div id="prompt_question">Loading...</div>';
		var am_prompt = document.createElement("div");
		am_prompt.setAttribute("id","prompt");
		//am_prompt.innerHTML = innertext
		document.body.appendChild(am_prompt);
		var am_prompta = document.getElementById("prompt");
		var am_sub1_prompt = document.createElement("div");
		am_sub1_prompt.innerHTML = title;
		am_sub1_prompt.setAttribute("id","prompt_title");
		am_prompta.appendChild(am_sub1_prompt);
		var am_sub2_prompt = document.createElement("div");
		am_sub2_prompt.setAttribute("id","prompt_question");
		am_sub2_prompt.innerHTML = "<div id='prompt_message'>message</div><a href='javascript:;' onclick='deletePrompt();'>Done</a>";
		am_prompta.appendChild(am_sub2_prompt);
		//var form = escape(ajaxFunction('/includes/emailform.php','prompt_question'));

		/* +'*Your Email Address:<br><input type="text" name="youremail" /><br><br>'
		+'*Your Friend\'s Name:<br><input type="text" name="friendname" /><br><br>'
		+'*Your Friend\'s Email Address:<br><input type="text" name="friendemail" /><br><br>'
		+'Your Message:<br>'+
		'<textarea name="message"></textarea></form></div><div id="prompt_options"><a href="#" onClick="submitPrompt();">Send</a> <a href="#" onClick="deletePrompt();">Cancel</a> */




	}
function deletePrompt(){
			var p = document.getElementById("prompt");
			var p_bg = document.getElementById("prompt_bg");
			document.body.removeChild(p);
			document.body.removeChild(p_bg);
}
function emailPrompt(){
		try{
			var p = document.getElementById("prompt");
			var p_bg = document.getElementById("prompt_bg");
			document.body.removeChild(p);
			document.body.removeChild(p_bg);
		}catch(e){
		}
		var prompt_bg = document.createElement("div");
		prompt_bg.setAttribute("id","prompt_bg");
		document.body.appendChild(prompt_bg);
		//var innertext = '<div id="prompt_title">Email A Friend</div><div id="prompt_question">Loading...</div>';
		var am_prompt = document.createElement("div");
		am_prompt.setAttribute("id","prompt");
		//am_prompt.innerHTML = innertext
		document.body.appendChild(am_prompt);
		var am_prompta = document.getElementById("prompt");
		var am_sub1_prompt = document.createElement("div");
		am_sub1_prompt.innerHTML = "Email A Friend";
		am_sub1_prompt.setAttribute("id","prompt_title");
		am_prompta.appendChild(am_sub1_prompt);
		var am_sub2_prompt = document.createElement("div");
		am_sub2_prompt.setAttribute("id","prompt_question");
		am_sub2_prompt.innerHTML = "Loading...";
		am_prompta.appendChild(am_sub2_prompt);
		var form = escape(ajaxFunction('/includes/emailform.php','prompt_question'));

		/* +'*Your Email Address:<br><input type="text" name="youremail" /><br><br>'
		+'*Your Friend\'s Name:<br><input type="text" name="friendname" /><br><br>'
		+'*Your Friend\'s Email Address:<br><input type="text" name="friendemail" /><br><br>'
		+'Your Message:<br>'+
		'<textarea name="message"></textarea></form></div><div id="prompt_options"><a href="#" onClick="submitPrompt();">Send</a> <a href="#" onClick="deletePrompt();">Cancel</a> */




	}
function submitPrompt(file){
		var el = document.shareForm.elements;
		var getstr = file+"?";
		for($n = 0; $n < el.length; $n++){
				if($n != 0){
				getstr += "&";
				}
				getstr += el[$n].name + "=" + el[$n].value;
		}
		//alert(getstr);
		 ajaxFunction(getstr, "prompt_question");
}
//if param = "" GET else POST
function postajax(){}
var ajaxRequest;  // The variable that makes Ajax possible!
function ajaxFunction(file, id){
	
	//var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Please Update Browser to allow for javascript!");
				return false;
			}
		}
	}
	ajaxRequest.abort();
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
	var myel = document.getElementById('loader');
		if(ajaxRequest.readyState == 4){
			var n = 1;
			var x = 0;
			var temptext = "";
			var ajaxtext = ajaxRequest.responseText;
			var array = new Array();
			array = ajaxtext.split('(amcolor123)');
			for(x in array){
				try{
					if(array[x].search("h4") != -1){
						n=1;
					}
					temptext += array[x].replace("amcolor", "amcolor" + n);
					if(n == 1){
						n = 2;
					}else{
						n = 1;
					}
				}catch(e){}

			}
			if(temptext == "")temptext = ajaxtext;
			//alert(temptext);
			var finder = "@@";
			var tempfinder = temptext.substring(temptext.length-3,temptext.length-1); 
			if(temptext.search(finder) != -1){
				var splittext = temptext.split(finder);
				temptext = splittext[0];
				var script = splittext[1];
			}
			
			if(id != ""){
				document.getElementById(id).innerHTML = temptext;
				try{
					if(script.length > 0){
						postajax(script);
					}
				}catch(e){
				}
				
				
			}
			showLoading(0);
		}
	}
		
		if(file.search(/\?/) == -1){
			file += "?";
		}else{
			file += "&";	
		}
		showLoading(1);
		var rand = Math.round(Math.random()*100000000)+1;
		file += rand;
		ajaxRequest.open("GET", file, true);
		ajaxRequest.send(null);
}
function showLoading(process){
	
	var pagebody = document.getElementById("pagebody");
	//debugConsole("processing");
	try{
		var loadingdiv = document.getElementById('uploadpopup');
		pagebody.removeChild(loadingdiv);
	}catch(e){//console.log(e);
	}
	if(process == 1){
	var loadingdiv = document.createElement("div");
	loadingdiv.setAttribute('id', 'uploadpopup');
	loadingdiv.setAttribute('class', 'show');
	loadingdiv.innerHTML = '<img alt="Loading" src="/admin/images/ajax-loader.gif"/> <span>Loading...</span>'; 
	try{
		pagebody.appendChild(loadingdiv);
	}catch(e){}
	}
}
function debugConsole(message){
//var console, documentForm;
//console.log("processing");
   /*  try {
        console = document.getElementById("SWFUpload_Console");

        if (!console) {
            documentForm = document.createElement("form");
            document.getElementsByTagName("body")[0].appendChild(documentForm);

            console = document.createElement("textarea");
            console.id = "SWFUpload_Console";
            console.style.fontFamily = "monospace";
            console.setAttribute("wrap", "off");
            console.wrap = "off";
            console.style.overflow = "auto";
            console.style.width = "700px";
            console.style.height = "350px";
            console.style.margin = "5px";
            documentForm.appendChild(console);
        }

        console.value += message + "\n";

        console.scrollTop = console.scrollHeight - console.clientHeight;
    } catch (ex) {
        alert("Exception: " + ex.name + " Message: " + ex.message);
    } */
}
function ajaxLoad(param){
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Please Update Browser to allow for javascript!");
				return false;
			}
		}
	}
	//pagingtext +="<span id=\"first\"><a href=\"javascript:ajaxLoad(&pageNum="+0+"&totalRows="+pages[1]");\">|<</a></span>";
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
	//var myel = document.getElementById('loader');
		if(ajaxRequest.readyState == 4){
			var n = 1;
			var x = 0;
			var temptext = ""
			var ajaxtext = ajaxRequest.responseText;
			temptext = ajaxtext.split("]||");
			var paginginfo = temptext[0].substring(1,temptext[0].length);
			var pages = paginginfo.split(",");
			var pagingtext = "";
			var pageNum = parseInt(pages[0]);
			var totalPages = parseInt(pages[1]);

			var nextPage = pageNum+1;
			if(nextPage > totalPages){
				nextPage = totalPages;
			}
			var prevPage = pageNum-1;
			if(prevPage < 0){
				prevPage = 0;
			}
			var totalRows = parseInt(pages[2]);
			if(pageNum == 0){
				pagingtext +="<span id=\"first\">|<</span>";
			}else{
				pagingtext +="<span id=\"first\"><a href=\"javascript:ajaxLoad('"+params+"&pageNum="+0+"&totalRows="+totalRows+"');\">|<</a></span>";
			}

			 if(pageNum == 0){
				pagingtext +=" <span id=\"previous\"><</span> ";
			}else{
				pagingtext +=" <span id=\"previous\"><a href=\"javascript:ajaxLoad('"+params+"&pageNum="+prevPage+"&totalRows="+totalRows+"');\"><</a></span> ";
			}
			var currentpage = pageNum+1;
			var lastPage = totalPages+1;
			pagingtext += " <span id=\"text\">Page "+currentpage+" of " + lastPage+"</span>";

			if(pageNum == totalPages){
				pagingtext +="<span id=\"next\">></span>";
			}else{
				pagingtext +=" <span id=\"next\"><a href=\"javascript:ajaxLoad('"+params+"&pageNum="+nextPage+"&totalRows="+totalRows+"');\">></a></span> ";
			}

			if(pageNum == totalPages){
				pagingtext +="<span id=\"last\">>|</span>";
			}else{
				pagingtext +="<span id=\"last\"><a href=\"javascript:ajaxLoad('"+params+"&pageNum="+totalPages+"&totalRows="+totalRows+"');\">>|</a></span>";
			}
			if(pageNum-1 > totalPages){
				pagingtext = "<span id=\"first\">|<</span> <span id=\"previous\"><</span> <span id=\"text\">Page 1 of 1</span> <span id=\"next\">></span> <span id=\"last\">>|</span>";
			}


			document.getElementById('media_list').innerHTML = temptext[1];
			}
	}
	params = param;
	var file = "/admin/querylist.php?" + param;
	//ajaxRequest.abort();
	ajaxRequest.open("GET", file, true);
	ajaxRequest.send(null);
}
var params = "";



function popUp(URL, width, height) {
	var popup_w = 600;
	var popup_h = 400;
	if(width != ""){
		popup_w = width;
	}
	if(height != ""){
		popup_h = height;
	}
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, 'Popup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+popup_w+",height="+popup_h+",left = 340,top = 200');");
}

function clearClick() {
	try{
		var q = document.getElementById("q");
		q.onfocus = function() {
			this.value='';
			this.onfocus = "";
			return false;
		}
	}catch(e){
		
	}
}
//window.onload=clearClick;

function calendarToggle(id, count){
		for(var n =0; n < count; n++){
			document.getElementById("monthTable"+n).className = "calendar hide";
		}
		for(var n =0; n < count; n++){
			document.getElementById(id).className = "calendar";
		}
		
}

function loadconfigReplace(){
	
}