﻿// JScript File

// create object type (display ad, company, category, brand, plant)
// call web service, load object attributes (determined by browser)
// display using CSS?

var CDAS_WS = 'http://' + window.location.host + '/websvc/Search.asmx'; //'http://localhost/websvc/Search.asmx';
var authKey = '00B22CBEE2BF4FE398B331514524EB18'; //'57B731594902461B9FCAA54A0BE0AF1B';
var SOAPEnvHeader = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
var SOAPAuthHeader = "<soap:Header><Authentication xmlns='http://www.10-20media.com'><Key>" + authKey + "</Key></Authentication></soap:Header>";
var xh;

var CDAS = new _CDAS();

function _CDAS()
{
    this.Status = -1;
    this.errorText = "";
    this.onStatusUpdate;
    this.Categories = new Array();
    this.aSync = false;    
    this.loadCategories =
    function (code, keywords)
    {        
        xh = getXmlHTTP();
        var sReq;
        var sArgs;         
        if (this.Categories.length>0) this.Categories = new Array();
                           
        sArgs = "<NetworkCode>" + code + "</NetworkCode><Keywords>" + keywords + "</Keywords>";
        
        if (this.aSync) xh.onreadystatechange = _loadCategories;    
        xh.open("POST",CDAS_WS,this.aSync);
        xh.setRequestHeader("Content-Type","text/xml");
        xh.setRequestHeader("SOAPAction","http://www.10-20media.com/getCategories");     
       
        sReq = SOAPEnvHeader + SOAPAuthHeader; 
        sReq += "<soap:Body>\
        <getCategories xmlns='http://www.10-20media.com'>" + sArgs + "\
        </getCategories>\</soap:Body>\</soap:Envelope>";
           
        xh.send(sReq);
        this.Status = 0;  
        if (!this.aSync) _loadCategories();
                
    }        
    
    this.Companies = new Array(); 
    this.loadCompanies =
    function (code,search_type,keywords,categoryID,plantID,brandID,geo_type,state,zipcode,mile_radius,max_results,sort_by,sort_order,results_per,page_index)
    {    
        xh = getXmlHTTP();
        var sReq;
        var sArgs;
        if (this.Companies.length>0) this.Companies = new Array();
        
        if (mile_radius=='')
            mile_radius=0;
        if (max_results=='')
            max_results=100;
        if (results_per=='')
            results_per=25;
        if (page_index=='')
            page_index=1;
        
        sArgs = "<NetworkCode>" + code + "</NetworkCode>\
            <SearchType>" + search_type + "</SearchType>\
            <Keywords>" + keywords + "</Keywords>\
            <CategoryID>" + categoryID + "</CategoryID>\
            <PlantID>" + plantID + "</PlantID>\
            <BrandID>" + brandID + "</BrandID>\
            <GeoType>" + geo_type + "</GeoType>\
            <State>" + state + "</State>\
            <Zipcode>" + zipcode + "</Zipcode>\
            <MileRadius>" + mile_radius + "</MileRadius>\
            <MaxResults>" + max_results + "</MaxResults>\
            <SortBy>" + sort_by + "</SortBy>\
            <SortOrder>" + sort_order + "</SortOrder>\
            <PageSize>" + results_per + "</PageSize>\
            <PageIndex>" + page_index + "</PageIndex>";

        if (this.aSync) xh.onreadystatechange = _loadCompanies;        
        xh.open("POST",CDAS_WS,this.aSync);
        xh.setRequestHeader("Content-Type","text/xml");
        xh.setRequestHeader("SOAPAction","http://www.10-20media.com/getCompanies");
        //xh.setRequestHeader("Authentication",authKey);     
       
        sReq = SOAPEnvHeader + SOAPAuthHeader; 
        sReq += "<soap:Body>\
        <getCompanies xmlns='http://www.10-20media.com'>" + sArgs + "\
        </getCompanies>\</soap:Body>\</soap:Envelope>";
                
        xh.send(sReq);
        this.Status = 0;
        if (!this.aSync) _loadCompanies();
    }             
    
    
    this.Ad = new cdasAd();    
    this.loadAd =     
    function (code,type,keywords,categoryID,plantID,brandID,state,zip,city)
    {    
        xh = getXmlHTTP();
        var sReq;
        var sArgs;        
        
        if (!keywords) var keywords='';
        if (!categoryID) var categoryID = '';
        if (!plantID) var plantID = '';
        if (!brandID) var brandID = '';
        if (!state) var state = '';
        if (!zip) var zip ='';
        if (!city) var city = '';
        
        sArgs = "<NetworkCode>" + code + "</NetworkCode>\
            <TypeID>" + type + "</TypeID>\
            <Keywords>" + keywords + "</Keywords>\
            <MarketID></MarketID>\
            <CategoryID>" + categoryID + "</CategoryID>\
            <PlantID>" + plantID + "</PlantID>\
            <BrandID>" + brandID + "</BrandID>\
            <State>" + state + "</State>\
            <Zipcode>" + zip + "</Zipcode>\
            <City>" + city + "</City>";
        
        if (this.aSync) xh.onreadystatechange = _loadAd;
        xh.open("POST",CDAS_WS,this.aSync);
        xh.setRequestHeader("Content-Type","text/xml");
        xh.setRequestHeader("SOAPAction","http://www.10-20media.com/getAd");
        //xh.setRequestHeader("Authentication",authKey);     
              
        sReq = SOAPEnvHeader + SOAPAuthHeader; 
        sReq += "<soap:Body>\
        <getAd xmlns='http://www.10-20media.com'>" + sArgs + "\
        </getAd>\</soap:Body>\</soap:Envelope>";
                     
        xh.send(sReq); 
        this.Status = 0;   
        if (!this.aSync) _loadAd();        
    }

    
    this.Plants = new Array();
    this.reset = function()
    {
        this.Categories = new Array();
        this.Companies = new Array();
        this.Ad = new cdasAd();
        this.Plants = new Array();
        this.onStatusUpdate = null;
        
    }
}    

