var xmlHttp;
var styleuid;
var style_page;
var append_unique_template_ref;
var template;

function getNextColour(strURL){
	append_unique_template_ref = 0;
	xmlHttp=GetXmlHttpObject();
	var url=strURL;	
	// Trap the style uid (Essential that the styleuid is the first param passed the through)
	var regex = new RegExp(/^(.*)\?styleuid=(.*)\&colouruid=(.*)$/);
	var regex2 = new RegExp(/^(.*)\?styleuid=(.*)\&colouruid=(.*)\&template=(.*)\&style_view=(.*)&(.*)$/);
	var regex3 = new RegExp(/^(.*)\?(.*)\&template=(.*)\&ctr=(.*)\&append_template=(.*)$/);
	var matches = regex.exec(url);
	var matches2 = regex2.exec(url);	
	var matches3 = regex3.exec(url);
	styleuid = matches[2];
	
	// test for style page stuff
	if (matches2 != null){
		style_page = matches2[5];
	}
	
	// test for sales page stuff
	if (matches3 != null){
		template = matches3[3];
		append_unique_template_ref = matches3[5];	
	}
	
	xmlHttp.onreadystatechange=stateChanged; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var ele_id = 'product_img_' + styleuid;
		
		if (append_unique_template_ref == 1){
			ele_id = ele_id + '_' +  template;
		}
		
		//alert('HERE ID >>>' + ele_id + ' appen unique >> ' + append_unique_template_ref);
		document.getElementById(ele_id).style.display="inline"; // needed to override country selection de-selection
 		if (style_page == 1){
 			document.getElementById(ele_id).innerHTML=parseScript(xmlHttp.responseText); 
 		//	//document.getElementById('criteoscript').innerHTML = eval("document.write('<div id=\"cto_gsi_7708762_ac\" style=\"display:none\">')";"document.write('<div class=\"ctoWidgetServer\">http:\/\/widget.criteo.com\/pmo\/<\/div>')";"document.write('<div class=\"ctoWidgetType\">getSimilarItems<\/div>')";"document.write('<div class=\"ctoParams\">wi=7708762&i=20345018<\/div>')";"document.write('<\/div>')" );
 		}
		else{
			document.getElementById(ele_id).innerHTML=xmlHttp.responseText;
		}
 	} 
}

function GetXmlHttpObject(){	
  	try {    
	  	// Firefox, Opera 8.0+, Safari    
	  	xmlHttp=new XMLHttpRequest();    
	} catch (e) {    
  		// Internet Explorer    
  		try { 
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  
	  	} catch (e) {      
    		try {  
	    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");    
	    	} catch (e) {        
      			alert("Your browser does not support AJAX!");       
      			return false;        
  			}      
  		}    
  	}  	
  	return xmlHttp;	
}

function parseScript(_source) {
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
	
	// Return the cleaned source
	return source;
}


