var currMapObj = null;
var currMapContainer = null;

function cmap()
{
	if (currMapObj)
	{
		GUnload();
		currMapObj.innerHTML = "";
		currMapObj.style.display = 'none';
		currMapObj = null;
	}
	if (currMapContainer)
	{
		currMapContainer.innerHTML = "";
		currMapContainer.style.display = 'none';
		currMapContainer = null;
	}
}
function omap(container,target,id)
{
	currMapContainer = $(container);
	currMapObj = $(target);
	
	if (currMapContainer)
	{
		currMapContainer.innerHTML = loading;
		currMapContainer.style.display = 'block';
	}
	if (currMapObj)
	{
    currMapObj.style.display = 'none';
		var param = 'action=readMap&loc_id=' + escape(id);
		ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,null,smap);
	}
}
function smap(text,status,xml)
{
	if (text != "")
	{
		if (currMapContainer)
		{
      currMapContainer.style.display = 'block';
			currMapContainer.innerHTML = "<strong>Karta för:</strong><br>"+text+"<br>";
		}
		if (currMapObj)
		{
			currMapObj.style.display = 'block';
			gmapLoad(currMapObj,escape(text),13,emap);
		}
	}
	else
	{
		if (currMapContainer)
		{
			currMapContainer.innerHTML = "Kartinformation saknas";
			currMapContainer.style.display = 'block';
		}
		if (currMapObj)
		{
			currMapObj.style.display = 'none';
		}
	}
}
function emap(a)
{
	if (currMapContainer)
	{
		currMapContainer.innerHTML = "Kunde ej hitta karta för sökorden:<br><br>" + a;
	}
	if (currMapObj)
	{
		currMapObj.style.display = 'none';
	}
}
// Save short information
function saveShort()
{
	$("preview_short").innerHTML = saving;
}
// Reload short preview
function saveShortDone()
{
	form = $("shortform");
	form.userfile.value = "";
	form.userfile_delete.checked = "";
	obj = $("preview_short");
	obj.innerHTML = loading;
	var param = 'action=readShort&id=' + escape(form.comp_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,null,saveShortDone2);
}
// Reload full preview
function saveShortDone2(text,status,xml)
{
	$("preview_short").innerHTML = text;
	obj = $("searchList");
	obj.innerHTML = loading;
	var param = 'action=readShortFull&id=' + escape(form.comp_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,obj);
}
// Reload offer images
function saveOfferDone()
{
	form = $("shortform");
	obj = $("shortform_img");
	obj.innerHTML = loading;
	var param = 'action=readOffer_img&offer_id=' + escape(form.offer_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,null,saveOfferDone2);
}
// Reload offer preview
function saveOfferDone2(text,status,xml)
{
  $("shortform_img").innerHTML = text; 
	form = $("shortform");
	obj = $("preview_short");
	obj.innerHTML = loading;
	var param = 'action=readOffer&offer_id=' + escape(form.offer_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,null,saveOfferDone3);
}
// Reload full offer preview
function saveOfferDone3(text,status,xml)
{
  $("preview_short").innerHTML = text; 
	obj = $("searchList");
	obj.innerHTML = loading;
	var param = 'action=readOfferFull&offer_id=' + escape(form.offer_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,obj);
}
// Save categories
function saveCats()
{
	form = $("catsform");
	$("preview_cats").innerHTML = saving;
	var objects = getElementsByClassName('pcatselected','p');
	var keystr = "";
	for (i=0;i<objects.length;i++)
	{
		obj = objects[i];
		objid = obj.id;
		objidlen = objid.length;
		keystr += objid.substr(4,objidlen-4)+":";
	}
	form.comp_cats.value = keystr;
}
// Reload categories
function saveCatsDone()
{
	form = $("catsform");
	obj = $("preview_cats");
	obj.innerHTML = loading;
	var param = 'action=readCats&id=' + escape(form.comp_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,obj);
}
// Reload categories
function saveOfferCatsDone()
{
	form = $("catsform");
	obj = $("preview_cats");
	obj.innerHTML = loading;
	var param = 'action=readOfferCats&offer_id=' + escape(form.offer_id.value);
	ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,obj);
}
// Class for location updates
function locationUpdate(comp_id,loc_id)
{
	var that = this;
	this.comp_id = comp_id;
	this.loc_id = loc_id;
	
	this.update = function() 
	{
		obj = $("locationsForm");
		obj.innerHTML = loading;
		var param = 'action=getLocForm&id='+that.comp_id+'&loc_id='+that.loc_id;
		ajaxGetMe('http://www.bildelar.se/ajaxGet.php',param,null,that.done);
	}
	this.done = function(text,status,xml)
	{
		obj = $("locationsForm");
		obj.innerHTML = text;
		if (that.loc_id == -1)
		{
      $("preview_loc").innerHTML = "Ny adress:<br>Fyll i formuläret till vänster för att generera en karta!";
      $("preview_map").style.display = 'none';
		}
		else
		{
      omap("preview_loc","preview_map",that.loc_id);
    }
	}
}

function picLocation(comp_id,loc_id)
{
		var l = new locationUpdate(comp_id,loc_id);
		l.update();
}

function getChildren(obj,classname) {
 var anchs =  obj.getElementsByTagName("input");
 var total_anchs = anchs.length;
 var regexp = new RegExp('\\b' + classname + '\\b');
 var class_items = new Array()
 
 for(var i=0;i<total_anchs;i++) { //Go thru all the links seaching for the class name
  var this_item = anchs[i];
  if(regexp.test(this_item.className)) {
   class_items.push(this_item);
  }
 }
 return class_items;
}

function toggleChecklist(id)
{
  form = $(id);
  objects = getChildren(form,"checklist");
  if (form.checkAll.checked)
  {
    for (i=0;i<objects.length;i++)
      objects[i].checked = true ;
  }
  else
  {
    for (i=0;i<objects.length;i++)
      objects[i].checked = false ;
  }
}