function cdasAd(parameter)
{
    this.errorText = "";
    this.AdType = 0;
    this.SourceURL = "";
    this.Text = "";
    this.DestURL = "";
    this.Width = 0;
    this.Height = 0;   
    this.IsFlash = false; 
    this.print=
    function(newWin)
    {
        if (this.AdType!=0)
        {                              
            if (this.IsFlash)
            {
                document.write('<OBJECT codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\" ' +
                    ' height=' + this.Height + ' width=' + this.Width + ' align=textTop class=\"CDAS_ad\" ' + 
                    ' classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 VIEWASTEXT id=ShockwaveFlash1>' + 
			        '<PARAM NAME=\"Movie\" VALUE=\"' + this.SourceURL + ' \">' +
			        '<PARAM NAME=\"Src\" VALUE=\"' + this.SourceURL + '\">' + 
			        '<PARAM NAME=\"WMode\" VALUE=\"Window\">' + 
			        '<PARAM NAME=\"Loop\" VALUE=\"-1\">' + 
			        '<PARAM NAME=\"Quality\" VALUE=\"High\">' + 
			        '<PARAM NAME=\"Menu\" VALUE=\"-1\">' + 
			        '<PARAM NAME=\"Base\" VALUE=\"\">' + 
			        '<PARAM NAME=\"Scale\" VALUE=\"ShowAll\">' + 
			        '<PARAM NAME=\"DeviceFont\" VALUE=\"0\">' + 
			        '<PARAM NAME=\"EmbedMovie\" VALUE=\"0\">' + 
			        '<PARAM NAME=\"BGColor\" VALUE=\"\">' + 
			        '<PARAM NAME=\"SWRemote\" VALUE=\"\">' + 
			        '<embed src=\"' + this.SourceURL + '\" quality=\"high\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application\/x-shockwave-flash\" width=\"' + this.Width + '\" height=\"' + this.Height + '\" align=\"texttop\">' + 
                    '</OBJECT>');
            }
            else
            {
                document.write('<a href=\"' + this.DestURL + '\" ');
                if (newWin)
                    document.write (' target=\"_blank\" ');
                
                document.write('><img class=\"CDAS_ad\" src=\"' + this.SourceURL + '\" width=\"' + this.Width + '\" height=\"' + this.Height + '\"></a>');
            }
        }
        if (this.errorText!="")
            document.write('<span class=\"cdas_err\">' + this.errorText + '</span>');
    }
}

function cdasCompanies(parameter)
{
    this.List = new Array();
    this.errorText = "";
    this.print=
    function()
    {
        if (this.errorText=="")        
            for (var i=0;i<this.List.length;i++)
            {   document.write(i + ' : ');
                this.List[i].print();
            }
        else
            document.write('<span class=\"cdas_err\">' + this.errorText + '</span>');
        
    }
}

function cdasCompany(parameter)
{
    this.ID = "";
    this.Name = "";
    this.ListingLevel = 1;
    this.Address1 = "";
    this.Address2 = "";
    this.City = "";
    this.State = "";
    this.PostalCode = "";
    this.Phone1 = "";
    this.Phone2 = "";
    this.Website = "";
    this.Email = "";
    this.Description = "";
    this.LogoSource = "";
    this.SmallLogoSource = "";
    this.TagLine = "";
    //public string[] Attributes;
    this.Distance = 0.0;
    this.print=
    function()
    {
        document.write(this.Name + '<br/>' + this.Address1 + ' ' + this.City + ', ' + this.State + ' ' + this.Zipcode + '</br>');
    }
}

