
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popup(mylink, windowname, window_width, window_height, resizable, scrollbars, status, menubar, toolbar, location )
{
	if (!window.focus) return true;
	
	var parms =	"'top=" + ((screen.height - window_height) / 2) + "," +
			"left=" + ((screen.width - window_width) / 2) + "," +
			"width=" + window_width + "," +
			"height=" + window_height + "," +
			"resizable=" + get_yes_no_string(resizable) + "," +
			"scrollbars=" + get_yes_no_string(scrollbars) + "," +
			"status=" + get_yes_no_string(status) + "," +
			"menubar=" + get_yes_no_string(menubar) + "," +
			"toolbar=" + get_yes_no_string(toolbar) + "," +
			"location=" + get_yes_no_string(location) + "'";

	var newwindow = window.open(mylink, windowname, parms);

	if (window.focus) {newwindow.focus();}

	return newwindow;
}

function get_yes_no_string(value) {
	return (typeof value == 'string' && (value.toLowerCase() == 'yes' || value.toLowerCase() == 'no')) ? value.toLowerCase() : (value) ? "yes" : "no";
}


function inputbox_navigation(obj, next_obj,ev) {

	//if(ev.keyCode==9 || ev.keyCode==16) {
	if(ev.keyCode==9 ) {
		obj.focus();
		obj.select();
		return;
	}

	if(obj.maxLength==obj.value.length) {
		var next_obj = document.getElementById(next_obj);
		var key = ev.keyCode;
		next_obj.focus();
		next_obj.select();
	}
}

function strip_inputbox_non_digits(obj) {
	obj.value = strip_non_digits(obj.value);
}

function uppercase_inputbox(obj) {
	obj.value = obj.value.toUpperCase();
}

function strip_non_digits(n) {
	var s = String(n);
	var re = /\D/g;
	return s.replace(re,"");
}

function hide_object(obj) {

	if(type_of(obj)!="object") {	
		obj = document.getElementById(obj);
		if(obj==null) 
			return;
	}


	switch(obj.nodeName.toUpperCase()) {

		case("DIV"):
	  		obj.style.visibility='hidden';
			break
		default:
			obj.style.visibility='hidden';
			obj.style.display ='none';
		break
	}

}

function is_hidden(obj) {
	if(type_of(obj)!="object") 
		obj = get_object(obj);
	
	if(obj!=null) 
		return (obj.style.visibility == 'hidden' || obj.style.display == 'none');
	
	return false;
}

function toggle_object(obj) {
	if(type_of(obj)!="object") 
		obj = get_object(obj);
		
	if(obj!=null) {
		if(obj.style.visibility == 'hidden' || obj.style.display == 'none')
			show_object(obj);
		else hide_object(obj);
	}
}

function show_object(obj) {
	if(type_of(obj)!="object") 
		obj = document.getElementById(obj);
		
	if(obj!=null) {
		obj.style.display= '';
		obj.style.visibility='visible';
	}
}


function get_object(id) {
	return document.getElementById(id);
}


function is_explorer() { return checkIt('msie'); }
function is_firefox() { return checkIt('firefox'); }
function is_safari() { return checkIt('safari'); }




function checkIt(string)
{
	var detect = navigator.userAgent.toLowerCase();
	return detect.indexOf(string)>0;
}


function type_of( vArgument )
{
 	var sTypeOf = typeof( vArgument );
	switch ( sTypeOf )
	{
		case "string":
			return "string";
			break;
		case "object":
						
			if( vArgument.constructor == Array )
			    return "array";
			else
			   return "object";
			   
		case "undefined":
			return "undefined";


	}
}

function is_defined(obj) { return type_of(obj)!=="undefined"; }
function is_array(obj) { return type_of(obj)=="array"; }


function submit_form(form_name) {
	if(is_defined(form_name)) {
		obj = get_object(form_name);
		if(obj!=null) obj.submit();	
	} else {
		obj = document.forms[0];
		if(obj!=null) obj.submit();	
	}
}

function on_submit(form_name) {
	obj = document.getElementById(form_name);
	obj.onsubmit();
}

function resize_window(scr_w,scr_h) {

	var browseWidth, browseHeight;

	if(document.layers || (document.getElementById&&!document.all)){
		browseWidth=window.outerWidth;
		browseHeight=window.outerHeight;
	} else if(document.all) {
	   browseWidth=document.body.clientWidth + 10;
	   browseHeight=document.body.clientHeight + 10;
	}

	if(scr_w != browseWidth && scr_w!=null)
		browseWidth = scr_w;
	if(scr_h != browseHeight && scr_h!=null)
		browseHeight = scr_h;

	window.resizeTo(browseWidth,browseHeight);
}

//
function confirm_submit(formname,msg) {
	var agree=confirm(msg);
	if (agree)	submit_form(formname);
	else		return false;
}

function toggle_select(name) {
	var forminputs = document.getElementsByTagName('input');

	for (i = 0; i < forminputs.length; i++) {
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
			if (regex.test(forminputs[i].getAttribute('name'))) {
			if(forminputs[i].checked)
				forminputs[i].checked = false;
			else forminputs[i].checked = true;
		}
	}
}

