// Gets a element by its Id. Used for shorter coding.
function GetE( elementId )
{
	return document.getElementById( elementId )  ;
}

function ShowE( element, isVisible )
{
	if ( typeof( element ) == 'string' )
		element = GetE( element ) ;
	element.style.display = isVisible ? '' : 'none' ;
}

function SetAttribute( element, attName, attValue )
{
	if ( attValue == null || attValue.length == 0 )
		element.removeAttribute( attName, 0 ) ;			// 0 : Case Insensitive
	else
		element.setAttribute( attName, attValue, 0 ) ;	// 0 : Case Insensitive
}

function GetAttribute( element, attName, valueIfNull )
{
	var oAtt = element.attributes[attName] ;

	if ( oAtt == null || !oAtt.specified )
		return valueIfNull ? valueIfNull : '' ;

	var oValue = element.getAttribute( attName, 2 ) ;

	return ( oValue == null ? valueIfNull : oValue ) ;
}

// Functions used by text fiels to accept numbers only.
function IsDigit( e )
{
	e = e || event ;
	var iCode = ( e.keyCode || e.charCode ) ;

	event.returnValue =
		(
			( iCode >= 48 && iCode <= 57 )		// Numbers
			|| (iCode >= 37 && iCode <= 40)		// Arrows
			|| iCode == 8						// Backspace
			|| iCode == 46						// Delete
		) ;

	return event.returnValue ;
}

String.prototype.trim = function()
{
	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
}

String.prototype.startsWith = function( value )
{
	return ( this.substr( 0, value.length ) == value ) ;
}

String.prototype.remove = function( start, length )
{
	var s = '' ;

	if ( start > 0 )
		s = this.substring( 0, start ) ;

	if ( start + length < this.length )
		s += this.substring( start + length , this.length ) ;

	return s ;
}

var stmpImg ;
var stmpImgPath;
function SetUrl( url, width, height, alt )
{
		stmpImg = GetE('tmpImg').value;
		stmpImgPath = GetE('basepath').value;
		//url = url.replace( stmpImgPath, '' ) ;
		var url2 = url.substr(1)
		GetE(stmpImg).value = url2;
		try {
			$.ajax({
				type: "GET",
				url: hmdBase + "/admin/index2.php",
				data: "comp=common&page=file&op=getlink&popup=xml&file=" + url2,
				success: function(result) {
					$("#"+stmpImg+"_txt").html(result);
				}
			});
			alert("File added successfully - don't forget to press 'save'");
			//return true;

		} catch(e) {
			//alert(e);
		}
}


// moves elements from one select box to another one
function hmdMoveOptions(from,to) {
  // Move them over
  for (var i=0; i<from.options.length; i++) {
	var o = from.options[i];
	if (o.selected) {
	  to.options[to.options.length] = new Option( o.text, o.value, false, false);
	}
  }
  // Delete them from original
  for (var i=(from.options.length-1); i>=0; i--) {
	var o = from.options[i];
	if (o.selected) {
	  from.options[i] = null;
	}
  }
  from.selectedIndex = -1;
  to.selectedIndex = -1;
}

function hmdAllSelected(element) {

   for (var i=0; i<element.options.length; i++) {
		var o = element.options[i];
		o.selected = true;

	}
}

/**
 * Author: Charl van Niekerk
 * Last Modified: 2008-01-10
 * Description: Shows or hides a particular piece of text and
 * changes background image of heading accordingly.
 */
function hmdShow(heading, id, openImg, closeImg) {
	var e = document.getElementById(id);
	if (e.style.display == "block") {
		e.style.display = "none";
		heading.style.backgroundImage = "url(" + openImg + ")";
	} else {
		e.style.display = "block";
		heading.style.backgroundImage = "url(" + closeImg + ")";
	}
}
/**
	Multiple checkbox select functions 
**/
function BoxStateCheck(FormName, FieldName)
{
	if (document.adminform.checkall.checked)
	{
		SetAllCheckBoxes(FormName, FieldName, true);
	}
	else
	{
		SetAllCheckBoxes(FormName, FieldName, false);
	}
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}
/**
	End Multiple checkbox select functions 
**/
$(document).ready(function(){
	$(".bookmarklink").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;
		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera && window.print) { // Opera Hotlist
			return true; 
		}
	
	});
});
