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()
            	{
            	}
          	});
  		})
}

function changeCategory()
{
	var selMan = $('searchManufacturerSelect');
	var selMod = $('searchModelSelect');
	var valCat = $F('searchCategorySelect');
	if (valCat != '' && valCat != '-1')
	{
	   	new Ajax.Request('ajax/admin_search.php?select=manufacturer&category='+valCat,
  		{
			method:'get',
		   	onSuccess: function(transport)
    		{
				while (selMan.options.length)
				{
					selMan.options[0] = null;
				}
				var firstRes = new String(transport.responseText);
				var resArray = firstRes.split(",");
				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]);
					selMan.options[i] = newOpt;
				}
			},
	        onFailure: function()
    	    {
				alert("nothing");
			}
		});
	}
	else
	{
		while (selMan.options.length)
		{
			selMan.options[0] = null;
		}
		var newOpt = new Option('Select Value', '');
		selMan.options[0] = newOpt;
		var newOpt = new Option('n/a', '-1');
		selMan.options[1] = newOpt;
		while (selMod.options.length)
		{
			selMod.options[0] = null;
		}
		var newOpt = new Option('Select Value', '');
		selMod.options[0] = newOpt;
		var newOpt = new Option('n/a', '-1');
		selMod.options[1] = newOpt;
	}
}

function changeManufacturer()
{
	var selMod = $('searchModelSelect');
	var valCat = $F('searchCategorySelect');
	var valMan = $F('searchManufacturerSelect');
	if (valMan != '' && valMan != '-1')
	{
	   	new Ajax.Request('ajax/admin_search.php?select=model&category='+valCat+'&manuf='+valMan,
  		{
			method:'get',
		   	onSuccess: function(transport)
    		{
				while (selMod.options.length)
				{
					selMod.options[0] = null;
				}
				var firstRes = new String(transport.responseText);
				var resArray = firstRes.split(",");
				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]);
					selMod.options[i] = newOpt;
				}
			},
	        onFailure: function()
    	    {
				alert("nothing");
			}
		});
	}
	else
	{
		while (selMod.options.length)
		{
			selMod.options[0] = null;
		}
		var newOpt = new Option('Select Value', '');
		selMod.options[0] = newOpt;
		var newOpt = new Option('n/a', '-1');
		selMod.options[1] = newOpt;
	}
}
