function serialize(input) {
	var tmp = new Array(input.length);
	for (var i = 0; i < input.length; i++) {
		tmp[i] = escape(input[i]);
	}
	return tmp.join(',');
}

function deserialize(input) {
	var output = input.split(',');
	for (var i = 0; i < output.length; i++) {
		output[i] = unescape(output[i].replace(/\+/g, " "));
		//output[i] = decode64(output[i]);
	}
	return output;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}


function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function do_suggest_cb(output) {
	if (output == "") {
		return;
	}
	output = deserialize(output);
	this.document.pictureedit.newtitle.value          = output[1];
	this.document.pictureedit.newpicturedate.value    = output[2];
	this.document.pictureedit.newpicturetime.value    = output[3];
	this.document.pictureedit.newcustomlocation.value = output[4];
	this.document.pictureedit.newlocation.value       = parseInt(output[5]);
	this.document.pictureedit.newspot.value           = parseInt(output[6]);
	this.document.pictureedit.newcountry.value        = parseInt(output[7]);
	this.document.pictureedit.newtype.value        	  = parseInt(output[8]);
	this.document.pictureedit.newoperator.value    	  = parseInt(output[9]);
	this.document.pictureedit.newstock.value       	  = parseInt(output[10]);
	this.document.pictureedit.newconfiguration.value  = parseInt(output[11]);
	this.document.pictureedit.newlicense.value        = parseInt(output[12]);
}

function do_suggest() {
	var input = new Array(12);
	input[0] = this.document.pictureedit.editid.value;
	input[1] = this.document.pictureedit.newtitle.value;
	input[2] = this.document.pictureedit.newpicturedate.value;
	input[3] = this.document.pictureedit.newpicturetime.value;
	input[4] = this.document.pictureedit.newcustomlocation.value;
	input[5] = this.document.pictureedit.newlocation.value;
	input[6] = this.document.pictureedit.newspot.value;
	input[7] = this.document.pictureedit.newcountry.value;
	input[8] = this.document.pictureedit.newtype.value;
	input[9] = this.document.pictureedit.newoperator.value;
	input[10] = this.document.pictureedit.newstock.value;
	input[11] = this.document.pictureedit.newconfiguration.value;
	input[12] = this.document.pictureedit.newlicense.value;

	x_suggest(serialize(input), do_suggest_cb);
}

var mapwindow = null;
var a = false;
function open_map(url) {
	if ((a == false) || (mapwindow.closed == true)) {
		mapwindow = open(url,"mapwindow","width=800,height=600,screenX=0,screenY=0,locationbar=no,menubar=no,status=no,resizeable=yes");
	} else {
		mapwindow.document.location.href=openlink;
	}
	a = true;
	mapwindow.focus();
}

function open_map_modify(center) {
	var url = "/index.php?map=1&center="+center;
	open_map(url);
}

function open_map_showspot(spot) {
	var url = "/index.php?showspot=" + spot;
	open_map(url);
}

function extractmainloc(location) {
	location = location.split(" - ")[0];
	location = location.split(", ")[0];
	return location;
}
