//OAS-Adtech migration script
//Created 2/26/08 by SSS-GMTI 
//Updated Regex change to determine the dynamic ads 3/31/2008 by sss-gmti
//Updated OAS_sitepage value to ignore the ending "/" and also updated new placement id 5/8/2008 by sss-gmti
//5/15/2008 -sss- updated the script tag so that link scanning software dont interpret the script tag. 
//Script is mainly used to migrate from  OAS to Adtech.Still the calls will use OAS_AD function itself. 
//Variable inizialization 

//Initial setup 
	if (adtech_selected !=1){
		OAS_version = 11;
		if (navigator.userAgent.indexOf('Mozilla/3') != -1 || navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1)
			OAS_version = 10;
		if (OAS_version >= 11)
			document.write('<scr'+'ipt language="javascript1.1" SRC="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '?' + OAS_query +'"></scri'+'pt>');
	}
	else
	{
		var adtech_enabled = 0;	
	    if (adtech_global_control ==1){
	        var randomnumber = Math.round(Math.random()*3 + 1);
			if( randomnumber >  (4- adtech_throttle)){
				adtech_enabled = 1; 
	            var keyword_counter=0;
		        var keyvaluepair_counter=0;
		        var g_keywordstring="";
		        var g_keyvaluepairstring="";
				var g_sitepage=OAS_sitepage;
				//Add the query paramter to key value pair 
		        AddKeyvaluepair(OAS_query);
	            if (window.adgroupid == undefined) {
                    window.adgroupid = Math.round(Math.random() * 1000000);
		        }
				 g_sitepage = g_sitepage.replace(/\/$/,''); 
        	}
        }		
	}
	
//This the OAS_Ad which is being used by both OAS and Adtech to make calls. 
function OAS_AD(pos) {
 
	if (adtech_selected !=1){
		if (OAS_version >= 11)
			OAS_RICH(pos);
		else
			OAS_NORMAL(pos);
	}
	else
	{
	//verify whether the Adtech was enabled in "Initial setup" section
	  if(adtech_enabled==1){
		       	var posReg = RegExp(/(\d+)x(\d+)_(.*)/);
				var sizepos ="";
		        var adtech_creativesize = "";
		        var g_keywordtag ="";
		        var g_keyvaluepairtag ="";
        		
		        if (pos.match(posReg)){
		        adtech_creativesize = "-1";
		        sizepos= pos.replace(/_\w$/, '');
		        adtech_pos = "size="+sizepos+";";
		        }
		        else {
		        adtech_creativesize = "0";
		        adtech_pos = "";
		        }
    		    if (g_keywordstring!= "")
    		        g_keywordtag ="key="+g_keywordstring+";";
    		    if (g_keyvaluepairstring != "")
    		      g_keyvaluepairtag=g_keyvaluepairstring+";";
		        document.write('<scr'+'ipt language="javascript1.1" src="http://'+adtechserver+'/addyn/3.0/'+adtechnetworkid +'/133600/0/'+ adtech_creativesize +'/ADTECH;'+ adtech_pos +'alias='+ g_sitepage+'_'+pos+';cookie=info;loc=100;target=_blank;'+g_keywordtag+g_keyvaluepairtag+'grp='+window.adgroupid+';misc='+new Date().getTime()+'"></scri'+'pt>');
		}
	}
}
//This function is only used by adtech to add the keyvaluepair to a variable. 
function AddKeyvaluepair(key){
    
		var querySplit = key.split("&");
		var keywordFound = 0;
			for(i = 0; i < querySplit.length ; i++){
                //Check whether the key is in key-value pair 
	            var keyvaluepairsplit = querySplit[i].split("=");			
					if (keyvaluepairsplit!="" && keyvaluepair_counter < 8){
						for (var j =0;j<adtech_keyvalue_Array.length;j++){
							if(adtech_keyvalue_Array[j].toLowerCase() == keyvaluepairsplit[0].toLowerCase()){
								keywordFound =1;
								break;
							}
						} //if found add the keyvalue pair to a string 
						if(keywordFound ==1){
					        if (g_keyvaluepairstring=="")
							    g_keyvaluepairstring= "kv"+querySplit[i];
						    else
							    g_keyvaluepairstring = g_keyvaluepairstring +";" + "kv"+querySplit[i];
					        keyvaluepair_counter++;
							}
						keywordFound=0;
					}
				
			}
}
//This function is only used by adtech to add the keywords to a variable. 
function AddKeyword(key){
    if (keyword_counter < 8){
			if (g_keywordstring=="")
				g_keywordstring= key;
			else
				g_keywordstring = g_keywordstring +"+" + key;
		keyword_counter++;
		}
}
