/*
 * Change Issue 	 Date		Programmer		Supervisor		Description
 * ----------------------------------------------------------------------------------
 * #1	 DA-3329     04.01.2010 Chandrashekhar	Saifee R	To add clint ip address as a new parameter in the QGW request
 * #2	 DA-3329     11.02.2011 Rishi 						To add widht and height parameter in iframe
 * #3    DA-3538     24-Sep-11  Natraj						Defect 1589 fix
 */
var ad_control_server_url =ad_url+"?op=getAdHTMLArray&ad_inventory_id="+ad_inventory_id;

// function to get cookie
function getCookie (name) { 
    	var dc = document.cookie;
    	var cname = name + "="; 
    	if (dc.length > 0) { 
      	begin = dc.indexOf(cname);
      	if (begin != -1) { 
        	begin += cname.length; 
        	end = dc.indexOf(";",begin);
        	if (end == -1) 
        		end = dc.length;
        	return unescape(dc.substring(begin, end));
      	} 
     	} 
    	return null; 
		}
		
// function to set cookie		
function setCookie ( name, value)
{
		var cookie_string = name + "=" + escape ( value );
		document.cookie = cookie_string;
}

// function to append parameter in url		
function ad_control_append_url(param, value) 
{
	if (value) {
		if(param=='hd') //#3
			value = value.replace(/&/g,'%26');  //#3
		ad_control_server_url += '&' + param + '=' + value;
	}
}


var ad_control_params_buf=new Array(
"userId"); // condensed array

// adding url parameter
function createUrl() 
{
	
	var hostname=window.location.href;
	if(typeof ad_fontSize != 'undefined')
			ad_control_append_url("fontSize",ad_fontSize);
	
	if(typeof ad_fontType != 'undefined')		
		ad_control_append_url("fontType",ad_fontType);
		
	if(typeof ad_fontColor != 'undefined')	
		ad_control_append_url("fontColor",ad_fontColor);
		
	if(typeof ad_backgroundColor != 'undefined')	
		ad_control_append_url("backGroundColor",ad_backgroundColor);
		
	if(typeof ad_border != 'undefined')
		ad_control_append_url("border",ad_border);
		
	if(typeof ad_response_is_structure != 'undefined')	
		ad_control_append_url("ad_response_is_structure",ad_response_is_structure);
		
	if(typeof max_num_ads != 'undefined')	
		ad_control_append_url("numberOfResults",max_num_ads);
		
	if(typeof page_number != 'undefined')	
		ad_control_append_url("pageNumber",page_number);
	
	if(typeof query_id != 'undefined')	
		ad_control_append_url("queryID",query_id);	
		
	if(typeof js_callback_fun != 'undefined')
		ad_control_append_url("callback",js_callback_fun);// callback function for json;
		
	if(typeof ad_category != 'undefined')
		ad_control_append_url("category",ad_category);
		
	if(typeof ad_keyword != 'undefined')
		ad_control_append_url("keyword",ad_keyword);	
		
	if(typeof ad_longitude != 'undefined')
		ad_control_append_url("loc.curr.long",ad_longitude);			
		
	if(typeof ad_latitude != 'undefined')
		ad_control_append_url("loc.curr.lat",ad_latitude);	
		
	if(typeof ad_media != 'undefined')
		ad_control_append_url("media",ad_media);	
		
	if(typeof ad_channel != 'undefined')
		ad_control_append_url("channel",ad_channel);	
		
	if(typeof ad_type != 'undefined')
		ad_control_append_url("type",ad_type);		
		
	if(typeof ad_size != 'undefined')
		ad_control_append_url("size",ad_size);	
		
	if(typeof ad_Price_Type != 'undefined')
		ad_control_append_url("priceType",ad_Price_Type);	
		
	if(typeof ad_city != 'undefined')
		ad_control_append_url("geoCity",ad_city);	
		
	if(typeof ad_Zip != 'undefined')
		ad_control_append_url("geoZip",ad_Zip);	
		
	if(typeof ad_country != 'undefined')
		ad_control_append_url("country",ad_country);	
		
	if(typeof ad_Shape_Ratio != 'undefined')
		ad_control_append_url("shapeRatio",ad_Shape_Ratio);		
		
	if(typeof ad_User_Agent != 'undefined')
		ad_control_append_url("userAgent",ad_User_Agent);	
		
	if(typeof ad_language != 'undefined')
		ad_control_append_url("language",ad_language);	
		
	if(typeof ad_Network_Type != 'undefined')
		ad_control_append_url("networkType",ad_Network_Type);	
		
	if(typeof ad_Sub_Network != 'undefined')
		ad_control_append_url("subNetwork",ad_Sub_Network);		
		
	if(typeof ad_device == 'undefined')
			ad_device = "web";// adding web by default
			
	if(typeof char_encoding != 'undefined')
		ad_control_append_url("cen",char_encoding);	
		
		if(typeof host_domain  == 'undefined')
	{
       ad_control_append_url("hd",hostname);	
	}
	else
		 ad_control_append_url("hd",host_domain);
		 
		 
	if(typeof json_bold_ind != 'undefined')
		ad_control_append_url("jbind",json_bold_ind);		
		
	//#1 Starts
	if(typeof clint_ip != 'undefined')
		ad_control_append_url("cIp",clint_ip);			
	//#1 Ends
			

	if(typeof char_encoding != 'undefined')
		ad_control_append_url("cen",char_encoding);	
		
	if(typeof test_ind != 'undefined')
		ad_control_append_url("test",test_ind);
		
	if(typeof ad_lPad != 'undefined')
		ad_control_append_url("lPad",ad_lPad);
	
}

