function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}

function change_manuf_category(loc)
{
	r = $('list_manufac');
	new Form.Element.Observer(
  		'cat_list',
  		1,  // 100 milliseconds
  		function(el, value)
  		{
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/change_list.php?id="+value+"&pos=catmanuf&item="+item+"&location="+loc,
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            		r.show();
					var sel = document.getElementById('list_manufac');
					while (sel.options.length)
					{
						sel.options[0] = null;
					}
					var firstRes = new String(transport.responseText);
					var resArray = firstRes.split(",");
					//alert(resArray.length);
					for (var i = 0; i < resArray.length; i++)
					{
						var resString = new String(resArray[i]);
						valArray = null;
						valArray = resString.split(":");
						var newOpt = new Option(valArray[1], valArray[0]);
						sel.options[i] = newOpt;
					}
            	},
            	onFailure: function()
            	{
					alert("nothing");
            		r.innerHTML = null;
            	}
          	});
  		})
}

function change_model_category(loc)
{
	r = $('list_model');
	new Form.Element.Observer(
  		'cat_list',
  		1,  // 100 milliseconds
  		function(el, value)
  		{
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/change_list.php?id="+value+"&pos=category&item="+item+"&location="+loc,
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            		r.show();
					var sel = document.getElementById('list_model');
					while (sel.options.length)
					{
						sel.options[0] = null;
					}
					var firstRes = new String(transport.responseText);
					var resArray = firstRes.split(",");
					//alert(resArray.length);
					for (var i = 0; i < resArray.length; i++)
					{
						var resString = new String(resArray[i]);
						valArray = null;
						valArray = resString.split(":");
						var newOpt = new Option(valArray[1], valArray[0]);
						sel.options[i] = newOpt;
					}
            	},
            	onFailure: function()
            	{
					alert("nothing");
            		r.innerHTML = null;
            	}
          	});
  		})
}

function change_model_manufac(loc)
{
	r = $('list_model');
	new Form.Element.Observer(
  		'list_manufac',
  		1,  // 100 milliseconds
  		function(el, value)
  		{
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/change_list.php?id="+value+"&pos=manufac&item="+item+"&location="+loc,
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            		r.show();
					var sel = document.getElementById('list_model');
					while (sel.options.length)
					{
						sel.options[0] = null;
					}
					var firstRes = new String(transport.responseText);
					var resArray = firstRes.split(",");
					//alert(resArray.length);
					for (var i = 0; i < resArray.length; i++)
					{
						var resString = new String(resArray[i]);
						valArray = null;
						valArray = resString.split(":");
						var newOpt = new Option(valArray[1], valArray[0]);
						sel.options[i] = newOpt;
					}
            	},
            	onFailure: function()
            	{
					alert("nothing");
            		r.innerHTML = null;
            	}
          	});
  		})
}

function fill_title_init()
{
	r = $('title');
	new Form.Element.Observer(
  		'list_cond',
  		1,  // 100 milliseconds
  		function(el, value)
  		{
			r.value = "Processing.... please wait...";
			var item = gup("item_id");
			var hire = gup("hire");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/fill.php?id="+item+"&hire="+hire+"&cond="+value,
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            		r.show();
	            	r.value = transport.responseText || "error transfering data";
					$('text').value = transport.responseText;
            	},
            	onFailure: function()
            	{
            		r.value = "search error";
            	}
          	});
  		})
}

function update_model_list()
{
	new Event.observe('list_model', 'change',
  		function()
  		{
			//r.value = "Processing.... please wait...";
			var value = this.value;
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/update.php?model="+value+"&id="+item+"&pos=model_list",
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            	},
            	onFailure: function()
            	{
            	}
          	});
  		})
}

function update_manuf_text()
{
	new Event.observe('an_manufac', 'change',
  		function()
  		{
			//r.value = "Processing.... please wait...";
			var value = this.value;
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/update.php?model="+value+"&id="+item+"&pos=manuf_txt",
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            	},
            	onFailure: function()
            	{
            	}
          	});
  		})
}

function update_model_text()
{
	new Event.observe('an_model', 'change',
  		function()
  		{
			//r.value = "Processing.... please wait...";
			var value = this.value;
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/update.php?model="+value+"&id="+item+"&pos=model_txt",
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            	},
            	onFailure: function()
            	{
            	}
          	});
  		})
}

function update_title_text()
{
	new Event.observe('title', 'change',
  		function()
  		{
			//r.value = "Processing.... please wait...";
			var value = this.value;
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/update.php?model="+value+"&id="+item+"&pos=title",
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            	},
            	onFailure: function()
            	{
            	}
          	});
  		})
}

function update_details_text()
{
	new Event.observe('text', 'change',
  		function()
  		{
			//r.value = "Processing.... please wait...";
			var value = this.value;
			var item = gup("item_id");
			if (item == "")
				item = gup("ad_id");
        	new Ajax.Request("ajax/update.php?model="+value+"&id="+item+"&pos=details",
          	{
	            method:"get",
    		    onSuccess: function(transport)
    		    {
            	},
            	onFailure: function()
            	{
            	}
          	});
  		})
}

