﻿function dbg(text) {
	$('dbg_msg').innerHTML = $('dbg_msg').innerHTML + text + '<br />';
}

function numbersonly(myfield, e, dec){
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}


function showElement(id){
	document.getElementById(id).style.display = "inline";
}

function hideElement(id){
	document.getElementById(id).style.display = "none";
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true;
	}else{
		return false;
	}

}

String.prototype.htmlEntities = function () { 
	return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); 
}; 

String.prototype.urlSave = function () { 
	var h = this.replace(/&/g,";+amp;");
	h = h.replace(/%/g,";perc;");
	h = h.replace(/\?/g,";qe;");
	h = h.replace(/#/g,";gat;");

	return h; 
}; 

String.prototype.myEncode = function () { 
	var h = encodeURI(this);
	h.replace(/&/g,"amp");
	h = h.replace(/%/g,"perc");
	h = h.replace(/\?/g,"qe");
	h = h.replace(/#/g,"gat");
	h = h.replace(/'/g,"1");
	h = h.replace(/"/g,"2");

	return h; 
};

String.prototype.urlSave2 = function () { 
	var h = this.stripTags();
	h = h.replace(/>/g,";gt;");
	h = h.replace(/</g,";lt;");
	h = h.replace(/&gt;/g,";gt;");
	h = h.replace(/&lt;/g,";lt;");
	h = h.replace(/&/g,";amp;");
	h = h.replace(/%/g,";perc;");
	h = h.replace(/\?/g,";qe;");
	h = h.replace(/#/g,";gat;");
	h = h.replace(/\+/g,";plus;");
	return urlencode(h);
};

String.prototype.txtField = function (){
	var h = this.replace(/&gt;/g,">");
	h = h.replace(/&lt;/g,"<");
	return h;
};

String.prototype.myEvalJSON = function () {
	return urldecode(this).evalJSON(true);
};




function myToggle(e){
	try{ 
		Effect.toggle(e, 'appear', {duration:0.3});
	}catch(err){}
}

function myPosToggle(e, x, y){
	try{ 
		e.style.top = y + 'px';
		e.style.left = x + 'px';
		Effect.toggle(e, 'appear', {duration:0.3}); 
	}catch(err){}
}

function findPos(obj){
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj == obj.offsetParent);
	return [curleft,curtop];
}


function savediv(from, to){
	try{ 
		if (myviewport === undefined) myviewport = document.viewport.getDimensions();
		document.getElementById('mysavediv').style.height = myviewport['height'] + 'px';
		document.getElementById('mysavediv').style.width = myviewport['width'] + 'px';
		Effect.toggle('mysavediv', 'appear', {duration:0.3, from:from, to:to});
	}catch(err){}
}

function modaldiv(from, to, zIndex){
	try{
		if (zIndex == undefined) zIndex = 10;
		if (myviewport === undefined) myviewport = document.viewport.getDimensions();
		document.getElementById('mymodaldiv').style.height = myviewport['height'] + 'px';
		document.getElementById('mymodaldiv').style.width = myviewport['width'] + 'px';
		document.getElementById('mymodaldiv').style.zIndex = zIndex;
		Effect.toggle('mymodaldiv', 'appear', {duration:0.3, from:from, to:to});
	}catch(err){}
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function checkEmail(email) {
	
	AtPos = email.indexOf("@");
	StopPos = email.lastIndexOf(".");
	var valid = true;
	if (email == "") {
		valid = false;
	}
	
	if (AtPos == -1 || StopPos == -1) {
		valid = false;
	}
	
	if (StopPos < AtPos) {
		valid = false;
	}
	
	if (StopPos - AtPos == 1) {
		valid = false;
	}
	
	if (valid){
		return true;
	}else{
		alert(tradnslate_validmail + ' ' + email);
		return false;
	}
	
}


function getDateString(seconds){
	if (seconds === undefined) seconds = false;
	var date =  new Date();
	var m = date.getMonth() + 1;
	if (m < 10) m = '0' + m;
	var d = date.getDate();
	if (d < 10) d = '0' + d;
	var s = date.getSeconds();
	if (s < 10) s = '0' + s;
	var min = date.getMinutes();
	if (min < 10) min = '0' + min;
	var h = date.getHours();
	if (h < 10) h = '0' + h;
	
	var retval = d + '.' + m + '.' + date.getFullYear() + ' um ' + h + ':' + min;
	if (seconds)  retval += ':' + s;
	return retval;
}



function urldecode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var histogram = {};
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
 
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
	var replace;
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}





/**
* headermenu
*/
//<body onmouseover="javaScript:menuMouseEventHandler(event);">
function menuMouseEventHandler(mEvent){
	
	var id;
  // Internet Explorer
  if (mEvent.srcElement)
  {
    id = mEvent.srcElement.id;
	if (id.length == 0){
		id = mEvent.srcElement.parentNode.id;	
	}
	if (id.length == 0){
		id = mEvent.srcElement.parentNode.parentNode.id;	
	}
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
    id = mEvent.target.id;
	if (id.length == 0){
		id = mEvent.target.parentNode.id;	
	}
	if (id.length == 0){
		id = mEvent.target.parentNode.parentNode.id;	
	}

  }
  
  if (id.substr(0,9) == 'menu_spot'){
	  document.getElementById('uppermenu_container').style.display=''; 
	  document.getElementById('menu_spot_container').style.display=''; 
	//$('stopper_menu_spot').style.display='';
	  document.getElementById('menu_gear_container').style.display='none';
	//$('stopper_menu_gear').style.display='none';
  }else{
  
	if (id.substr(0,9) == 'menu_gear'){
		document.getElementById('uppermenu_container').style.display=''; 
		document.getElementById('menu_gear_container').style.display=''; 
		//$('stopper_menu_gear').style.display='';
		document.getElementById('menu_spot_container').style.display='none';
		//$('stopper_menu_spot').style.display='none';
	}else{
		document.getElementById('uppermenu_container').style.display='none';
		document.getElementById('menu_spot_container').style.display='none';
		//$('stopper_menu_spot').style.display='none';
		document.getElementById('menu_gear_container').style.display='none';
		//$('stopper_menu_gear').style.display='none';
	}
  
  }
  
}

/*
	 			onmouseover="$('uppermenu_container').style.display=''; $('menu_gear').style.display=''; $('menu_gear_stopper').style.display='';"
	 			onmouseout="$('uppermenu_container').style.display='none'; $('menu_gear').style.display='none'; $('menu_gear_stopper').style.display='none';"


	 			onmouseover="$('uppermenu_container').style.display=''; $('menu_spot').style.display=''; $('menu_spot_stopper').style.display='';"
	 			onmouseout="$('uppermenu_container').style.display='none'; $('menu_spot').style.display='none'; $('menu_spot_stopper').style.display='none';"

*/

function fblogout(){
	//FB.Connect.logout();
	//location.href= baseUrl + '/auth/logout';
	ajaxmanagerpushSynchronous(baseUrl + '/auth/ajaxlogout/', "",
				function(request){
					location.href= baseUrl + '/auth/logout';
	});
}


function noPropagation(e){
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}


function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function myStopPropagation (e){
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();	
}