//Trying to get userId from cookie, in case there is no userId, create and save it in the cookie of the specific user.
var userId = getCookie("userId");

if( userId == null){
	var time = new Date();
	timestamp = time.getTime();
	userId = Math.round(Math.random())*11+timestamp;
	setCookie("userId",userId);
}

createUrl();

for (var i = 0; i < ad_control_params_buf.length; i++){
	
	if(getCookie(ad_control_params_buf[i]) != null)
		ad_control_append_url(ad_control_params_buf[i], getCookie(ad_control_params_buf[i]));
}
//json
if(typeof  ad_response_is_structure != 'undefined' && ad_response_is_structure == "true")
{
	
	    var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = ad_control_server_url;

     	document.body.appendChild(newScript);
}
else if(ad_device == "web" && typeof  parent_id != 'undefined' && parent_id.length > 1)
{
	var el = document.createElement("iframe");
	el.name="myAd";
	el.style.border="0";       
	//el.style.width="0";
	//el.style.height="0";
	el.marginwidth="0";
	el.marginheight="0";
	el.vspace="0";
	el.hspace="0";
	el.allowtransparency="true";
	el.scrolling="no";
	el.frameBorder="0";
	
	if(typeof ad_width != 'undefined')
		el.width=ad_width;
		
	if(typeof ad_height != 'undefined')
		el.height=ad_height;
		
		
	el.src = ad_control_server_url;
	document.getElementById(parent_id).appendChild(el);
}
else if(ad_device == "web")
{
	var el = document.createElement("iframe");
	el.name="myAd";
	el.style.border="0";       
	//el.style.width="0";
	//el.style.height="0";
	el.marginwidth="0";
	el.marginheight="0";
	el.vspace="0";
	el.hspace="0";
	el.allowtransparency="true";
	el.scrolling="no";
	el.frameBorder="0";
	
		if(typeof ad_width != 'undefined')
			el.width=ad_width;
		
		if(typeof ad_height != 'undefined')
			el.height=ad_height;
		
		
	el.src = ad_control_server_url;
	document.body.appendChild(el);
}
else
{
	
	var el = document.createElement("frame");
	el.id = "myAd";
  el.name = "myAd";
  el.src = ad_control_server_url;
  var frameset = document.createElement("FRAMESET");
 // frameset.rows = "1*";
 // frameset.cols = "*";
 // frameset.frameborder="no";
//  frameset.framespacing="0";
  frameset.border="0";
  frameset.appendChild(el);
	document.body.appendChild(frameset);
	//window.location.href = ad_control_server_url;
}
		
	