function cdasCategory(parameter)
{
    this.ID = "";
    this.Name = "";
    this.Relevance = 0;
}

function cdasPlant(parameter)
{
    this.ID = "";
    this.Name = "";
    this.Relevance = 0;    
}



function _loadAd()
{
    if (xh.readyState==4)
    {
        // validate if no errors, etc.
        if (xh.status!="200")
        {            
            CDAS.errorText += selectSingleNode(xh.responseXML,"//faultcode");//xh.responseXML.selectSingleNode("//faultcode").text;
            CDAS.Status = -1;
                    
        }
        else
        {        
            oAd = CDAS.Ad;
            var adXML = xh.responseXML;    
            CDAS.errorText = "";        
                                                                                         
            oAd.AdType = selectSingleNode(adXML,"//AdType");//.getElementsByTagName("AdType")[0].firstChild.data;// adXML.selectSingleNode("//AdType").text;
            if (oAd.AdType>0)
            {
                oAd.SourceURL = selectSingleNode(adXML,"//SourceURL");//adXML.getElementsByTagName("SourceURL")[0].firstChild.data;// adXML.selectSingleNode("//SourceURL").text;
                //oAd.Text = adXML.selectSingleNode("//Text").text;
                oAd.DestURL = selectSingleNode(adXML,"//DestURL");//adXML.getElementsByTagName("DestURL")[0].firstChild.data;//adXML.selectSingleNode("//DestURL").text;
            
                oAd.Width = selectSingleNode(adXML,"//Width");//adXML.getElementsByTagName("Width")[0].firstChild.data;//adXML.selectSingleNode("//Width").text;
                oAd.Height = selectSingleNode(adXML,"//Height");//adXML.getElementsByTagName("AdType")[0].firstChild.data;//adXML.selectSingleNode("//Height").text;                                        
                if (selectSingleNode(adXML,"//IsFlash")=='true')
                    oAd.IsFlash=true;
                else
                    oAd.IsFlash=false;
                    
            }  
            
            CDAS.Status = 1;            
        }
                            
        //xh = null;
        if (CDAS.onStatusUpdate)
            CDAS.onStatusUpdate();                
     }
}

function _loadCategories()
{
    if (xh.readyState==4)
    {        
        // validate if no errors, etc.
        if (xh.status!="200")
        {     
            // how do we handle errors here?          
            CDAS.errorText += selectSingleNode(xh.responseXML,"//faultcode");//xh.responseXML.selectSingleNode("//faultcode").text;  
            CDAS.Status = -1;               
        }
        else
        {                
            var catXML = xh.responseXML;    
            var CATs = catXML.getElementsByTagName("Category");
                                    
            for (iCAT=0;iCAT<CATs.length;iCAT++)
            {
                
                var oCAT = new cdasCategory;
                oCAT.ID = selectSingleNode(CATs[iCAT],"ID");//CATs[iCAT].selectSingleNode("ID").text;
                oCAT.Name = selectSingleNode(CATs[iCAT],"Name");//selectSingleNode("Name").text;
                oCAT.Relevance = selectSingleNode(CATs[iCAT],"Relevance");//CATs[iCAT].getElementsByTagName("Relevance")[0].firstChild.data;//CATs[iCAT].selectSingleNode("Relevance").text;
              
                CDAS.Categories.push(oCAT);
                
            }
            
            CDAS.Status = 1;
        }        
        //xh = null;
        if (CDAS.onStatusUpdate)
            CDAS.onStatusUpdate();                
    }
}

