﻿
    var xmlRequester = null;
    var READY_STATE_SUCCESS = 4;
    var RPC_Initialized = false;
    var RPC_Busy = false;

    function callAsyncRPC(rpcPath,sXML,callBackFunc)
    {
        try
        {
            xmlRequester.open("POST", rpcPath, true);
            if (trim(sXML)=="")
                sXML = "<XML></XML>";
            xmlRequester.onreadystatechange = callBackFunc;
            xmlRequester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequester.send("XML=" + escape(sXML.replace(/\+/g,"%2B")));
        }
        catch(ex)
        {
            //alert(ex.message);
        }
    }  
        
    function callRPC(rpcPath,sXML)
    {
        var JSONReturn;
        try
        {
            xmlRequester.open("POST", rpcPath, false);
            if (trim(sXML)=="")
                sXML = "<XML></XML>";
            xmlRequester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequester.send("XML=" + escape(sXML.replace(/\+/g,"%2B")));
            JSONReturn = eval("(" + xmlRequester.responseText + ")");
        }
        catch(ex)
        {
            alert("error: " + ex.message);
        }        
        
        return JSONReturn;
    }    
    
    function InitRPC()
    {
        if (window.XMLHttpRequest)
        {
            xmlRequester = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            xmlRequester = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
        RPC_Initialized = true;     
    }

    function trim(sBuf)
    {
        if (!sBuf)
            return "";
        //var iNum = parseInt(sBuf);
        //if (!isNaN(iNum))
            //return iNum.toString();
        while (sBuf.lastIndexOf(" ") == sBuf.length - 1) 
        {
            sBuf = sBuf.substring(0,sBuf.lastIndexOf(" "));
            if (sBuf == "")
                return sBuf;
        }
        while (sBuf.indexOf(" ") == 0) 
        {
            sBuf = sBuf.substring(1,sBuf.length);
        }
        return sBuf;
    }      

    function getBetweenText(sBuffer, sStartText, sEndText)
    {
	    if((sBuffer.indexOf(sStartText) > -1) && (sBuffer.indexOf(sEndText) > -1))
	    {
		    return(sBuffer.substring(sBuffer.indexOf(sStartText) + sStartText.length, sBuffer.indexOf(sEndText)));
	    }
	    else
	    {
		    return(null);
	    }
    }
    
    function appendOptionToSelect(theVal, theText, selectName)
    {
      var elOptNew = document.createElement('option');
      elOptNew.text = theText;
      elOptNew.value = theVal;
      var elSel = document.getElementById(selectName);

      try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
      }
      catch(ex) {
        elSel.add(elOptNew); // IE only
      }
    }

    function clearDropDown(selectName)
    {
        document.getElementById(selectName).options.length = 0;    
    }     

    function findPos(obj) 
    {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft,curtop];
    }


    function getRadioSelectedValue(radioList)
    {
        var options = radioList.getElementsByTagName('input');
        for(var i=0;i<options.length;i++)
        {
            var opt = options[i];
            if(opt.checked)
            {
                return opt.value;
            }
        }
    }

    function AddMailingList() {
        var theEmail = trim(document.getElementById("mailListBox").value);

        if (theEmail.length == 0) {
            alert("You must enter an email address");
            return;
        }

        var sXml = "<Contents>";
        sXml = sXml + "<Type>MAILING_LIST</Type>";
        sXml = sXml + "<Email><![CDATA[" + theEmail + "]]></Email>";
        sXml = sXml + "</Contents>";

        callAsyncRPC(AppRoot + 'AJAXCaller.aspx', sXml, MailingListCallBack);
    }

    function MailingListCallBack() {
        //get return XML
        var ret;
        if (xmlRequester.readyState == 4) {
            ret = eval("(" + xmlRequester.responseText + ")");
        }
        else {
            return;
        }

        if (ret.error != "") {
            alert(ret.error);
        }
        else {
            alert(ret.ReturnMessage);
        }
    }

    function AddToCart(theProdID, theSize, theColor, theQuantity) {
        if (theSize == "")
            theSize = "0";

        if (theColor == "")
            theColor = "0";

        if (theQuantity == "")
            theQuantity = "1";

        if (theProdID == "") {
            alert('Product ID Not Supplied');
            return;
        }

        var sUrl = AppRoot + "ShoppingCart.aspx?addProdID=" + theProdID + "&addSizeID=" + theSize + "&addColorID=" + theColor + "&addQuantity=" + theQuantity;
        window.location.href = sUrl;
    }

    function AddToCartText(theProdID, theSize, theColor, theQuantity) {

        theSize = escape(theSize);
        theColor = escape(theColor);

        if (theQuantity == "")
            theQuantity = "1";

        if (theProdID == "") {
            alert('Product ID Not Supplied');
            return;
        }

        var sUrl = AppRoot + "ShoppingCart.aspx?addProdID=" + theProdID + "&addSize=" + theSize + "&addColor=" + theColor + "&addQuantity=" + theQuantity;
        window.location.href = sUrl;
    }

    function QDAddToCart(ProductID) {
        var theSize = "";
        var theColor = "";
        var theQuantity = "1";

        var drpSize = document.getElementById("drpQDProdSizes");
        var drpColor = document.getElementById("drpQDProdColors");

        theSize = GetDropDownVal(drpSize);
        theColor = GetDropDownVal(drpColor);

        var theSizeText = GetDropDownText(drpSize);
        var theColorText = GetDropDownText(drpColor);

        var theQuanData = document.getElementById("hdnQuanData").value;
        if (IsVariantAvailable(theQuanData, theSizeText, theColorText) == false) {
            var theMsg = "We are sorry, but the item you have selected";
            if (theSizeText.length > 0)
                theSizeText = "in size '" + theSizeText + "'";
            if (theColorText.length > 0) {
                if (theSizeText.length > 0)
                    theColorText = " and color '" + theColorText + "'";
                else
                    theColorText = "in color '" + theColorText + "'";
            }
            theSizeText = theSizeText + theColorText;

            if (theSizeText.length > 0)
                theMsg = theMsg + " " + theSizeText;

            theMsg = theMsg + ", is currently not in stock.\r\n\r\nPlease select a different product, or email us at help@playfulthreads.com if you are interested in back-ordering.";

            alert(theMsg);
            return;
        }

        AddToCart(ProductID, theSize, theColor, theQuantity);
    }

    function IsVariantAvailable(theQuantityData, theSize, theColor) {
        var theReturn = false;
        var theOuterArray = theQuantityData.split("|");
        for (var x = 0; x < theOuterArray.length; x++) {
            var theInnerArray = theOuterArray[x].split("~");
            if (theSize == theInnerArray[0] && theColor == theInnerArray[1]) {
                if (theInnerArray[2] != "0" && theInnerArray[2] != "") {
                    theReturn = true;
                }
                else if (theInnerArray[3].length > 0) {
                    if (theInnerArray[3] != "" && theInnerArray[3] != "0") {
                        theReturn = true;
                    }
                }

                break;
            }
        }

        return theReturn;
    }

    function GetDropDownVal(drp) {
        if (drp) {
            var val = drp.options[drp.selectedIndex].value;
            return val;
        }
        else
            return "";
    }

    function GetDropDownText(drp) {
        if (drp) {
            var val = drp.options[drp.selectedIndex].text;
            return val;
        }
        else
            return "";
    }

    function bookmark(url, sitename) {
        var ns = "Netscape and FireFox users, use CTRL+D to bookmark this site."
        if ((navigator.appName == 'Microsoft Internet Explorer') &&
            (parseInt(navigator.appVersion) >= 4)) {
            window.external.AddFavorite(url, sitename);
        }
        else if (navigator.appName == 'Netscape') {
            alert(ns);
        }
    }


    function AddFan() {
        window.open(AppRoot + "tt.aspx");
        return true;
    }

    function AddTwitter() {
        window.open(AppRoot + "tt.aspx?twitter=1");
        return true;
    }

    function enterInBox(e) {
        var characterCode;

        if (e && e.which) {
            e = e;
            characterCode = e.which;
        }
        else {
            e = event;
            characterCode = e.keyCode;
        }

        if (characterCode == 13) {
            RunSearch();
            return false;
        }
        else {
            return true;
        }
    }

    var currentAdNum = 1;
    function TopImageClick() {
        if (currentAdNum == 1) {
            window.location.href = AppRoot + "exotic-dancewear.aspx";
        }
        else if (currentAdNum == 2) {
            window.location.href = AppRoot + "clubwear.aspx";
        }
        else {
            window.location.href = AppRoot + "plus-size.aspx";
        }
    }

    function BottomLinkClick(theAdNum) {
        if (theAdNum == 1) {
            window.location.href = AppRoot + "exotic-dancewear.aspx";
        }
        else if (theAdNum == 2) {
            window.location.href = AppRoot + "clubwear.aspx";
        }
        else {
            window.location.href = AppRoot + "plus-size.aspx";
        }
    }

    var timerID = null;
    function PauseRotate() {
        clearInterval(timerID);
    }

    function ResumeRotate() {
        timerID = setInterval("TopImageScroll()", 4000);
    }

    function TopImageScroll() {
        if (currentAdNum == 1) {
            document.getElementById('imgMain').src = "images/clubwear_splash.jpg";
            document.getElementById('imgMain').alt = "Clubwear & Lingerie At PlayfulThreads.com";
            document.getElementById('rotateDiv1').className = "rotateDisabled";
            document.getElementById('rotateDiv2').className = "rotate";
            document.getElementById('rotateDiv3').className = "rotateDisabled";
            currentAdNum = 2;
        }
        else if (currentAdNum == 2) {
            document.getElementById('imgMain').src = "images/plus_splash.jpg";
            document.getElementById('imgMain').alt = "Plus-Size Selections At PlayfulThreads.com";
            document.getElementById('rotateDiv1').className = "rotateDisabled";
            document.getElementById('rotateDiv2').className = "rotateDisabled";
            document.getElementById('rotateDiv3').className = "rotate";
            currentAdNum = 3;
        }
        else {
            document.getElementById('imgMain').src = "images/damce_splash.jpg";
            document.getElementById('imgMain').alt = "The Hottest Dancewear & Stripper Products At Playful Threads";
            document.getElementById('rotateDiv1').className = "rotate";
            document.getElementById('rotateDiv2').className = "rotateDisabled";
            document.getElementById('rotateDiv3').className = "rotateDisabled";
            currentAdNum = 1;
        }
    }	 
    
    
