var combosCount = 0;
var combos = new Array();


function ccardData(num, name) {
	this.data = num;
	this.name = name;
}

var cCards = new Array(
	new ccardData('MASTERCARD', 0),
	new ccardData('VISA', 1),
	new ccardData('DISCOVER', 2),
	new ccardData('AMERICAN EXPRESS', 3)
	);

function shippingData( country, baseRate, perEachRate )
{
    this.data = country;
    this.baseRate = baseRate;
    this.perEachRate = perEachRate;
}

var shippingCosts = new Array(
    new shippingData( 'Australia', 31.95, 2.00 ),
    new shippingData( 'Austria', 40.95, 2.00 ),
    new shippingData( 'Bahamas', 24.95, 1.50 ), 
    new shippingData( 'Bahrain', 46.95, 3.00 ),
    new shippingData( 'Belgium', 28.95, 1.50 ), 
    new shippingData( 'Bermuda', 24.95, 1.50 ),
    new shippingData( 'Brazil', 36.95, 3.50 ),
    new shippingData( 'Bulgaria', 55.95, 3.25 ),
    new shippingData( 'Canada', 24.95, 1.00 ),
    new shippingData( 'Cayman Islands', 24.95, 1.50 ),
    new shippingData( 'Czech Republic', 55.95, 3.25 ),
    new shippingData( 'Denmark', 40.95, 2.00 ),
    new shippingData( 'Estonia', 55.95, 3.25 ),
    new shippingData( 'Finland', 40.95, 2.00 ),
    new shippingData( 'France', 28.95, 1.50 ),
    new shippingData( 'Germany', 28.95, 1.50 ),
    new shippingData( 'Greece', 40.95, 2.00 ),
    new shippingData( 'Hong Kong', 31.95, 2.00 ),
    new shippingData( 'Iceland', 40.95, 2.00 ),
    new shippingData( 'Ireland, Northern', 28.95, 1.50 ),
    new shippingData( 'Ireland, Republic', 28.95, 1.50 ),
    new shippingData( 'Israel', 46.95, 3.00 ),
    new shippingData( 'Italy', 28.95, 1.50 ),
    new shippingData( 'Japan', 29.95, 1.50 ),
    new shippingData( 'Korea, South', 31.95, 2.00 ),
    new shippingData( 'Kuwait', 46.95, 3.00 ),
    new shippingData( 'Latvia', 55.95, 3.25 ),
    new shippingData( 'Lithuania', 55.95, 3.25 ),
    new shippingData( 'Luxembourg', 28.95, 1.50 ),
    new shippingData( 'Mexico', 37.95, 2.50 ),
    new shippingData( 'Netherlands', 28.95, 1.50 ),
    new shippingData( 'New Zealand', 31.95, 2.00 ),
    new shippingData( 'Norway', 40.95, 2.00 ),
    new shippingData( 'Poland', 55.95, 3.25 ),
    new shippingData( 'Portugal', 40.95, 2.00 ),
    new shippingData( 'Saudi Arabia', 52.95, 3.00 ),
    new shippingData( 'Slovak Rep.', 55.95, 3.25 ),
    new shippingData( 'Slovenia', 55.95, 3.25 ),
    new shippingData( 'Spain', 40.95, 2.00 ),
    new shippingData( 'Sweden', 40.95, 2.00 ),
    new shippingData( 'Switzerland', 40.95, 2.00 ),
    new shippingData( 'Taiwan', 31.95, 2.00 ),
    new shippingData( 'United Arab Emirates', 46.95, 3.00 ),
    new shippingData( 'United Kingdom', 28.95, 1.50 ),
    new shippingData( 'United States', 0, 0 ),
    new shippingData( 'Virgin Islands, British', 24.95, 1.50 ),
    new shippingData( 'Virgin Islands, U.S.', 24.95, 1.50 )
    );

var usaShippingCosts = new Array(
    new shippingData( 'Ground 5-7 days', 6.95, 1.00 ),
    new shippingData( 'Express 2-3 days', 11.95, 1.00 ),
    new shippingData( 'Next Day', 22.95, 1.00 )
    );

function comboBox_getData()
{
    var inp = findObject( this.divID+'_ci'+ this.selectedItem );
    if( inp )
        return inp.innerHTML;
    else
        return '';
}