function select_all_checkboxes(form, name, value) {
	var formblock= document.getElementById(form);
	var forminputs = formblock.getElementsByTagName('input');
	
	for (i = 0; i < forminputs.length; i++) {
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		
		if (regex.test(forminputs[i].getAttribute('name')))
			forminputs[i].checked = (value) ? true : false;
	}
}

function dropdown_select(id,is_select) {
	for(var i = 0; i < $(id).options.length; i++)
		$(id).options[i].selected = is_select;
}

function select_all(id) {
	dropdown_select(id,true);
}

function select_none(id) {
	dropdown_select(id,false);
}


function select_dropdown(id, value) {	
	var obj = get_object(id);
	if(obj)
		for (i = 0; i < obj.options.length; i++)
			if(obj.options[i].value==value)
				obj.options[i].selected = true;		
}



function set_field_value(fieldname, value) {
	var obj = document.getElementById(fieldname);
	obj.value = value;
}

function focus_object(id) {
	set_field_focus(id);
}

function select_radiobutton(id,selected) {

	selected = (selected==null) ? true : selected;
	var obj = document.getElementById(id);

	if(obj!=null)
		obj.checked = selected;

}

function check_checkbox(id) {
	var obj = document.getElementById(id);

	if(obj!=null)
		obj.checked = true;
}

function set_field_focus(fieldname) {
	obj = get_object(fieldname);
		if(obj)
			// IE8 will through an error when trying to focus on a hidden field, so use trapping
			try {
				obj.focus();
			} catch(err) {}
}

function redirect(url) {
	window.location = url;
}

function submit_form_back(form_name) {
	obj = get_object("cmd_back");
	if(obj) {
		obj.value = "1";
		submit_form(form_name);
	}

}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	

	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}



function get_selected_radiobutton_value(name) {
	var forminputs = document.getElementsByTagName('input');

	for (i = 0; i < forminputs.length; i++) {
		// regex here to check name attribute
		var regex = new RegExp(name);
		if (regex.test(forminputs[i].getAttribute('name'))) 
			if(forminputs[i].checked) return forminputs[i].value;			
	}
	return false;
}

function get_elements_by_name(type,name) {
	var elements = new Array();
	
	var objects = document.getElementsByTagName(type);
	var index = 0;

	for (i = 0; i < objects.length; i++) {
		var regex = new RegExp(name);
		if (regex.test(objects[i].getAttribute('name')))
			elements[index++] = objects[i];	
	}
	return elements;	
}

function select_radio_button_by_name(name,selected_value) {
	elements = get_elements_by_name("input",name);
	for(i=0;i<elements.length;i++) {
		if(elements[i].value==selected_value)
			elements[i].checked = true;
	}
}



function get_window_dimensions() {	
	var width = 0, height = 0;
	if (typeof( window.innerWidth ) == "number") {
		width = window.innerWidth;
		height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}

	var dim = Array(2);
	dim[0] = width;
	dim[1] = height;		
	return dim;
}

function set_inner_html(div,content) {
	document.getElementById(div).innerHTML = content;
	
	var search = content;
	var script;

	while( script = search.match(/(<script>\s*(<!--)?)/i)) {
	
		search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);

		if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;

		block = search.substr(0, search.indexOf(RegExp.$1));
		search = search.substring(block.length + RegExp.$1.length);

		var oScript = document.createElement('script');
		oScript.text = block;
		document.getElementsByTagName("head").item(0).appendChild(oScript);		
	}
}

// If NS -- that is, !is_explorer() -- then set up for mouse capture
if (!is_explorer()) document.captureEvents(Event.MOUSEMOVE)
if (!is_explorer()) document.captureEvents(Event.KEYDOWN)

function get_mouse_cords(e) {

	var tempX = 0;
	var tempY = 0;

	if (is_explorer()) { // grab the x-y pos.s if browser is IE
		
		if(!is_defined(event.clientX) || !is_defined(event.clientY))
			return false;
			
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
		
	} else { // grab the x-y pos.s if browser is NS
		
		if(!is_defined(e.pageX) || !is_defined(e.pageY))
			return false;
			
		tempX = e.pageX;
		tempY = e.pageY;
	} 
	
	// catch possible negative values in NS4
	if (tempX < 0) {tempX = 0}
	if (tempY < 0) {tempY = 0} 


	return [tempX,tempY];
}	


function getWindowSize() {

	var windowWidth = 0;
	var windowHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	return [windowWidth,windowHeight];
}

function getBody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function popup_translation(lang,constant,component,container,name,context,componet_directory) {

	url = "/admin/translation/update/l:" + lang + "/c:" + constant + "/t:" + component + "/o:" + container + "/n:" + name  + "/x:" + context + "/d:" + componet_directory + "/";
	popup(url, "translation", 550, 300, 1, 1);
}

function set_form_action(form_id,action) {
	get_object(form_id).getAttributeNode("action").value = action;
}

function add_load_event(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }      
    }
  }
}

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

function round(val,dec) {
	var dec = dec ? dec : 0;
	return Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
}

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.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();


