// JScript File - ppc_module.js - v1.0 (2007-05-13)

var mPPCModuleDomain = 'http://privpakportal.schenker.nu/';

var mPPCModuleFormHTML = '';
mPPCModuleFormHTML += '<table><tr><td colspan="2" style="font-family:Verdana, Geneva, Arial, Helvetica; font-size: 10px; height: 30px">';
mPPCModuleFormHTML += '<input name="ppc_module_input" type="text" onkeydown="disableEnter(event)" onkeyup="initSearch(event)" tabindex="1" title="Fyll i postnummer eller ort" style="width:100px;">';
mPPCModuleFormHTML += '<input type="button" onclick="ppcModuleSearch()" name="ppc_module_search" value="S&ouml;k" tabindex="2" title="Klicka h&auml;r f&ouml;r att s&ouml;ka utl&auml;mningsst&auml;lle" style="font-family:Verdana;">';
mPPCModuleFormHTML += ' eller g&ouml;r valet via <a href="javascript:ppcModuleOpenMap()" tabindex="3">karta</a>.';
mPPCModuleFormHTML += '</td></tr>';
mPPCModuleFormHTML += '</table>';

var mPPCModuleMapHTML = ''
mPPCModuleMapHTML += '<html><head><title>V&auml;lkommen till Karthotellet</title>';
mPPCModuleMapHTML += '<script>function valjOmbud(info)';
mPPCModuleMapHTML += '{';
mPPCModuleMapHTML += 'if (window.opener && !window.opener.closed)';
mPPCModuleMapHTML += '{';
mPPCModuleMapHTML += 'opener.ppcModuleCallback(info);';
mPPCModuleMapHTML += 'opener.ppcChoice(info);';
mPPCModuleMapHTML += 'self.close();';
mPPCModuleMapHTML += '}';
mPPCModuleMapHTML += '}</script>';
mPPCModuleMapHTML += '</head>';
mPPCModuleMapHTML += '<body>';
mPPCModuleMapHTML += '<applet code="schenker.class" codebase="http://www.karthotellet.com/schenker/" name="mapApplet" width="571" height="322" mayscript>';
mPPCModuleMapHTML += '<param name="id" value="0" />';
mPPCModuleMapHTML += '<param name="postnr" value="0"/>';
mPPCModuleMapHTML += '</applet>';
mPPCModuleMapHTML += '</body>';
mPPCModuleMapHTML += '</html>';


/**
 *  Initialize the input form
 */
function ppcModuleInit()
{
    ppcModuleElement = document.getElementById('ppc_module');

    formElement = document.createElement('form');
    formElement.id = 'ppc_module_form';
    formElement.innerHTML = mPPCModuleFormHTML;

    resultElement = document.createElement('span');
    resultElement.id = 'ppc_module_result';
    formElement.appendChild(resultElement);
    ppcModuleElement.appendChild(formElement);
    
    resultElement = document.createElement('span');
    resultElement.id = 'ppc_module_choice';
    formElement.appendChild(resultElement);
    ppcModuleElement.appendChild(formElement);
}


/**
 * Handles the search action and loads the search result
 */
function ppcModuleSearch()
{
    ppcModuleCallback('');

    var ppcModuleElement = document.getElementById('ppc_module_choice');
    ppcModuleElement.innerHTML = '';

    var ppcModuleElement = document.getElementById('ppc_module_result');
    ppcModuleElement.innerHTML = '';
    
    var headElement = document.getElementsByTagName("head")[0];
    scriptElement = document.createElement('script');
    scriptElement.id = 'uploadScript';
    scriptElement.type = 'text/javascript';
    scriptElement.src = mPPCModuleDomain + "/PPC_modul/ppc_module_search.aspx?data.input=" + document.forms['ppc_module_form'].elements['ppc_module_input'].value;
    headElement.appendChild(scriptElement);
}


/**
 * Adds a search result to the list.
 */
