/* vim:set et ts=4 sw=4 sts=4: */
/* Copyright (C) 2003-2007 Axentra Corporation.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE OF Axentra Corporation.
 * The copyright notice above does not evidence any actual or intended
 * publication of such source code. The contents of this file may not
 * be disclosed to third parties, copied or duplicated in any form, in
 * whole or in part, without the prior written permission of
 * Axentra Corporation.
 *
 */

/*** onkeypress handler for IP address entry ***
- assumes one single form, and that IP address field names are of the form
   fieldname1, fieldname2, fieldname3, fieldname4
*/
function ipAddr(e)
{
    if(!e) e = event;
    var el = e.target || e.srcElement;
    var key = (e.which != undefined ? e.which : e.keyCode);
    var val = el.value;

    if( key != 46 && key != 8 && key != 0 && (key < 48 || key > 57) )
    {
        return false;
    }

    if( key >= 48 && key <= 57 )
    {
        var n = Number("0123456789".charAt(key - 48));
        
        if( Number(val+n) >= 0 && Number(val+n) <= 255 &&
            val.length < 3 )
            return true;
    }

    if( key == 46 || key == 8 )
    {
        if( key == 8 && val.length > 0 )
            return true;

        var elNum = Number(el.name.match(/[0-9]$/));
        var elName = el.name.replace(/[0-9]$/, "");

        var newElNum  = elNum + (key==8 ? -1 : 1);
        var newEl;

        if( newElNum >= 1 && newElNum <= 4 )
            newEl = document.forms[0].elements[elName+newElNum];

        if(newEl)
            newEl.focus();

        return false;
    }

    return key == 0;
}

/*** onclick handler for radio buttons ***
- assumes one single form, and that 'subordinate' inputs have the attribute 'for' set on them.
  Eg: <radio name="connection" value="foo">  <input for="connection.foo"> 
  When that radio is selected, anything for connection.foo is enabled,
  and anything for connection.* (for * != foo) is disabled.

  inputs for nested radios should look something like for="connection.foo ip.bar" (ie, space-separated).

  This function was duplicated to homebase/html/js/js_functions.js. Any change here is supposed to 
  go there as well.
*/
function radioClick(aEl)
{
    if( aEl.getAttribute("disabled") ) return;

    var name = aEl.name;

    var value;
    if (aEl.getAttribute("type") == "checkbox") {
        if (aEl.checked) 
            value = aEl.value;
        else
            value = "";
    } else 
        value = aEl.value;

    for(var i=0; i < document.forms[0].elements.length; i++)
    {
        var el = document.forms[0].elements[i];
        if(!el) continue;

        var elFor = el.getAttribute("for");
        if(!elFor) continue;

        var elForArr = elFor.split(" ");

        for(var j=0; j<elForArr.length; j++)
        {
            if(elForArr[j] == name+"."+value)
            {
                if(elForArr.length == 1)
                {
                    el.removeAttribute("disabled");
                }
                else
                {
                    var conds = 0;
                    for(var k=0; k<elForArr.length; k++)
                    {
                        if(k==j) continue;
                        var arr = elForArr[k].split(".");
                        var radios = document.getElementsByName(arr[0]);
                        for(var l=0; l<radios.length; l++)
                        {
                            if(radios[l].value == arr[1] && radios[l].checked)
                            {
                                conds++;
                            }
                        }
                    }
                    if(conds == elForArr.length-1)
                    {
                        el.removeAttribute("disabled");
                    }
                }
            }
            else if(elForArr[j].match( new RegExp("^"+name+"\.") ))
            {
                el.setAttribute("disabled", "true");
            }
        }
    }
}

/*** onload handler for entire document ***
- looks for checked radio buttons, and runs radioClick for them
- ... that's it for now
*/
window.onload = function()
{
    if(document.forms && document.forms[0] && document.forms[0].elements) {
        for(var i=0; i < document.forms[0].elements.length; i++)
        {
            var el = document.forms[0].elements[i];
            if(!el) continue;

            if(el.type.match(/^radio$/i) && el.checked)
                radioClick(el);
        }
    }
};

/**** dynamicServiceDNS
 *
 * PURPOSE: remove DNS providers that do not allow user's to register
 *          subdomains of already existing domain names (e.g. zoneedit
 *          does not provide this service).
 */

function dynamicServiceDNS()
{
    var domainsToRemove = new Array("zoneedit");
    var optionsList = document.wanForm.provider.options;
    for (var i = 0; i < domainsToRemove.length; i++)
    {
        for (var j = 0; j < optionsList.length; j++)
        {
            if (domainsToRemove[i] == optionsList[j].value)
            {
                optionsList[optionsList.length] = null;
                optionsList.length--;
                break;
            }
        }
    }
}

