﻿/*
    General Javascript Library
    License: Compunix LLC.
    Website: http://www.compunix.us/
    Creator: Adam Scholfield
    Last Modified: 11/09/2009
    Last Modified By: Adam Scholfield
*/

        
/* Shortcut Functions */
function Compunix_$(id)
{
    return document.getElementById(id);
}
/* Shortcut Functions */
/* Ajax Functions */
function sendAJAXRequest(url, params, callback_function)
{ 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.open('POST',url);
    
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState==4)
        { 
            callback_function(xmlHttp.responseText);
        }
    };
    
    //xmlHttp.setRequestHeader('Content-Type', 'text/html');
    xmlHttp.send(params);  
}

function GetXmlHttpObject()
{
    var xmlHttp1=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp1=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp1;
}
function AjaxCall(divid, url)
{
    sendAJAXRequest(url, '', function(response) {
            //Compunix_$(divid).outerHTML = '<div id="' + divid + '">' + response + '</div>';
            Compunix_$(divid).innerHTML = '' + response;
        });
}
/* Ajax Functions */
/* Key Control Functions */
function CatchEnter(e)
{ 
    var characterCode;
        if( e && e.which)
        { 
            e = e;
            e.cancel = true;
            characterCode = e.which;
        }
        if(e && window.event)
        {
            e = window.event;
            e.cancel = true;
            characterCode = e.keyCode;
        }
    return characterCode == 13;
}
/* Key Control Functions */
/* Cookie Functions */        
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) 
    {
        var c = ca[i];
        while (c.charAt(0)==' ') 
            c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length,c.length);
    }
    return '';
}

function eraseCookie(name) 
{
    createCookie(name,"",-1);
}
/* Cookie Functions */        
    
/*postCodeAnywhere methods*/
function postCodeAnywhereInlineJSTag(accountCode, licenseCode, action, parameter2DArray, callBack_functionName)
{
    var scriptTag = document.getElementById("pcaScript");
    var headTag = document.getElementsByTagName("head").item(0);
    //Build the url
    var strUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?callback=" + callBack_functionName;
    strUrl += "&license_code=" + licenseCode;
    strUrl += "&account_code=" + accountCode;
    strUrl += "&action=" + action;
    for(var index=0;index<parameter2DArray.length;index++)
    {
        if(parameter2DArray[index][0].length > 0)
        {
            strUrl += '&' + parameter2DArray[index][0] + '=' + escape(parameter2DArray[index][1]);
        }
    }
    //Make the request
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScript";
    headTag.appendChild(scriptTag);
} 
    
