﻿var iTimerOut =10000;
var myArray =new Array ()
var golableZoneId=0
var tmp ='';
var ZoneAdCounter=0;
var myautorotate = "{myadautorotate:["  
var AdAutoRotate='';
var gvLogIp='';var gvLogKey='';var gvLogUA='';var gvPageKeyword='';

function LoadBanners(jsonBZData,logIp,logKey,logUserAgent,pageKeyword){
    if(jsonBZData=='' || jsonBZData=='undefined'){return false;}
    gvLogIp=logIp;gvLogKey=logKey;gvLogUA=logUserAgent;gvPageKeyword=pageKeyword
    tmp =  eval('(' + jsonBZData + ')');
    for (var i=0;i<tmp.AdData.length;i++){
        if (tmp.AdData[i].autoRotate!='false')
        {
            displayAd(i,tmp.AdData[i].AdId,tmp.AdData[i].AdKey,tmp.AdData[i].AdType,tmp.AdData[i].zone_id,tmp.AdData[i].AdRedirectURL,tmp.AdData[i].AdFileSrc,tmp.AdData[i].AdTextSrc,tmp.AdData[i].AdAltText,tmp.AdData[i].AdRedirectInNewWindow,tmp.AdData[i].zoneWidth,tmp.AdData[i].zoneHeight);
        }
        else{
              if (golableZoneId==0)
              {
               golableZoneId=tmp.AdData[i].zone_id;
               myautorotate+="{zoneid:" + tmp.AdData[i].zone_id + ",ArryCount:" +tmp.AdData[i].ZoneCount + ",index:0}"
                }
                else{
                       if (golableZoneId!=tmp.AdData[i].zone_id)
                       {
                        golableZoneId=tmp.AdData[i].zone_id;
                        myautorotate+=","
                        myautorotate+="{zoneid:" + tmp.AdData[i].zone_id + ",ArryCount:" +tmp.AdData[i].ZoneCount + ",index:0}" 
                        }
                   }
            }
    }
    // for function to display the first ads for each zone in array
    myautorotate+="]}"
    var objArray=myautorotate
    AdAutoRotate =  eval('(' + objArray + ')');
    if (AdAutoRotate.myadautorotate.length>0)
    {
        for (var j=0;j<AdAutoRotate.myadautorotate.length;j++)
        {
            var AdZoneRotate=AdAutoRotate.myadautorotate[j].zoneid;
            ProcessAutoRotateAd(AdZoneRotate,j);
        }
    }
}