/**** dynamicOwnedDNS
 *
 * PURPOSE: add DNS providers that only allow user's to register
 *          domains but not subdomains of already existing domains
 *          (e.g. zoneedit is one of those services)
 */

function dynamicOwnedDNS()
{
    var domainsToAdd = new Array();
    var optionsList = document.wanForm.provider.options;
    domainsToAdd[0] = new Array("ZoneEdit.com", "zoneedit");
    for (var i = 0; i < domainsToAdd.length; i++)
    {
        optionsList[optionsList.length] = new Option(domainsToAdd[i][0], domainsToAdd[i][1]);
    }
}

function popupwindow(url, height, width )
{
    if( height == 0 )
      height = "400";

   if( width == 0 )
      width = "600";

   var mywindow = window.open(url, 'Tracker', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+'');
    if( mywindow )
    {
        mywindow.location = url;
        mywindow.focus();
    }
}

/*** Validating the serial number ***/

var serial_alphabet = new String("ABCDEFGHJKMNPQRTUVWXYZ");

function alphabet_index( c )
{
   return serial_alphabet.indexOf( c.toUpperCase() );
}

function validateSerial( )
{
    var key = document.forms[0].serialnum.value;
    // Strip spaces and hyphens
    var regexp = /[\- ]/g
    key = key.replace( regexp, "" );
    key = key.toUpperCase();
    var position_V1;
    var position_V2;
    var V1;
    var V2;
    var index_set_one = new Array(6);
    var index_set_two = new Array(6);
    var number_one, number_two;
    var i;
    var j;

    position_V1 = ( alphabet_index( key.charAt(2) ) % 13 ) + 3;
    V1 = alphabet_index( key.charAt(position_V1) );
    position_V2 = (position_V1 > ((V1 % 12) + 3))
                  ? ((V1 % 12) + 3)
                  : ((V1 % 12) + 4);

    V2 = alphabet_index( key.charAt(position_V2) );

    for( number_one=0, j=2, i=0; i<6; i++ )
    {
        while( j == position_V1 || j == position_V2 )
            j++;
        index_set_one[i] = alphabet_index( key.charAt(j++) );
        if( key.charAt(0) == 'R' || key.charAt(0) == 'H' || key.charAt(0) == 'A' || key.charAt(0) == 'B' || key.charAt(0) == 'Z' )
            number_one *= serial_alphabet.length;
        number_one += index_set_one[i];
    }
//        if( key.charAt(0) != 'R' || key.charAt(1) != 'P' )
    {
        if( key.charAt(0) == 'R' || key.charAt(0) == 'H' || key.charAt(0) == 'A' || key.charAt(0) == 'B' || key.charAt(0) == 'Z' )
            number_one *= serial_alphabet.length;
        number_one += alphabet_index( key.charAt(0) );
    }

    for( number_two=0, i=0; i<6; i++ )
    {
        while( j == position_V1 || j == position_V2 )
            j++;
        index_set_two[i] = alphabet_index( key.charAt(j++) );
        if( key.charAt(0) == 'R' || key.charAt(0) == 'H' || key.charAt(0) == 'A' || key.charAt(0) == 'B' || key.charAt(0) == 'Z' )
            number_two *= serial_alphabet.length;
        number_two += index_set_two[i];
    }
//        if( key.charAt(0) != 'R' || key.charAt(1) != 'P' )
    {
        if( key.charAt(0) == 'R' || key.charAt(0) == 'H' || key.charAt(0) == 'A' || key.charAt(0) == 'B' || key.charAt(0) == 'Z' )
            number_two *= serial_alphabet.length;
        number_two += alphabet_index( key.charAt(1) );
    }
    if( (((number_one + 11) % (serial_alphabet.length)) != V1) ||
        (((number_two + 7) % (serial_alphabet.length)) != V2) )
    {
        return false;
    }

    return key.toUpperCase().replace( /(....)(....)(....)(....)/, "$1-$2-$3-$4" );
    return true;
}

/*** Setting an input, regardless of its type ***/
function setInput( el, newValue )
{
    if( !el ) return;
    el.value = newValue;
    if( el.value == newValue ) return;

    // IE5Mac and Safari prefer it this way
    if( el.tagName == 'SELECT' )
    {
        var opts = el.options;
        for( var i=0; i < opts.length; i++ )
        {
            if( opts[i].value == newValue )
            {
                opts[i].selected = true;
                el.selectedIndex = i;
                return;
            }
        }
    }
    //alert( "Couldn't set "+el.id+" to "+newValue );
}
function displayPleaseWait()                                                    
{                                                                               
    var msgBox = document.getElementById('pleaseWaitMessage');                  
    if( !msgBox ) return;                                                       
    msgBox.style.display = 'block';                                             
}

if ( navigator.userAgent.toLowerCase().indexOf("safari") != -1) top.resizeTo( 805, 700 );
                                                                               

