﻿// JScript File
function bannerclick( name ) {
    var xmlhttp;
    if (window.XMLHttpRequest) 
        xmlhttp=new XMLHttpRequest();
    else 
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("POST",  "Services/WineServices.asmx/PostBannerClick", true); 
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xmlhttp.send("bannername=" + name ); 
   return true; 
}

var currentImage = 'Images/map.gif';
function imagemap( place, ctrl ) { 
	var image = document.getElementById( ctrl );
	if( image == null ) return;
	if( place != 0 ) currentImage =  image.src;

	switch(place)
	{ 
		case(0): image.src= currentImage; break;
		case(1): image.src='Images/mapauckland.gif'; break;
		case(2): image.src='Images/mapgisborne.gif'; break;
		case(3): image.src='Images/maphawkesbay.gif'; break;
		case(4): image.src='Images/mapwairarapa.gif'; break;
		case(5): image.src='Images/mapmarlborough.gif'; break;
		case(6): image.src='Images/mapnelson.gif'; break;
		case(7): image.src='Images/mapcanterbury.gif'; break;
		case(8): image.src='Images/mapotago.gif'; break;
		default: image.src='Images/map.gif'; break;
	}
} 

function stopDefault( e ) {
	if(  e && e.preventDefault )
		e.preventDefault( );
	else
		window.event.returnValue = false;    
	return false;  
}

var currentMenuBackgroundColor = "transparent";
function hoverMenu(  ctrl ) {
    currentMenuBackgroundColor = ctrl.style.backgroundColor;
    ctrl.style.backgroundColor = '#808040'
}  

function unhoverMenu(  ctrl ) {
    ctrl.style.backgroundColor = currentMenuBackgroundColor;
}  

// Used on tables with potential embedded tables.
function selectTableRowAndReturnCellText1( e, table, columnNum, nullColour, selectColour ) {
    // Ensure we drill down to the bottom most table.
    var i=0;
    var answer = "";
    for ( i=0; i < table.childNodes.length; i++ )
    {
        if( table.childNodes[i].tagName == 'TABLE' )
        {
            answer = selectTableRowAndReturnCellText1( e, table.childNodes[i], columnNum, nullColour, selectColour )
            if( answer != '' )
                return answer ;
        }  
    } 
    return selectTableRowAndReturnCellText2( e, table, columnNum, nullColour, selectColour );
}

function selectTableRowAndReturnCellText2( e, table, columnNum, nullColour, selectColour ) {
    var answer = "";

    if( !table ) return answer;    
    if( columnNum < 0 ) return answer;
    
    // First we need to find the cell that was clicked.
    var targetevent;
    if ( !e ) var e = window.event;
    if (e.target) targetevent = e.target;
    else if(e.srcElement) targetevent = e.srcElement;
    
   // Defeat Safari bug 
    if( targetevent.nodeType == 3) 
        targetevent = targetevent.parentNode;
   
    // Find the actual cell for comparison  
    var targetcell = targetevent;
    while( ( targetcell.tagName != 'TD') && (targetcell.parentNode != null ) )
       targetcell = targetcell.parentNode;
       
    // Scan through the rows in the table and set the colour.
    var oRows = table.rows;
    if ( oRows )
    {
        // Find the clicked row
        for ( var i=0; i < oRows.length; i++ )
        {
            oRows[i].style.color = nullColour;  
         
            if( oRows[i].cells.length > columnNum ) 
            {
                if( targetcell == oRows[i].cells[columnNum] )
                {
                     answer = targetevent.innerHTML;
                     oRows[i].style.color = selectColour;  
                } 
             }
        }
    }
    return answer;
}

// Used on table. (Does not yet support embedded tables)
function setFocus( ctrl, focusColour ) {
    // To prevent processing the user moving the cursor prior to us getting the callback. 
    if( typeof(WineContent) !== 'undefined' && !WineContent.finished( ) ) return;
    ctrl.style.color =  focusColour;
}

// Used on table. (Does not yet support embedded tables)
function removeFocus( row, ctrl, active, inactive ) {
    // To prevent processing the user moving the cursor prior to us getting the callback. 
    if( typeof(WineContent) !== 'undefined' && !WineContent.finished( ) ) return;

	var s = document.getElementById( ctrl ).value;
    if( (row.cells.length >0) && (row.cells[0].innerHTML == s) )
		row.style.color = active;
	else
		row.style.color = inactive;
}

// Called by custom control...
function findItemClickedCustom( e, ctrlID, columnNum ) {
    // First find the holding container.
    var table=document.getElementById( ctrlID );
    if( table == null )
        return '';
    return selectTableRowAndReturnCellText1( e, table, columnNum, "White", "#004040" );
}


