/* Ajax Routines */

var xmlHttp;
var rootpath;
var active_id = "";
rootpath = "http://diadora.exanano.com/ajax/";
function GetXmlHttpObject(handler)
{
    var objXmlHttp=null
 
    if (navigator.userAgent.indexOf("Opera")>=0)
    {
        alert("Opera not supported...")
        return;
    }
    if (navigator.userAgent.indexOf("MSIE")>=0)
    {
        var strName="Msxml2.XMLHTTP"
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
        {
            strName="Microsoft.XMLHTTP"
        }
        try
        {
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler
            return objXmlHttp
        }
        catch(e)
        {
            alert("Error. Scripting for ActiveX might be disabled")
            return
        }
    }
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler
        return objXmlHttp
    }
} 

function get_fields(competition_id, num_fields)
{  
	//if(competition_id != ""){
    var url= "../php_templates/form_fields.ajax.php?competition_id=" + competition_id + "&num_fields=" + num_fields;
		document.getElementById("form_fields").innerHTML="<span class='info'>populating fields...</span>";
    xmlHttp=GetXmlHttpObject(stateChanged1);
		
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);		
	//}	
}
function stateChanged1()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {  			  
			data = xmlHttp.responseText;			
			document.getElementById("form_fields").innerHTML=data;			
			launch_js(data);
    }
}

var sel;
function get_size(product_id, colorname, theSel){
	sel = theSel;
	var url = "../../php_templates/get_size.tmp.php?product_id=" + product_id + "&colorname=" + colorname;
	document.getElementById(sel).innerHTML="loading size..";
	xmlHttp=GetXmlHttpObject(stateChanged2);
	
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);	
}
function stateChanged2()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {  			  
			data = xmlHttp.responseText;			
			document.getElementById(sel).innerHTML=data;						
    }
}


function launch_js(data) {
  // RegExp from prototype.sonio.net
  var ScriptFragment = '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';
           
  var match    = new RegExp(ScriptFragment, 'img');
  var scripts  = data.match(match);
		
    if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
            var match = new RegExp(ScriptFragment, 'im');
            js += scripts[s].match(match)[1];
        }
        eval(js);
    }
}

function get_products(str, product_id)
{  	
	if(str!=""){
    var url=rootpath + "products.php" + "?cat_id=" + str + "&product_id=" + product_id;
		document.getElementById("form1_subcat").innerHTML="<span class='normaltext'>loading...</span>";
    xmlHttp=GetXmlHttpObject(stateChanged);
		
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);		
	}	
}
function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {  	
			data = xmlHttp.responseText;
			
			document.getElementById("form1_subcat").innerHTML=xmlHttp.responseText;
			//launch_js(data);
    }
}

function add_associate(product_id){
	var ctrl=document.getElementById("associate");
	var ctrl1 = document.getElementById("products");
	var txt1 = ctrl1.options[ctrl1.selectedIndex].text;		
	var len = ctrl.options.length;
	
	if(product_id != ""){		
		ctrl.options[len] = new Option(txt1, product_id);
		ctrl.options[len].selected = true;		
	}
}
function remove_associate(){
	var ctrl=document.getElementById("associate");
	sel = 0;
	for(i=ctrl.options.length-1; i>=0; i--){
		if(ctrl.options[i].selected){			
			sel =1;	
			ctrl.options[i] = null;			
		}		
	}
	if(sel == 0){
		alert("please select a product to remove");
	}
}
