var regionArray = new Array(20);

function getHeaderRegion(){
    var kblocs = kbLocs;
    var lngRandRegion = Math.floor(Math.random()*20);
    var lngRandCountry = Math.floor(Math.random()*50);
    var lngRandCity = Math.floor(Math.random()*100);
    //Delete_Cookie('strRegion','/','');
    //Delete_Cookie('lngRegionID','/','');
    //Delete_Cookie('strCountry','/','');
    //Delete_Cookie('lngCountryID','/','');
    //Delete_Cookie('strCity','/','');
    //Delete_Cookie('lngCityID','/','');
    var strRegion = Get_Cookie('strRegion');
    var lngRegionID = eval(Get_Cookie('lngRegionID'));
    var strCountry = Get_Cookie('strCountry');
    var lngCountryID = eval(Get_Cookie('lngCountryID'));
    var strCity = Get_Cookie('strCity');
    var lngCityID = eval(Get_Cookie('lngCityID'));
    

    if (strRegion==null || strRegion=='') {
        regionArray = kblocs[1][1];
        if (regionArray.length < lngRandRegion) {
            lngRandRegion = regionArray.length;
        }

        var regionarray = regionArray[lngRandRegion-1];
        strRegion = regionarray[1];
        lngRegionID = regionarray[0];
        Set_Cookie('strRegion',strRegion, 1,'index.html','','');
        Set_Cookie('lngRegionID',lngRegionID, 1,'index.html','','');

        var countryArray;
        var level = lngRegionID;
        for (var i=0; i<kblocs.length; i++){
	        if (kblocs[i][0]==level){
		        countryArray=kblocs[i][1];
		        if (countryArray.length < lngRandCountry) {
                    lngRandCountry = countryArray.length;
                }
                strCountry = countryArray[lngRandCountry-1][1];
                lngCountryID = countryArray[lngRandCountry-1][0];
                Set_Cookie('strCountry',strCountry, 1,'index.html','','');
                Set_Cookie('lngCountryID',lngCountryID, 1,'index.html','','');
	        }
        }

        var cityArray;
        var level = lngCountryID;
        for (var i=0; i<kblocs.length; i++){
	        if (kblocs[i][0]==level){
		        cityArray=kblocs[i][1];
		        if (cityArray.length < lngRandCity) {
                    lngRandCity = cityArray.length;
                }
                strCity = cityArray[lngRandCity-1][1];
                lngCityID = cityArray[lngRandCity-1][0];
                Set_Cookie('strCity',strCity, 1,'index.html','','');
                Set_Cookie('lngCityID',lngCityID, 1,'index.html','','');
	        }
        }
    }
    
    var oFeaturedCitySpan =MM_findObj('lblFeaturedCountryCity');
    if (oFeaturedCitySpan!=null) {
        if (strCity==null || strCity=='') {
            oFeaturedCitySpan.innerHTML = '<a href="apps/destination/default.aspx?rid=' + lngRegionID + '&nid=' + lngCountryID + '">' + strCountry + ' in the Region of ' + strRegion + '. Click Here To Learn More</a>';
        } else {
            oFeaturedCitySpan.innerHTML = '<a href="apps/destination/default.aspx?rid=' + lngRegionID + '&nid=' + lngCountryID + '&cid=' + lngCityID + '">' + strCity + ' in the Country of ' + strCountry + '. Click Here To Learn More</a>';        
        }
    }
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function doLinkTracking(strTrackingNumber, strShipperNameCode) {
    if (strShipperNameCode == "FDX") {
		window.open('http://fedex.com/cgi-bin/tracking?action=track&tracknumbers=' + strTrackingNumber); 
	}
	else if (strShipperNameCode == "USPS" || strShipperNameCode == "USP") {
	    window.open('http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum=' + strTrackingNumber); 
	} 
	else if (strShipperNameCode == "UPS") {
		window.open('http://wwwapps.ups.com/WebTracking/processInputRequest?HTMLVersion=5.0&sort_by=status&term_warn=yes&Requester=UPSHome&tracknums_displayed=5&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1=' + strTrackingNumber + '&InquiryNumber2=&InquiryNumber3=&InquiryNumber4=&InquiryNumber5=&AgreeToTermsAndConditions=yes&track.x=14&track.y=5'); 
	}
	else {
	    alert('Additional tracking details unavailable.');
	}
} 

function changeImage(strImg){
    var mainimage = document.getElementById("imgInventory");
   mainimage.src=strImg; 
}

function open_popup(page, width, height) 
{
    window.open(page,'PopupWindow','width='+width+',height='+height+',resizable=no,scrollbars=no');
}

function openMapPopup(lat, lng, alt) {
    window.open("viewmap.aspx?lat=" + lat + "&lng=" + lng + "&alt=" + alt, "map", "width=710,height=415,scrollbar=false");
}