function comboBox_setData( newData )
{
    for( i=0; i<this.values.length; i++ )
        if( newData == this.values[i].data )
            {
            var iel = findObject( this.divID+'_ie' );
            if( iel )
                {
                iel.innerHTML = newData;
                this.selectedItem = i;
                }
            return;
            }
}

function comboBox_getID( extractFrom )
{
    var arrayOfStrings = extractFrom.split('_',1);
    return arrayOfStrings[0]+'_div';
}

function comboBox_getComboObj( extractFrom )
{
    var objName = comboBox_getID( extractFrom );
    var target = null;
    for( i=0; i<combosCount && !target; i++ )
        if( combos[i].divID == objName )
            target = combos[i];
    return target;
}

function comboBox_clicked()
{
	var target = comboBox_getComboObj( this.id );
    if( !target )
        return false;
    if( !target.enabled )
        return false;
    if( target.droppedDown )
        hideElem( target.divID+'_ddc' );
    else
        showElem( target.divID+'_ddc' );
    target.droppedDown = !target.droppedDown;
    return false;
}

function comboBox_itemSelected( extrId )
{
    var target = comboBox_getComboObj( extrId );
    if( !target )
        return false;
    var selItem = extrId.split('_');
    var itemNum = selItem[ selItem.length-1 ].split( 'i' );

    var inp = findObject( target.divID+'_ie' );
    var src = findObject( extrId );
	if( inp && src )
        {
        inp.innerHTML = src.innerHTML;
        target.selectedItem = itemNum[1];
        hideElem( target.divID+'_ddc' );
        target.droppedDown = false;

        if( target.onselectitem ){
		target.onselectitem();
	}
        }
}

function comboBox_scrollIt( objId, step )
{
    var target = comboBox_getComboObj( objId );
    if( !target )
        return false;
    if( target.timerID )
        clearTimeout( target.timerID );
    var ddel = findObject( target.divID + '_ddel' );
    var ddc = findObject( target.divID + '_ddc' );
    if( !ddel || !ddc )
        return;
    var y = ddel.offsetTop+step;
    if( step > 0 && y > 0 )
        y = 0;
    if( step < 0 && ddel.offsetHeight+y < ddc.offsetHeight )
        y = ddc.offsetHeight-ddel.offsetHeight;
    ddel.style.top = y;
    str = 'comboBox_scrollIt("'+objId+'",'+step+')';
    target.timerID = setTimeout( str, 10 );
}

function comboBox_clearTimeout()
{
    var target = comboBox_getComboObj( this.id );
    if( !target )
        return false;
    if( target.timerID )
        clearTimeout( target.timerID );
}

function comboBox_scrollUp()
{
    var target = comboBox_getComboObj( this.id );
    if( !target )
        return false;
    if( target.timerID )
        clearTimeout( target.timerID );
    target.timerID = setTimeout( 'comboBox_scrollIt("'+target.divID+'",2)', 10 );
}

function comboBox_scrollDown()
{
    var target = comboBox_getComboObj( this.id );
    if( !target )
        return false;
    if( target.timerID )
        clearTimeout( target.timerID );
    target.timerID = setTimeout( 'comboBox_scrollIt("'+target.divID+'",-2)', 10 );
}