function displayAd(arIndex,adId,adKey,adType,zid,DirectURL,FileSource,fileTextSrc,AdTitle,urlTarget,zoneWidth,zoneHeight)
{
try{
    var imgfile='';
    var target='_self';
    var adTargeturl=DirectURL;
    var urlCTParams =new Array;//ClickThough Params
    if(adTargeturl!=''){DirectURL='http://bannerpro.kuwait.tt/ClickThrough.aspx';}
    var BROWSER_AGENT_IS_MOBILE ='<%= Session("VISITOR_BROWSER_AGENT_IS_MOBILE") %>'; 
    if (urlTarget!='false')
        target='_blank';
    var dtFormat=new Date();   
    if (FileSource!=''){FileSource=FileSource + '?dt=' + dtFormat};
    if(fileTextSrc !='')
    {
        var fileExtension = fileTextSrc.slice(fileTextSrc.lastIndexOf(".")).toLowerCase();
        fileTextSrc=fileTextSrc + "?dt=" + dtFormat;
        if (BROWSER_AGENT_IS_MOBILE==true)
        {
            if (FileSource!='')
            {
                if (DirectURL!='')
                    imgfile ="<a style='cursor:pointer;' onclick=PostForm('" + DirectURL + "'," + arIndex + ",'" + target + "') target='" +  target + "'><img width='" + zoneWidth + "' height='" + zoneHeight + "' src='"+ FileSource + "' title='" + AdTitle + "'alt=" + AdTitle +"' border='0'/></a>"
                else
                    imgfile ="<img width='" + zoneWidth + "' height='" + zoneHeight + "' src='" + FileSource + "' title='" + AdTitle + "' alt='" + AdTitle + " ' border='0'/>"
            }            
        }
        else
        {
        // flash player
            //alert('fileExtension=' + fileExtension);
            if(fileExtension=='.html'){
                imgfile ="<iframe width='" + zoneWidth + "' height='" + zoneHeight + "' frameborder='0' scrolling='no' src='" + fileTextSrc + "'></iframe>";
            }
            else{
                if(adKey==71){
                    imgfile ="<object width='" + zoneWidth + "' height='" + zoneHeight + "' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0'>" +
                             "<param name='movie' value='" + fileTextSrc + "' />" +
                             "<param name='FlashVars' value='clickTag=http://ae.saxobank.com/ar/lp/main/trade-with-saxobank?dfaid=1&cmpid=dfa_2372793;1195435;76264668;' />" + 
                             "<!--[if !IE]>-->" + 
                             "<object type='application/x-shockwave-flash' data='" + fileTextSrc + "' width='" + zoneWidth + "' height='" + zoneHeight + "'>" +
                             "<param name='movie' value='" + fileTextSrc + "'/>" +
                             "<param name='FlashVars' value='clickTag=http://ae.saxobank.com/ar/lp/main/trade-with-saxobank?dfaid=1&cmpid=dfa_2372793;1195435;76264668;' />" + 
                             "<!--<![endif]-->" + 
                             "<a href='http://www.adobe.com/go/getflash'><img src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' title='Get Adobe Flash player'/></a>" + 
                             "<param name='quality' value='high'/>" + 
                             "<!--[if !IE]>-->" + 
                             "</object>" +
                             "<!--<![endif]-->" + 
                             "</object>";
                }else{
                    imgfile ="<object width='" + zoneWidth + "' height='" + zoneHeight + "' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0'><param name='movie' value='" + fileTextSrc + "'><param name='quality' value='high'><embed width='" + zoneWidth + "' height='" + zoneHeight + "' src='"+ fileTextSrc + "' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash'></embed></object>";
                }
            }
        }
    }   
    else
    {
        if (FileSource!='')
        {
            if (DirectURL!='')
               imgfile ="<a style='cursor:pointer;' onclick=PostForm('" + DirectURL + "'," + arIndex + ",'" + target + "') target='" +  target + "'><img width='" + zoneWidth + "' height='" + zoneHeight + "' src='" + FileSource + "' title='" + AdTitle + "'  alt='" + AdTitle + " ' border='0'/></a>"
            else
              imgfile ="<img width='" + zoneWidth + "' height='" + zoneHeight + "' src='" + FileSource + "' title='" + AdTitle + "' alt='" + AdTitle +"' border='0'/>"
        }   
    }
    try{
        //var bzid='ctl00_ContentPane_BPZ_'+zid;
        var objbz=null;
        if(zid==1){
        objbz=document.getElementById('ctl00_BPZ_'+zid);
        }else{
        //alert('zoneid ='  + zid+ ' - imgfile =' + imgfile);
        objbz=document.getElementById('ctl00_ContentPane_BPZ_'+zid);
        }

        if(objbz!=null){
        if (imgfile!='')
           objbz.innerHTML=imgfile;
         else
            objbz.style.display='none';
        }        
    }catch(e){alert('error :'+ e.message);}        
}catch(e){alert('adKey :' + adKey + '-' + e.message);}            
   
}