function ppcModulePopulateResult(aId, aResult)
{
    if (aId == 'null' && aResult == 'null')
    {
        var ppcModuleElement = document.getElementById('ppc_module_choice');
        ppcModuleElement.innerHTML = '<font face=Verdana, Geneva, Arial, Helvetica size=1>Ingen s&ouml;ktr&auml;ff.</font>';
    }
    else
    {
        var ppcModuleElement = document.getElementById('ppc_module_choice');
        ppcModuleElement.innerHTML = '';

        var ppcModuleElement = document.getElementById('ppc_module_result');
        spanElement = document.createElement('span');

        spanElement.innerHTML = '<input type="radio" onclick="ppcModuleAccept()" value="' + aId + '" name="ppc_module_result">' + aResult + '</input><br>';
        ppcModuleElement.appendChild(spanElement);
    }
}


/**
 * Adds the user choice to a list.
 */
function ppcModulePopulateChoice(aId, aResult)
{
    var ppcModuleElement = document.getElementById('ppc_module_choice');
    spanElement = document.createElement('span');

    spanElement.innerHTML = aResult;
    ppcModuleElement.appendChild(spanElement);
}


/**
 * Handles the Accept action and calls the ppcModuleCallback method 
 * with the chosen value.
 */
function ppcModuleAccept()
{
    var radioObject = document.forms['ppc_module_form'].elements['ppc_module_result'];
    if (radioObject.value != undefined)
    {
        ppcChoice(radioObject.value);
        ppcModuleCallback(radioObject.value);
    }
    else
    {
        var checkedIndex = -1;
	    for(i = 0; i < radioObject.length; i++)
	    {
		    if(radioObject[i].checked)
		    {
		        checkedIndex = i;
                break;
		    }
	    }
	    
	    if (checkedIndex > -1)
	    {
	        ppcChoice(radioObject[checkedIndex].value);
            ppcModuleCallback(radioObject[checkedIndex].value);
        }
        else
        {
            alert('Inget ombud &auml;r valt.');
        }
    }
    
    var ppcModuleElement = document.getElementById('ppc_module_result');
    ppcModuleElement.innerHTML = ''; 
                
    document.forms['ppc_module_form'].elements['ppc_module_input'].value = '';
}


/**
 * Populates a list with data from the chosen ppc.
 */
function ppcChoice(ppc)
{
    var ppcModuleElement = document.getElementById('ppc_module_choice');
    var headElement = document.getElementsByTagName("head")[0];
    scriptElement = document.createElement('script');
    scriptElement.id = 'uploadScript';
    scriptElement.type = 'text/javascript';
    scriptElement.src = mPPCModuleDomain + "/PPC_modul/ppc_module_choice.aspx?data.input=" + ppc;
    headElement.appendChild(scriptElement);
}


/**
 * Opens a new window with the map search.
 */
function ppcModuleOpenMap()
{
    var ppcModuleElement = document.getElementById('ppc_module_choice');
    ppcModuleElement.innerHTML = '';

    var ppcModuleElement = document.getElementById('ppc_module_result');
    ppcModuleElement.innerHTML = '';
    
    document.forms['ppc_module_form'].elements['ppc_module_input'].value = '';

    ppcModuleElement = document.getElementById('ppc_module');
    
    var mapWindow = window.open('','','left=250,top=250,height=350,width=590,status=yes,resizable=no,scrollbars=no,toolbar=no,location=no,menubar=no');
    newDocument = mapWindow.document;
    newDocument.write(mPPCModuleMapHTML);
    newDocument.close();
}


/**
 * Disables the default action on enter key press (to avoid form submits) and calls the search function.
 */
function disableEnter(oEvent)
{
    if ((oEvent.keyCode && oEvent.keyCode == 13) || (oEvent.which && oEvent.which == 13))
    {
        // Mozilla
        if (oEvent.preventDefault)
        {
            oEvent.preventDefault();
        }
        // IE
        else
        {
            oEvent.returnValue = false;
        }
    }
}


/**
 * Calls the search function when the user releases the enter button.
 */
function initSearch(oEvent)
{
    if ((oEvent.keyCode && oEvent.keyCode == 13) || (oEvent.which && oEvent.which == 13))
    {
        // Mozilla
        if (oEvent.preventDefault)
        {
            ppcModuleSearch();

        }
        // IE
        else
        {
            ppcModuleSearch();
        }
    }
}