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");
    var currentClass = thebutton.className.replace(/b[0|1]/, "");
    if(thecheck.checked){
        thecheck.checked = "";
        thebutton.className = currentClass + "b0";
    }else{
        thecheck.checked = "checked";
        thebutton.className = currentClass + "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);
    }
}

/*-------------------------------------------------------------*/
/* Capitalizes the first letter of the string
/*-------------------------------------------------------------*/
function ucfirst(str){
    var first = str.charAt(0).toUpperCase();
    str = first + str.substring(1);
    return 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(navigator.appName.indexOf("Microsoft") != -1){
        return window[moviename];   
    }else{
        return document[moviename];
    }
}

/*--------------------------------------------------*/
/*  Initializes 'clearClick' functionality for all 
/*  elements with the class 'clearClick'
/*--------------------------------------------------*/
function clearClick(){
    $(".clearClick").each(function(i,elem){
        var defaultText = $(elem).val();
        $(elem).attr("defaultText", defaultText);
        $(elem).focus(function(){
            if($(this).val() == $(this).attr("defaultText"))
                $(this).val("");
        });
        $(elem).blur(function(){
            if($(this).val() == "")
                $(this).val($(this).attr("defaultText"));
        });
    });
}

/*--------------------------------------------------*/
/*  retrieves current user's browser info 
/*--------------------------------------------------*/
function getBrowser(){
    var name = "";
    var version = jQuery.browser.version.charAt(0);
    if(jQuery.browser.safari) name = "safari";
    if(jQuery.browser.opera) name = "opera";
    if(jQuery.browser.msie){ name = "ie";
        if(navigator.userAgent.search("MSIE 8.0") != -1){
            version = 8;
        }else if(navigator.userAgent.search("MSIE 7.0") != -1){
            version = 7;
        }else{
            version = 6;
        }
    }
    if(jQuery.browser.mozilla) name = "firefox";
    
    if(navigator.appVersion.search("Chrome") != -1) name = "chrome";
    if(navigator.appVersion.search("Macintosh") != -1 && jQuery.browser.mozilla) name = "firefox-mac";
    
    var browser = {
        "name" : name,
        "version" : version
    };
    return browser;
}

function showHelp(id){
    $("#"+id).slideToggle();
}


/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only thing you need to change in this file is the following
variables: checkboxHeight, radioHeight and selectWidth.

Replace the first two numbers with the height of the checkbox and
radio button. The actual height of both the checkbox and radio
images should be 4 times the height of these two variables. The
selectWidth value should be the width of your select list image.

You may need to adjust your images a bit if there is a slight
vertical movement during the different stages of the button
activation.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "121";

/* No need to change anything after this */


var Custom = {
    init: function() {
        var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
        for(a = 0; a < inputs.length; a++) {
            if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
                span[a] = document.createElement("span");
                span[a].className = inputs[a].type;

                if(inputs[a].checked == true) {
                    if(inputs[a].type == "checkbox") {
                        position = "0 -" + (checkboxHeight*2) + "px";
                        span[a].style.backgroundPosition = position;
                    } else {
                        position = "0 -" + (radioHeight*2) + "px";
                        span[a].style.backgroundPosition = position;
                    }
                }
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                inputs[a].onchange = Custom.clear;
                span[a].onmousedown = Custom.pushed;
                span[a].onmouseup = Custom.check;
                document.onmouseup = Custom.clear;
            }
        }
        inputs = document.getElementsByTagName("select");
        for(a = 0; a < inputs.length; a++) {
            if(inputs[a].className == "styled") {
                option = inputs[a].getElementsByTagName("option");
                active = option[0].childNodes[0].nodeValue;
                textnode = document.createTextNode(active);
                for(b = 0; b < option.length; b++) {
                    if(option[b].selected == true) {
                        textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                    }
                }
                span[a] = document.createElement("span");
                span[a].className = "select";
                span[a].id = "select" + inputs[a].name;
                span[a].appendChild(textnode);
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                inputs[a].onchange = Custom.choose;
            }
        }
    },
    pushed: function() {
        element = this.nextSibling;
        if(element.checked == true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
        } else if(element.checked == true && element.type == "radio") {
            this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
        } else if(element.checked != true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
        } else {
            this.style.backgroundPosition = "0 -" + radioHeight + "px";
        }
    },
    check: function() {
        element = this.nextSibling;
        if(element.checked == true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 0";
            element.checked = false;
        } else {
            if(element.type == "checkbox") {
                this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
            } else {
                this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
                group = this.nextSibling.name;
                inputs = document.getElementsByTagName("input");
                for(a = 0; a < inputs.length; a++) {
                    if(inputs[a].name == group && inputs[a] != this.nextSibling) {
                        inputs[a].previousSibling.style.backgroundPosition = "0 0";
                    }
                }
            }
            element.checked = true;
        }
    },
    clear: function() {
        inputs = document.getElementsByTagName("input");
        for(var b = 0; b < inputs.length; b++) {
            if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
            } else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 0";
            } else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
            } else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 0";
            }
        }
    },
    choose: function() {
        option = this.getElementsByTagName("option");
        for(d = 0; d < option.length; d++) {
            if(option[d].selected == true) {
                document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
            }
        }
    }
}
window.onload = Custom.init;


