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");
	if(pagebody != null && pagebody != "undefined"){
		//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>'; 
			pagebody.appendChild(loadingdiv);
		}
	}
}
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');");
}

/*---------------------------------------------------
* String Trim Functions
*---------------------------------------------------*/
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

/*-------------------------------------------------------------*/
/* Modify "String" prototype for trim() functionality
/*-------------------------------------------------------------*/
String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/*---------------------------------------------------
* Get Browser's Width and Height
*---------------------------------------------------*/
function getScreenSize() {
	var w = 0;
	var h = 0;
	if(typeof(window.innerWidth) == 'number'){
		w = window.innerWidth;
		h = window.innerHeight;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return w+'x'+h;
}

/*-------------------------------------------------------------*/
/* Get the current X and Y coordinates of the user's mouse
/*-------------------------------------------------------------*/
function getMouseXY(e){
	var x = 0;
	var y = 0;
	if(!e) var e = window.event;
	if(e.pageX || e.pageY){
		x = e.pageX;
		y = e.pageY;
	}else{
		x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return x+"x"+y;
}

/*-------------------------------------------------------------*/
/* Checks whether or not the "ENTER" key was pressed
/*-------------------------------------------------------------*/
function isEnterKey(e){
	e = e || window.event;
	if(e.keyCode == 13){
		return true;
	}
	return false;
}
	
/*-------------------------------------------------------------*/
/* Fix for 'protected' Email javascript response text  
/*-------------------------------------------------------------*/
function insertEmail(id, alink){
	var elem = document.getElementById(id);
	elem.innerHTML = alink;
}

/*-------------------------------------------------------------*/
/* Checks whether string is a number
/*-------------------------------------------------------------*/
function isNumeric(str){
	if(isNaN(str))
		return false;
	else
		return true;
}

/*-------------------------------------------------------------*/
/* Converts a numeric STRING to an INT
/*-------------------------------------------------------------*/
function intval(str){
	if(isNaN(str)){
		return 0;
	}else{
		if(str.charAt(0) == '0' && str.length > 1){
			str = str.substring(1);	
		}
		return parseInt(str);
	}
}

/*-------------------------------------------------------------*/
/* Use jQuery to Fade In and Fade Out the specified element
/*-------------------------------------------------------------*/
function fadeInOut(elem, speed, timeout){
	$(elem).stop();
	$(elem).fadeIn(speed, function(){
		setTimeout('$("'+elem+'").fadeOut("'+speed+'")', timeout);
	});
}

/*-------------------------------------------------------------*/
/* Configure a Date object based on the given parameters
/*-------------------------------------------------------------*/
function mktime(hours, minutes, ampm, month, day, year){
	var dateObj = new Date();
	dateObj.setFullYear(intval(year), intval(month)-1, intval(day));
	hours = intval(hours);
	minutes = intval(minutes);
	if(ampm == "PM" && hours < 12){
		hours += 12;
	}else if(ampm == "AM" && hours == 12){
		hours = 0;
	}
	dateObj.setHours(hours, minutes);
	return dateObj;
}

/*-------------------------------------------------------------*/
/* Disable the "select" functionality for <ul> dropdown
/*-------------------------------------------------------------*/
function disableMenu(menu){
	$("#"+menu+" a.selection").unbind("click");
	$("#"+menu+" a.selection").unbind("focus");
	$("#"+menu+" a.selection").unbind("blur");
	$("#"+menu+" ul").unbind("mouseover");
	$("#"+menu+" ul").unbind("mouseout");
}

/*-------------------------------------------------------------*/
/* Enable the "select" functionality for <ul> dropdown
/*-------------------------------------------------------------*/
function enableMenu(menu){
	$("#"+menu+" a.selection").click(function(){
 		if($("#"+menu+" ul").attr("focusEnabled") != "true"){
			if($("#"+menu+" ul").css("display") == "block"){
				$(".select a.selection").css("position", "relative");
				$("#"+menu+" ul").hide();
			}else{
				$(".select a.selection").not(this).css("position", "static");
				$("#"+menu+" ul").show();
			}
		}
	});
	$("#"+menu+" a.selection").focus(function(){
		$(".select a.selection").not(this).css("position", "static");
		$("#"+menu+" ul").attr("focusEnabled", "true");
		$("#"+menu+" ul").show();
	});
	$("#"+menu+" a.selection").blur(function(){
		if($("#"+menu+" ul").attr("onMenu") == "true"){
			// do nothing
		}else{
			$(".select a.selection").css("position", "relative");
			$("#"+menu+" ul").hide();
		}
	});
	$("#"+menu+" ul").mouseover(function(){
		$(this).attr("onMenu","true");
	});
	$("#"+menu+" ul").mouseout(function(){
		$(this).attr("onMenu","false");
	});
}

/*-------------------------------------------------------------*/
/* Return DOM reference to the specified Flash object
/*-------------------------------------------------------------*/
function thisMovie(moviename){
	if(jQuery.browser.msie && jQuery.browser.version == "6.0"){
		return window[moviename];	
	}else{
		return document[moviename];
	}
}