// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}

// 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;

	}
}

function popupMapWindow(params) {
 window.open("http://global.george.co.za/map/popup.php?" + params, null, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=400,left=262,top=184");
}

function popupMap(locationName) {
 popupMapWindow("type=businesses&locationName=" + locationName);
}

function popupMap2(title, location, latitude, longitude, zoom) {
 popupMapWindow("type=single&title=" + title + "&location=" + location + "&latitude=" + latitude + "&longitude=" + longitude + "&zoom=" + zoom);
}