
var onload_events = new Array();

function SID()
{
	return document.getElementById("system").elements['SID'].value;
}

function dom_id(elmnt)
{
	return document.getElementById(elmnt);
}

function dom_by_tag(tag, obj)
{
	if (! obj) {
		obj = document;
	}
	return obj.getElementsByTagName(tag);
}

function add_to_onload(event)
{
	onload_events.push(event);
	window.onload = function() { 
		for (var x = 0; x < onload_events.length; x ++) {
			eval(onload_events[x]);
		}
	}
}

// returns uppercase
function ucfirst(w)
{
	w = String(w);
	return w.substring(0, 1).toUpperCase() + w.substring(1).toLowerCase();
}

// returns key of required value
function array_pos(array, val)
{
	for (var i = 0; i < array.length; i ++) {
		if (array[i] == val) {
			return i;
		}
	}
	return -1;
}

// return which key was pressed
function which_key(e)
{
	return (e.keyCode) ? e.keyCode : (e.which) ? e.which : e.charCode;
}

function validkey(type, e)
{
	var kp = which_key(e);
	switch (type) {
		case "num" : case "number" :
			return ((kp >= 96 && kp <= 105) || (kp >= 48 && kp <= 57) || kp == 8 || kp == 46);
		break;
	}
	return true;
}

// returns the elements position on the document
function get_position(obj, type)
{
	var retval = 0;

	if (type == "Left" && obj.x) {
		return obj.x;
	}
	else if (obj.y) {
		return obj.y;
	}
	else {
		while (obj.offsetParent) {
			if (obj && obj.style && obj.style.position) obj.style.position = "relative";
			retval += eval("obj.offset" + type);
			obj = obj.offsetParent;
		}
		return retval;
	}
}

function max_length(textarea, maxlength, e)
{
	var key = which_key(e);
	var d = dom_id("div_" + textarea.name);
	if (! d) {
		d = document.createElement("div");
		d.id = "div_" + textarea.name;
		textarea.parentNode.appendChild(d);
	}
	var remaining = (maxlength - textarea.value.length - 1);
	if (remaining < 0) {
		textarea.value = textarea.value.substring(0, maxlength);
		d.innerHTML = "No characters remaining";
		// allow delete
		return (key) ? false : true;
	}
	else {
		d.innerHTML = remaining + " characters remaining";
		return true;
	}
}

function stop_event(e)
{
	if (! e) {
		e = window.event;
	}

	if (e.stopPropagation) {
		e.stopPropagation();
	}
	else {
		e.cancelBubble = true;
	}
}

function show_props(obj)
{
	for (var prop in obj) {
		alert(prop + " = " + eval("obj." + prop));
	}
}

function add_popups()
{
	var lnks = document.getElementsByTagName("A");
	for (var x = 0; x < lnks.length; x ++) {
		var rel = lnks[x].getAttribute("rel");
		if (rel && rel.search(/^popup/) != -1) {
			lnks[x].onclick = function() {
				var pstr = this.getAttribute("rel").replace(/^popup_?/, "");
				window.open(this.href, "", pstr);
				return false;
			}
		}
	}
}

function unhide_elements()
{
	var e = dom_by_tag("*");
	for (var x = 0; x < e.length; x ++) {
		if (e[x].className.search(/js_hidden/) != -1) {
			e[x].style.display = 'block';
		}
	}
}

function processKeyPress(e){
	var e = window.event  || e;
	
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
	
}


 function getScrollTop() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return scrOfY;
	}
var siteBaseUrl = '/';

function popupKeyListener() {document.onkeypress = processKeyPress; }

function closePopup() {
	e = document.getElementById('map-display');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function getWindowDimensions(){
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}
/**
 * Google maps function
 * @param {Object} id
 * @param {Object} dealershipId
 * @param {Object} addressType
 */
function attachOnClickMap(id, dealershipId, addressType){
	element = document.getElementById(id);
	element.style.cursor = 'pointer';
	var divWidth = 600;
	var divHeight = 450;		
	element.onclick = function (){
		//check if this element exists.
		e = document.getElementById('map-display');
		if (!e){
			myDimensions = getWindowDimensions();
			myWidth = myDimensions[0];
			
			popupKeyListener();
			
			var myDiv = document.createElement("DIV");
			myDiv.id = "map-display";
			myIframe = document.createElement('iframe');
			myIframe.src = '/map.php?business='+dealershipId+'&type='+addressType;
			
			myIframe.style.zIndex = '1';
			myIframe.scrolling = 'no';
			myIframe.width = divWidth;
			myIframe.height = divHeight+40;					
			myIframe.frameBorder = "0";
			
			if (document.all && !window.opera) {
				myIframe.onreadystatechange  = function(){
					if (this.readyState == 'interactive') {
						var e= document.getElementById('closeButton');
					e.innerHTML = 'close';
					e.className = 'close';	
					}
				};
			} else {
					myIframe.onload = function(){
					var e= document.getElementById('closeButton');
					//e.innerHTML = 'close';
					e.className = 'close';
				}	
			}
			
			//Append
			var closeB = document.createElement('A');
			closeB.className = 'close';
			closeB.id = 'closeButton';
			//closeB.innerHTML = 'loading map';
			closeB.onclick = function(){
				closePopup();
			}
			myDiv.appendChild(closeB);
			myDiv.appendChild(myIframe);
			myDiv.style.position = 'absolute';
			myDiv.style.width = divWidth+'px';
			myDiv.style.top = (getScrollTop() + 50)+'px';
			myDiv.style.left = (myWidth / 2) - (divWidth / 2) + "px";
			myDiv.style.backgroundColor = '#fff';
			document.body.appendChild(myDiv);
			
		}
		
		return false;
	}
}

function Sprite(obj,pos) {
	obj.style.backgroundPosition = pos;
}


function HighlightSmartBuys(idn, colour, position) {
	document.getElementById('smartbuys_model_'+idn).style.background="url('/includes/css/images/trivett/home/"+colour+"')";
	Sprite(document.getElementById('smartbuys_moreinfo_'+idn),position)
}

var scrollingInterval = null;
function Scroll(obj,increment){
	if (document.getElementById(obj).style.top=='') {
		document.getElementById(obj).style.top = '0';
	}
	var newpos = parseInt(document.getElementById(obj).style.top) + increment;
	if (newpos>0) {
		StopScroll();
		return;
	}
	var realheight = parseInt(document.getElementById(obj).offsetHeight);
	var visibleheight = parseInt(document.getElementById(obj).parentNode.offsetHeight);
	if ((visibleheight-realheight)>=newpos) {
		StopScroll();
		return;
	}
	document.getElementById(obj).style.top = (newpos) + 'px';
}

function ActivateScroll(obj,increment,speed){
	scrollingInterval = setInterval("Scroll('"+obj+"',"+increment+")", speed);
}

function StopScroll(){
	clearInterval(scrollingInterval);
}


add_to_onload("add_popups()");
add_to_onload("unhide_elements()");