function _loadCompanies()     
{
    
    if (xh.readyState==4)
    {
    
        // validate if no errors, etc.
        if (xh.status!="200")
        {     
            // how do we handle errors here?          
            CDAS.errorText += selectSingleNode(xh.responseXML,"//faultcode");//xh.responseXML.selectSingleNode("//faultcode").text;                 
            CDAS.Status = -1;
        }
        else
        {        
            var coXML = xh.responseXML;    
            var COs = coXML.getElementsByTagName("Company");
            
            for (iCO=0;iCO<COs.length;iCO++)
            {
                var oCO = new cdasCompany;
                oCO.ID = selectSingleNode(COs[iCO],"ID");//COs[iCO].selectSingleNode("ID").text;
                oCO.Name = selectSingleNode(COs[iCO],"Name");//COs[iCO].selectSingleNode("Name").text;
                oCO.ListingLevel = selectSingleNode(COs[iCO],"ListingLevel");//COs[iCO].selectSingleNode("ListingLevel").text;
                oCO.Address1 = selectSingleNode(COs[iCO],"Address1");//COs[iCO].selectSingleNode("Address1").text;
                oCO.Address2 = selectSingleNode(COs[iCO],"Address2");//COs[iCO].selectSingleNode("Address2").text;
                oCO.City = selectSingleNode(COs[iCO],"City");//COs[iCO].selectSingleNode("City").text;
                oCO.State = selectSingleNode(COs[iCO],"State");//COs[iCO].selectSingleNode("State").text;
                oCO.Zipcode = selectSingleNode(COs[iCO],"PostalCode");//COs[iCO].selectSingleNode("PostalCode").text;
                oCO.Phone1 = selectSingleNode(COs[iCO],"Phone1");//COs[iCO].selectSingleNode("Phone1").text;
                oCO.Phone2 = selectSingleNode(COs[iCO],"Phone2");//COs[iCO].selectSingleNode("Phone2").text;
                oCO.Website = selectSingleNode(COs[iCO],"Website");//COs[iCO].selectSingleNode("Website").text;
                oCO.Email = selectSingleNode(COs[iCO],"Email");//COs[iCO].selectSingleNode("Email").text;
                oCO.Description = selectSingleNode(COs[iCO],"Description");
                oCO.SmallLogoSource = selectSingleNode(COs[iCO],"SmallLogoSource");
                
                CDAS.Companies.push(oCO);            
            }
            CDAS.Status = 1;
        }                    
        //xh=null;   
        if (CDAS.onStatusUpdate)
            CDAS.onStatusUpdate();                             
    }
}



function loadPlants(code,keywords,name_type)
{
    var xh = getXmlHTTP();
    var sReq;
    var sArgs;
    var oPLTs = new cdasPlants;   
        
    sArgs = "<NetworkCode>" + code + "</NetworkCode><Keywords>" + keywords + "</Keywords><NameType>" + name_type + "</NameType>";

    xh.onreadystatechange = _loadPlants(code,keywords,name_type);     
    xh.open("GET",CDAS_WS,true);
    xh.setRequestHeader("Content-Type","text/xml");
    xh.setRequestHeader("SOAPAction","http://www.10-20media.com/getPlants");     
   
    sReq = SOAPEnvHeader + SOAPAuthHeader; 
    sReq += "<soap:Body>\
    <getPlants xmlns='http://www.10-20media.com'>" + sArgs + "\
    </getPlants>\</soap:Body>\</soap:Envelope>";
    
    //alert(sReq);
    xh.send(sReq);
    
    // validate if no errors, etc.
    if (xh.status!="200")
    {     
        // how do we handle errors here?          
        oPLTs.errorText = xh.responseXML.selectSingleNode("//faultcode").text;                 
    }
    else
    {                
        var PLTXML = xh.responseXML;    
        var PLTs = PLTXML.getElementsByTagName("PLTegory");
        
        for (iPLT=0;iPLT<PLTs.length;iPLT++)
        {
            var oPLT = new cdasPlant;
            oPLT.ID = PLTs[iPLT].selectSingleNode("ID").text;
            oPLT.Name = PLTs[iPLT].selectSingleNode("Name").text;
            oPLT.Relevance = PLTs[iPLT].selectSingleNode("Relevance").text;
          
            oPLTs.List.push(oPLT);
            
        }
        
    }
                
    return oPLTs;
}

function getXmlHTTP()
{
    var xmlHTTP;
    
    try
    {    // Firefox, Opera 8.0+, Safari    
    xmlHTTP=new XMLHttpRequest();                 
    }
    catch (e)
    {    // Internet Explorer   
        try
        {
        xmlHTTP=new ActiveXObject("Msxml2.XMLHTTP");      
        }
        catch (e)
        {   
            try
            {
            xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP");        
            }
            catch (e)
            {        
            return null;        
            }      
         }    
    }  
    
    return xmlHTTP;   
} 

function selectSingleNode(xmlDoc, elementPath)
{
    if(window.ActiveXObject)
    {
        if (xmlDoc.selectSingleNode(elementPath))
            return xmlDoc.selectSingleNode(elementPath).text;
        else
            return '';
    }
    else
    {
       //var xpe = new XPathEvaluator();
       //var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
       //var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);       
       //var results = xmlDoc.evaluate(elementPath, xmlDoc, null, 9, null);    
       elementPath = elementPath.replace("//","");   
       if (xmlDoc.getElementsByTagName(elementPath)[0].firstChild)
            return xmlDoc.getElementsByTagName(elementPath)[0].firstChild.data;
       else
            return '';//null;
       //return results.singleNodeValue; 
    }
}


