// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : G-Forces Web Management Ltd Main site functions
// Author : Developers / Developers@gforces.co.uk / G-Forces Web Management Ltd
//
// Description : Core functionality for standard sites developed by G-Forces Web Management Ltd
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function theflash(movie, width, height) {
	document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '">');
    document.writeln('<param name="movie" value="' + movie + '">');
	document.writeln('<param name="quality" value="high" />');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="wmode" value="transparent">');
	document.writeln('<embed src="' + movie + '" width="' + width + '" height="' + height + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" wmode="transparent"></embed>');
    document.writeln('</object>');
}

//Set Cookie Function
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

function postcodeMap(postcode) {
	if (navigator.javaEnabled() == true) {
		return 'http://link2.map24.com/?lid=03763162&amp;maptype=JAVA&amp;width0=1500&amp;zip0=' + postcode.replace(' ', '+') + '&amp;country0=GB';
	} else {
		return 'http://www.multimap.com/map/browse.cgi?client=public&amp;db=pc&amp;cidr_client=none&amp;pc=' + postcode.replace(' ', '+');
	}
}

function open_window(fileName, windowName, windowWidth, windowHeight, scrollbars, resizable) {
	window.open(fileName, windowName, 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=' + scrollbars + ', resizable=' + resizable + ', left=' + centre(windowWidth, screen.availWidth) + ', top=' + centre(windowHeight, screen.availHeight) + ', width=' + windowWidth + ', height=' + windowHeight);
}

function centre(size,area) {
	return (area/2)-(size/2);
}

function fancyRules() {
	if (!document.getElementsByTagName) return; 
	var hr = document.getElementsByTagName("hr");
	for (var i = 0; i < hr.length; i++) { 
		var newhr = hr[i]; 
		var wrapdiv = document.createElement('div');
		wrapdiv.className = 'line';  
		newhr.parentNode.replaceChild(wrapdiv, newhr);  
		wrapdiv.appendChild(newhr);  
	}
}

function getFirstChild(f) {
	f = f.firstChild;
	while (f && !f.tagName) {
		f = f.nextSibling;
	}
	return f;
}

function getNextSibling(ns) {
	ns = ns.nextSibling;
	while (ns && !ns.tagName) {
		ns = ns.nextSibling;
	}
	return ns;
}

function changeState(theLi) {
	var dealerTabs = document.getElementById('dealerLocatorNav');
	var arLiList = dealerTabs.getElementsByTagName('li');

	for (i=0; i < arLiList.length; i++) { // cloase all existing tabs
		getNextSibling(getFirstChild(arLiList[i])).style.display = 'none';
		(arLiList[i]).className = 'closed';
	}
	getNextSibling(getFirstChild(theLi)).style.display = 'block';
	theLi.className = 'open';
}

////////////////////////// Admin Functions ///////////////////////////////
//DO NOT EDIT INLESS A SENIOR DEVELOPER GIVES YOU PERMISSION BELOW THIS LINE


var whichList;

function changeHidden(theElement, theHidden) {
	//whichList = theElement;
	theHidden.value += theElement.options[theElement.selectedIndex].value.substring(0, theElement.options[theElement.selectedIndex].value.indexOf('|'));
}

function colourBars() {
	var even = false;
	var evenColor = "#FFFFFF";
	var oddColor = "#ECECEC";
	var thearea = document.getElementById('1table');
	var trs = thearea.getElementsByTagName('tr');
	for (var i = 0; i < trs.length; i++) {
		trs[i].style.backgroundColor = even ? evenColor : oddColor;
		even =  ! even;
	}
}

function checkLogout(theUrl) {
	if (confirm('Are you sure you want to logout?') == true) {
		window.location.href = theUrl + '?logout=true';
	}
}

function checkALL(checkbox, theElement) {
	for (i = 0; i < document.getElementsByName('' + theElement + '').length; i++) {
		document.getElementsByName('' + theElement + '')[i].checked = checkbox.checked;
	}
}

function checkCancel(theLink) {
	if (confirm('Are you sure you want to cancel these changes?') == true) {
		window.location.href = theLink;
	}
}

function previewEditor(theForm, theUrl) {
	var theAction = theForm.action;
	theForm.action = theUrl;
	theForm.target = 'previewWindow';
	theForm.submit();
	theForm.action = theAction;
	theForm.target = '_top';
}


function submitOnEnter(theEvent, theAction) {
	theEvent = (theEvent) ? theEvent : event;
	var charCode = (theEvent.charCode) ? theEvent.charCode :
		((theEvent.which) ? theEvent.which : theEvent.keyCode);
	if (charCode == 13 || charCode == 3) {
		document.getElementById(theAction).click();
	}
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function