function comboBox_init()
{
    var srcDiv = findObject( this.divID );
    if( !srcDiv )
        return;

    var coords = getScreenCoords( srcDiv );

    var inputEl = document.createElement('DIV');
    inputEl.id = this.divID + '_ie';
    inputEl.innerHTML = this.promptTitle;
    inputEl.className = 'txtinput';
    inputEl.style.width = srcDiv.offsetWidth - 22;
    inputEl.style.height = '15px';
    inputEl.style.position = 'absolute';
    inputEl.style.top = this.coords.y-1;
    inputEl.style.left = this.coords.x+1;
    inputEl.style.cursor = 'pointer';
    inputEl.onclick = comboBox_clicked;
    srcDiv.appendChild( inputEl );

    var dropDownPointer = document.createElement('DIV');
    dropDownPointer.id = this.divID + '_ddp';
    dropDownPointer.className = 'txtinput';
    dropDownPointer.style.width = '19px';
    dropDownPointer.style.height = '15px';
    dropDownPointer.style.left = srcDiv.offsetWidth - 20 + this.coords.x+1;
    dropDownPointer.style.top = this.coords.y-1;
    dropDownPointer.style.position = 'absolute';
    dropDownPointer.style.backgroundImage = 'url(img/dd.gif)';
    dropDownPointer.style.cursor = 'pointer';
    dropDownPointer.onclick = comboBox_clicked;
    srcDiv.appendChild( dropDownPointer );

    var dropDownContainer = document.createElement('DIV');
    dropDownContainer.id = this.divID + '_ddc';
    dropDownContainer.className = 'txtinput';
    dropDownContainer.style.top = this.coords.y+18;
    dropDownContainer.style.left = this.coords.x;
    dropDownContainer.style.width = srcDiv.offsetWidth;
    dropDownContainer.style.height = '55px';
    dropDownContainer.style.overflow = 'hidden';
    dropDownContainer.style.position = 'absolute';
    dropDownContainer.style.backgroundColor = 'white';
    dropDownContainer.style.visibility = 'hidden';
    dropDownContainer.droppedDown = false;
    srcDiv.appendChild( dropDownContainer );

    var dropDownEl = document.createElement('DIV');
    var s = '';
    dropDownEl.id = this.divID + '_ddel';
    dropDownEl.style.top = 2;
    dropDownEl.style.left = 2;
    dropDownEl.style.width = srcDiv.offsetWidth-21;
    dropDownEl.style.position = 'absolute';
    
	var content = shippingCosts;
	if (this.divID == 'cctype_div')
		content = cCards;
	for( i=0; i<content.length; i++ )
        {
        s += '<div id=' + this.divID+'_ci'+i+' ';
        if( i!=this.currentCountry )
	{
		s += 'onmouseover="this.style.backgroundColor=\'#e9e9e9\';" ';
        	s += 'onmouseout="this.style.backgroundColor=\'white\';" ';
        	s += 'onclick="return comboBox_itemSelected(this.id);" ';
	}
	s += 'style="width: auto; cursor: pointer; cursor: hand;">'+this.values[i].data+'</div>';
        }
    dropDownEl.innerHTML = s;
    dropDownContainer.appendChild( dropDownEl );

    var arrowUp = document.createElement('DIV');
    arrowUp.id = this.divID + '_au';
    arrowUp.style.position = 'absolute';
    arrowUp.style.top = 0;
    arrowUp.style.left = dropDownContainer.offsetWidth - 18;
    arrowUp.innerHTML = '<img src=img/scup.gif width=15 height=15 border=0>';
    arrowUp.style.cursor = 'pointer';
    arrowUp.onmouseover = comboBox_scrollUp;
    arrowUp.onmouseout = comboBox_clearTimeout;
    dropDownContainer.appendChild( arrowUp );

    var arrowDown = document.createElement('DIV');
    arrowDown.id = this.divID + '_ad';
    arrowDown.style.position = 'absolute';
    arrowDown.style.top = dropDownContainer.offsetHeight - 18;
    arrowDown.style.left = dropDownContainer.offsetWidth - 18;
    arrowDown.innerHTML = '<img src=img/scdn.gif width=15 height=15 border=0>';
    arrowDown.style.cursor = 'pointer';
    arrowDown.onmouseover = comboBox_scrollDown;
    arrowDown.onmouseout = comboBox_clearTimeout;
    dropDownContainer.appendChild( arrowDown );
}

function createComboBox( divId, promptTitle, values, coords )
{
    this.divID = divId;
    this.values = values;
    this.promptTitle = promptTitle;
    this.selectedItem = -1;
    this.droppedDown = false;
    this.timerID = null;
    this.onselectitem = null;
    this.enabled = true;
    this.coords = coords;

    this.getData = comboBox_getData;
    this.setData = comboBox_setData;
	this.init = comboBox_init;
    this.getID = comboBox_getID;

    this.init();

    combos[ combosCount++ ] = this;
}

function updateShippingOptions()
{
    if( this.getData() == 'United States' )
        showElem( 'shipping_options' );
    else
        hideElem( 'shipping_options' );
}

function getComboByID( id )
{
    var foundObj = null;
    for( i=0; i<combos.length && !foundObj; i++ )
        if( combos[i].divID == id )
            foundObj = combos[i];
    return foundObj;
}

function getComboByIDVal( id )
{
    var foundObj = null;
    for( i=0; i<combos.length && !foundObj; i++ )
        if( combos[i].divID == id )
            foundObj = combos[i];
    if( foundObj )
        return foundObj.getData();
    else
        return '';
}
