﻿// -----------------------------------------------------------------------------------
//
//	Generic Web Service Functionality used by all WSAJAX services
//	by Jonathan Lawson & Web Divalicious
//	Last Modification: ummm how's about you check the file date for that info
//
//	For more information, visit: the IT Dungeon
//
// -----------------------------------------------------------------------------------

/*------------------------------- Catalog Functionality -------------------------------*/
//load catalog (used for menu bar)
function LoadCatalog(catID, catArea)
{
    //alert("catID: " + catID + "\ncatArea: " + catArea);
    window.location.href="_ShopHandler.aspx?_Catid=" + catID + "&Group=" + catArea;
}

/*------------------------------- Product Functionality -------------------------------*/
//load catalog (used for menu bar)
function LoadProduct(ID, catArea)
{
    window.location.href="_ShopHandler.aspx?_ProductID=" + ID + "&Group=" + catArea;
}

/*---------------------------------- Page Variables -----------------------------------*/
//programatically include a js file
function IncludeJSFile(jsFile)
{
  document.write('<script type="text/javascript" src="'+ jsFile + '"></scr' + 'ipt>'); 
}


/*IF IE ERROR PERSISTS... TRY THIS: 
//http://clientside.cnet.com/code-snippets/manipulating-the-dom/ie-and-operation-aborted/
var load_method = (window.ie ? 'load' : 'domready');//"load";//
function IncludeFunctionOnLoad2(newLoadFunction)
{
    //seems that IE doesn't use either of these methods, yet it still executes the function      
    //maybe based off the fact that it's passes as a var???  

    //also, seems that FF throws an error that doesn't seem to break the page:
    //Error: uncaught exception: [Exception... "Could not convert JavaScript argument"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: http://localhost/Microsoft/Script/WSAJAX.js :: IncludeFunctionOnLoad :: line 27"  data: no]

    if(1==0&& window.addEventListener ) {alert("window.addEventListener");
        window.addEventListener(load_method,newLoadFunction,false);
    }else if (window.attachEvent){alert("window.attachEvent");//IE exclusive method for binding an event HOWEVER, it still throws Operation Aborted error for JL
        window.attachEvent("onload", newLoadFunction);
    }else if( document.addEventListener ) {alert("document.addEventListener");
        document.addEventListener(load_method,newLoadFunction,false);
    }
} */


//programatically include a function with window.onload
function IncludeFunctionOnLoad(newLoadFunction)
{
    var oldonload = window.onload;
    
//    if (typeof window.onload != 'function')
//        window.onload = newLoadFunction;
//    else 
//    {
        window.onload = function() 
        {
            if (oldonload)
                oldonload();

            newLoadFunction;
        }
//    }
}

//find an ID that may be set by an ASP element
function GetDivID(id)
{
    var divs=document.getElementsByTagName('div');
    
    for(var i=0; i < divs.length; i++)
    {
        if(divs[i].id.indexOf(id)>0)
            return divs[i].id;
    }
}

function GetInputID(id)
{
    var divs=document.getElementsByTagName('input');
    
    for(var i=0; i < divs.length; i++)
    {
        if(divs[i].id.indexOf(id)>0)
            return divs[i].id;
    }
}

//find an ID that may be set by an ASP element
function GetSpanID(id)
{
    var spans=document.getElementsByTagName('span');

    for(var i=0; i < spans.length; i++)
    {
        if(spans[i].id.indexOf(id)>0)
            return spans[i].id;
    }
}

//find an ID that may be set by an ASP element
function GetImageID(id)
{
    var img=document.getElementsByTagName('img');

    for(var i=0; i < img.length; i++)
    {
        if(img[i].id.indexOf(id)>0)
            return img[i].id;
    }
}

//update the final div accounting for any failure and reset text
function setElement(args, divID, failText)
{
    if(args.toString().indexOf("fail") == 0)    //check for failure
        document.getElementById(divID) ? 
            document.getElementById(divID).innerHTML = failText : null;
    else                                        //display contact info
        document.getElementById(divID) ? 
            document.getElementById(divID).innerHTML = args : null;
}

/***********************************************
* Drag and Drop Script: © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
*
* (modified for dynamic use)
*
***********************************************/
var thisDragObjectID = "";

var dragobject={
    z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0, DragObjectID : "",
    initialize:function(DragObjectID){
        thisDragObjectID=DragObjectID;//set ID of popup to drag

        document.onmousedown=this.drag;
        document.onmouseup=function(){this.dragapproved=0;}
    },
    drag:function(e){
        var evtobj=window.event? window.event : e;
        this.targetobj=window.event? event.srcElement : e.target;
        this.DragObjectID=GetDivID(thisDragObjectID);
        
        if ((this.targetobj.className=="drag")||(this.targetobj.className=="modalPopup1")){
            this.dragapproved=1

            //set ID of popup to drag
            document.getElementById(this.DragObjectID) ? this.targetobj=document.getElementById(this.DragObjectID) : alert("no ID: "+this.DragObjectID);

            if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left='100px'}
            if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top='0px'}
            
            this.offsetx=parseInt(this.targetobj.style.left)
            this.offsety=parseInt(this.targetobj.style.top)
            this.x=evtobj.clientX
            this.y=evtobj.clientY
            
            if (evtobj.preventDefault)
                evtobj.preventDefault()
            
            document.onmousemove=dragobject.moveit
        }
    },
    moveit:function(e){
        var evtobj=window.event? window.event : e
        if (this.dragapproved==1){
            //set ID of popup to drag
            document.getElementById(this.DragObjectID) ? this.targetobj=document.getElementById(this.DragObjectID) : null;
            
            this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
            this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
            
            return false
        }
    }
}

dragobject.initialize();


/***********************************************
* Restrict Duplicate Postback - double click on submit button
* much props to Dave Ward at: 
* http://encosia.com/2007/01/16/css-style-as-ajax-progress-indicator/
*
* (modified for dynamic use)
*
***********************************************/
/*
var prm = Sys.WebForms.PageRequestManager.getInstance();

// Using that prm reference, hook _initializeRequest and _endRequest, 
//to run our code at the begin and end of any async postbacks that occur.
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

// Executed anytime an async postback occurs.
function InitializeRequest(sender, args) 
{
    // Change the Container div's CSS class to .Progress.
    //$get('Container').className = 'Progress';
    $get(args._postBackElement.id).className = 'BTN_Submit_Blue_LG';

    // Get a reference to the element that raised the postback,
    //   and disables it.
    $get(args._postBackElement.id).disabled = true;
}

// Executed when the async postback completes.
function EndRequest(sender, args) 
{
    // Change the Container div's class back to .Normal.
    //$get('Container').className = 'Normal';
    $get(sender._postBackSettings.sourceElement.id).className = 'BTN_Submit_Green';

    // Get a reference to the element that raised the postback
    //   which is completing, and enable it.
    $get(sender._postBackSettings.sourceElement.id).disabled = false;
}
*/


/*---------------------------------- WSAJAX JS Includes -----------------------------------*/
IncludeJSFile("Script/ProcessQuickView.js");
IncludeJSFile("Script/GetProduct.js");
IncludeJSFile("Script/MyAccount.js");
IncludeJSFile("Script/GetCart.js");
IncludeJSFile("Script/GetAllotment.js");

//don't think we use these anywhere now that cart is changed
//IncludeJSFile("Script/ShippingInfo.js");
//IncludeJSFile("Script/PromoCodes.js");