/**********************************
    Custom Functions go here
    Custom Software : PostCodeAnywhere DealerLocator
    Date: 1st Quarter 2010
    Creator: Adam Scholfield
    License: Adam Scholfield, Compunix LLC.
*********************************/    
var orderNumber;
var billingZip;   
var accountCode;
var licenseCode;
var numberOfItems = '3';
 function loadDealers()
 {
    if(billingZip.length > 0)
    {
        var action = 'stored_nearest';
        var parameters = [['origin',escape(billingZip.split(' ')[0])],['items','3']];
        //alert(billingZip.split(' ')[0]);
        postCodeAnywhereInlineJSTag(accountCode, licenseCode, action, parameters, 'assignResults');
    }
}
function assignResults()
{
    if (pcaIsError)
    {
        //alert(pcaErrorMessage);
        Compunix_$('DealerSelection').style.display = "none";
    }
    else
    {
        if (pcaRecordCount==0)
        {
            //alert("Sorry, no matching items found");
            Compunix_$('DealerSelection').style.display = "none";
        }
        else
        {
            assignResult(0);
            assignResult(1);
            assignResult(2);
            chooseFirst();
        }
    }
    Compunix_$('DealerListLoadingSpinner').className = "";
    Compunix_$('DealerListLoadingSpinner').style.display = "none";
    Compunix_$('DealerListLoadingOverLay').className = "";
}
function assignResult(index)
{
    Compunix_$('Dealer'+index+'radio').value = pca_id[index];
    Compunix_$('Dealer'+index+'Name').innerHTML = pca_name[index] 
    Compunix_$('Dealer'+index+'Description').innerHTML = pca_description[index];
    Compunix_$('Dealer'+index+'Distance').innerHTML = pca_distance[index];
}
 function chooseFirst()
 {
    Compunix_$('Dealer0radio').checked = true;
    setDealer();
 }
 function setDealer()
 {
    if(orderNumber.length > 0)
    {
        var id;
        if(Compunix_$('Dealer0radio').checked)
            id = Compunix_$('Dealer0radio').value;
        if(Compunix_$('Dealer1radio').checked)
            id = Compunix_$('Dealer1radio').value;
        if(Compunix_$('Dealer2radio').checked)
            id = Compunix_$('Dealer2radio').value;
        sendAJAXRequest("ajaxCompunixPostCodeAnywhereReuest.aspx?DealerID="+escape(id)+"&OrderNumber="+escape(orderNumber), '', function(response){});
    }
 }
 function findDealers()
 {
    if(billingZip.length > 0)
    {
        Compunix_$('DealerListLoadingSpinner').style.display = "none";
        var action = 'stored_nearest';
        var parameters = [['origin',escape(billingZip)],['items',numberOfItems]];
        postCodeAnywhereInlineJSTag(accountCode, licenseCode, action, parameters, 'listDealers');
    }
 }
 function listDealers()
 {
    if (pcaIsError)
    {
        //alert(pcaErrorMessage);
        //Compunix_$('DealerSelection').style.display = "none";
        Compunix_$('DealerSelection').innerHTML = "Post code not found!";
    }
    else
    {
        if (pcaRecordCount==0)
        {
            //alert("Sorry, no matching items found");
            //Compunix_$('DealerSelection').style.display = "none";
            Compunix_$('DealerSelection').innerHTML = "Post code not found!";
        }
        else
        {
            var innerhtml = '<table class="DealerTable">';
            innerhtml += '<tr>';
            innerhtml += '<td class="Title">Name</td><td class="Title">Description</td><td class="Title">Distance (miles)</td><td class="Title">View Map</td>';
            innerhtml += '</tr>';    
            for(var index=0;index<pca_id.length;index++)
            {
                innerhtml += '<tr>';
                innerhtml += '<td class="NameCol">'+pca_name[index]+'</td><td class="DescriptionCol">'+pca_description[index]+'</td><td class="DistanceCol">'+pca_distance[index]+'</td><td class="MapCol"><a href="#" onclick="javascript:ShowMap(\''+pca_name[index]+'\','+pca_latitude[index]+','+pca_longitude[index]+');">View Map</a></td>';
                innerhtml += '</tr>'; 
            }
            innerhtml += '</table>';
            Compunix_$('DealerSelection').innerHTML = innerhtml;
        }
        /*
    Compunix_$('Dealer'+index+'radio').value = pca_id[index];
    Compunix_$('Dealer'+index+'Name').innerHTML = pca_name[index] 
    Compunix_$('Dealer'+index+'Description').innerHTML = pca_description[index];
    Compunix_$('Dealer'+index+'Distance').innerHTML = pca_distance[index];*/
    }
 }
 function ShowMap(name, latitude, longitude)
 {
        name = name.replace('<br/>','+').replace('<br>','+').replace('<br >','+').replace(' ','+');  
        googleSrc = '';  
//        var action = 'fetch';
//        var parameters = [['id',id],['language','english']];
//        var longitude = pca_longitude[index];
//        var latitude = pca_latitude[index];
        //postCodeAnywhereInlineJSTag(accountCode, licenseCode, action, parameters, 'embedPostCodeAnywhereGoogleMapAdapter');
        googleSrc = "http://maps.google.com/maps?q="+ escape(name) +"@"+latitude+","+longitude+"&iwloc=A&hl=en&z=17&output=embed&t=m";
//        googleSrc = "http://maps.google.com/maps?q="+ escape(name).replace('%3Cbr%3E','+') +"&iwloc=A&hl=en&z=17&output=embed&t=h";
//        googleSrc = "http://www.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q="+name+"&amp;z=14&amp;iwloc=A&amp;output=embed";
//alert(googleSrc);
        Compunix_$("DealerMap").src = googleSrc;
 }
 function embedPostCodeAnywhereGoogleMapAdapter()
 {
        
 }