function ProcessAutoRotateAd(zoneId,adindex)
{
    if (AdAutoRotate.myadautorotate[adindex].ArryCount==1)
    {
        var JSONAdIndex = AdIndex(zoneId,AdAutoRotate.myadautorotate[adindex].index)
        displayAd(JSONAdIndex,tmp.AdData[JSONAdIndex].AdId,tmp.AdData[JSONAdIndex].AdKey,tmp.AdData[JSONAdIndex].AdType,zoneId,tmp.AdData[JSONAdIndex].AdRedirectURL,tmp.AdData[JSONAdIndex].AdFileSrc,tmp.AdData[JSONAdIndex].AdTextSrc,tmp.AdData[JSONAdIndex].AdAltText,tmp. AdData[JSONAdIndex].AdRedirectInNewWindow,tmp.AdData[JSONAdIndex].zoneWidth,tmp.AdData[JSONAdIndex].zoneHeight);
    } 
    else
    {
        //the array index for specified zone has not reached the end
        if (AdAutoRotate.myadautorotate[adindex].index != AdAutoRotate.myadautorotate[adindex].ArryCount-1)
        {
            //Serve ad for selected zone
            var JSONAdIndex = AdIndex(zoneId,AdAutoRotate.myadautorotate[adindex].index)
            // display the  ad content for each zone
            displayAd(JSONAdIndex,tmp.AdData[JSONAdIndex].AdId,tmp.AdData[JSONAdIndex].AdKey,tmp.AdData[JSONAdIndex].AdType,zoneId,tmp.AdData[JSONAdIndex].AdRedirectURL,tmp.AdData[JSONAdIndex].AdFileSrc,tmp.AdData [JSONAdIndex].AdTextSrc,tmp.AdData[JSONAdIndex].AdAltText,tmp.AdData[JSONAdIndex].AdRedirectInNewWindow,tmp.AdData[JSONAdIndex].zoneWidth,tmp.AdData[JSONAdIndex].zoneHeight);
            
            //Update the zone serve count
            AdAutoRotate.myadautorotate[adindex].index = AdAutoRotate.myadautorotate[adindex].index + 1
        }
        else  if(AdAutoRotate.myadautorotate[adindex].index == AdAutoRotate.myadautorotate[adindex].ArryCount-1)
        {
            var JSONAdIndex = AdIndex(zoneId,AdAutoRotate.myadautorotate[adindex].index)
            // display the  ad content for each zone
            displayAd(JSONAdIndex,tmp.AdData[JSONAdIndex].AdId,tmp.AdData[JSONAdIndex].AdKey,tmp.AdData[JSONAdIndex].AdType,zoneId,tmp.AdData[JSONAdIndex].AdRedirectURL,tmp.AdData[JSONAdIndex].AdFileSrc,tmp.AdData [JSONAdIndex].AdTextSrc,tmp.AdData[JSONAdIndex].AdAltText,tmp.AdData[JSONAdIndex].AdRedirectInNewWindow,tmp.AdData[JSONAdIndex].zoneWidth,tmp.AdData[JSONAdIndex].zoneHeight);            
            AdAutoRotate.myadautorotate[adindex].index = AdAutoRotate.myadautorotate[adindex].index + 1
        }
        if(AdAutoRotate.myadautorotate[adindex].index < AdAutoRotate.myadautorotate[adindex].ArryCount)
            setTimeout(function(){ProcessAutoRotateAd(zoneId,adindex)},iTimerOut)
        

   }          
}

function AdIndex(zid,adindex)
{
    ZoneAdCounter=0;
    for (var js=0;js<tmp.AdData.length;js++)
    {
        if (tmp.AdData[js].zone_id==zid)
        {
            if (ZoneAdCounter==adindex)
            {
                return js;
                break ;
            }
        ZoneAdCounter +=1;
        }
    }
}

function PostForm(url,jsonArIndex,target){
    urlCTParams={field1:tmp.AdData[jsonArIndex].AdId,field2:tmp.AdData[jsonArIndex].AdKey,field3:tmp.AdData[jsonArIndex].AdType,field4:tmp.AdData[jsonArIndex].zone_id,field5:gvPageKeyword,field6:gvLogIp,field7:gvLogKey,field8:gvLogUA};
    var Ref=null;
    var myForm = document.createElement("form");
    myForm.method="post";
    myForm.action = url;
    var myInput='';
    for (var param in urlCTParams){
        myInput = document.createElement("input") ;
        myInput.setAttribute("name", param) ;
        myInput.setAttribute("value", urlCTParams[param]);
        myForm.appendChild(myInput) ;
    }
    myForm.target = target; 
    document.body.appendChild(myForm) ;
    myForm.submit();
    document.body.removeChild(myForm);
}
