/*  SOME SCRIPTS REQUIRE THE JQUERY LIBRARY TO RUN  */

//  ---------------------------------------------------------------------------
//	initilise when document ready
//  ---------------------------------------------------------------------------

    $(document).ready(function() {
        //	center align primary links
        var PrimaryLinksWidth = 0;
        
        $("#TopBar ul.PrimaryLinks li").each(function(){ 
            PrimaryLinksWidth += $(this).width();
        });
        
//        if (PrimaryLinksWidth != 0) {
//            $("#TopBar ul.PrimaryLinks").css("margin-left", parseInt(($("#TopBar").width() - PrimaryLinksWidth) / 2) - 20 + "px");
//        }
        
        //	toggle dl - id must now be assigned to the <dd> element to be toggles and also in an attribute called "contentId" in the calling anchor
		$(".JS_ToggleDL dt a").bind("click", function () {
		    $("#" + this.getAttribute("contentId")).toggle();
		});
		
		//	internet explorer :hover psuedo class hacks
        if ($.browser.msie) {
            //  highlight the heading tabs
            $("#TopBar ul.PrimaryLinks li").hoverClass("Hover");
        }
    });



//  ---------------------------------------------------------------------------
//	generic jquery class swapper function
//  ---------------------------------------------------------------------------

    $.fn.hoverClass = function(c) {
	    return this.each(function() {
		    $(this).hover(function() {
			        $(this).addClass(c);
			    }, function() {
			        $(this).removeClass(c);
			    }
		    );
	    });
    }


    
//  ---------------------------------------------------------------------------
//	attach/detach events to element
//  ---------------------------------------------------------------------------
 
	/*  the event supplied must be supplied without the "on" prefix eg: for an "onclick" the event would be "click".  */

	function objectAttachEvent(obj, eventHandler, functionName, useCapture) {
		if (useCapture == null) {
			useCapture = false;
		}
		if (window.attachEvent) {
			obj.attachEvent("on" + eventHandler, functionName);
		} else if (window.addEventListener) {
			obj.addEventListener(eventHandler, functionName, useCapture);
		}
	}
	
	function objectDetachEvent(obj, eventHandler, functionName, useCapture) {
		if (useCapture == null) {
			useCapture = false;
		}
		if (window.detachEvent) {
			obj.detachEvent("on" + eventHandler, functionName);
		} else if (window.removeEventListener) {
			obj.removeEventListener(eventHandler, functionName, useCapture);
		}
	}
	

	
//  ---------------------------------------------------------------------------
//	checkbox select all
//  ---------------------------------------------------------------------------
    
    function selectAll(obj, name) {
        //  return error message if checkbox group name not found
        if (!name) {
            alert("No name found on select all checkbox.");
            return;
        }
        
        //  toggle check boxes with same name as group to be the checked value of the select all checkbox
        //  bind click event to turn off select all if one item in the group is marked as unselected
        $("input[@name='" + name + "']").attr("checked", obj.checked).bind('click', function() {
            if (!this.checked) {
                obj.checked = false;
            }
        });
    }
    
    function checkAll(name, isChecked) {
        $("input[@name='" + name + "']").attr("checked", isChecked);
    }
    
    
   

//  ---------------------------------------------------------------------------
//	drop down of all phones
//  ---------------------------------------------------------------------------

    function selectPhone(obj){
        if (obj.options.selectedIndex == 0 && obj.value == '') {
            alert("Please select a phone");
            return false;
        } else {
            document.location.href = obj.options[obj.selectedIndex].value;
        }
    }

//  ---------------------------------------------------------------------------
//	drop down of all phone reviews
//  ---------------------------------------------------------------------------

    function selectReview(obj){
        if (obj.options.selectedIndex == 0 && obj.value == '') {
            alert("Please select a phone review");
            return false;
        } else {
            document.location.href = obj.options[obj.selectedIndex].value;
        }
    }


//  ---------------------------------------------------------------------------
//	form field character validation
//  ---------------------------------------------------------------------------
    
    //	only numbers
    var sRegExpNumOnly = /[0-9]/;
    
    //	anything but numbers
	var sRegExpNumOnlyExept = /[^0-9]/;
							
    //	only letters
	var sRegExpChars = /[a-zA-Z '-]/;
	
	//	anything but letters
	var sRegExpCharsExcept = /[^a-zA-Z ']/;
	
	//	only numbers and letters
	var sRegExpNumChars = /[a-zA-Z0-9 ']/;
	
	//	anything but numbers and letters
	var sRegExpNumCharsExcept = /[^a-zA-Z0-9 ']/;
	
	//	only characters for email
	var sRegExpEmail = /[a-zA-Z0-9@'.,+_!#$%&*-~]/;

    function getKeyCode(e) {
		//	internet explorer
		if (window.event) {
			return e.keyCode;

		//	gecko/other
		} else {
			return e.charCode;
		}
	}

	//	for fields where only numbers are accepted
	function onlyNumbers(e) {
		//	get the key code
		var i = getKeyCode(e);
		
		//	cancel enter key mozilla
		var j = (i == 0) ? e.keyCode : 0;
		
		//	test the actual character from key code (i == 0 && j != 13) is enter/retrun key
		if (sRegExpNumOnly.test(String.fromCharCode(i)) || (i == 0 && j != 13)) {
			return true; 
		} else {
			return false;
		}
	}
	
	//	for fields where only alpha characters are accepted
	function onlyAlpha(e) {
		//	get the key code
		var i = getKeyCode(e);
				
		//	cancel enter key mozilla
		var j = (i == 0) ? e.keyCode : 0;
	
		//	test the actual character from key code (i == 0 && j != 13) is enter/retrun key
		if (sRegExpChars.test(String.fromCharCode(i)) || (i == 0 && j != 13)) {
			return true; 
		} else {
			return false;
		}
	}
	
	//	for fields where only alpha numeric characters are allowed
	function onlyAlphaNumbers(e) {
		//	get the key code
		var i = getKeyCode(e);
		
		//	cancel enter key mozilla
		var j = (i == 0) ? e.keyCode : 0;
		
		//	test the actual character from key code (i == 0 && j != 13) is enter/retrun key
		if (sRegExpNumChars.test(String.fromCharCode(i)) || (i == 0 && j != 13)) {
			return true; 
		} else {
			return false;
		}
	}
	
	//	for fields to disable the enter key
	function cancelEnterKey(e) {
		//	get the key code
		var i = getKeyCode(e);
		
		//	cancel enter key mozilla
		if (i == 0) { i = e.keyCode; }
		
		//	if keycode is equal to any of the follow return false (deny) else return true (allow): 13 = enter/return key
		if (i == 13) {
			return false; 
		} else {
			return true;
		}
	}
	
	//	for fields where only alpha numeric characters are allowed
	function onlyEmail(e, cancelCopy) {
		//	get the key code
		var i = getKeyCode(e);
		
		if (cancelCopy) {
			//	i will be different as not e.which but e.charCode
			if ((e.ctrlKey && i == 99) || (e.ctrlKey && i == 118)) {
				return false;
			}
		}
		
		//	test the actual character from key code and keycode values (0 = cursor, 32 = space, 39 = single quote)
		if (sRegExpEmail.test(String.fromCharCode(i)) || i == 0) {
			return true; 
		} else {
			return false;
		}
	}

    //  validate email address
	function validateEmail(oEmail) {
		var sRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (!oEmail.value.match(sRegExp)) {
			return (false);
		}
		return(true);
	}



//  ---------------------------------------------------------------------------
//	css display
//  ---------------------------------------------------------------------------

    /*  as some browsers use different css display values for different elements use this function to set the display  */
	
    function setDisplay(obj, isDisplay) {
		var sDisplay = "block";
		if (isDisplay == true) {
			//	table row display (show) value for non-internet explorer browsers
			if (!$.browser.msie && obj.tagName == "TR") {
				sDisplay = "table-row";
			}
			obj.style.display = sDisplay;
		} else {
			obj.style.display = "none";
		}
	}



//  ---------------------------------------------------------------------------
//	spawn new window
//  ---------------------------------------------------------------------------

	function openWindow(sURL, sName, isScroll, isResize, iWidth, iHeight) {
	
	    //	check if window is to be scrollable and resizable
		isScroll    = (isScroll || isScroll == null) ? "yes" : "no";
		isResize    = (isResize || isResize == null) ? "yes" : "no";
		
		//	check if window size has been provided, if not create
		if (!iWidth) {
			iWidth  = parseInt((iClientInnerWidth / 10) * 7);
		}
		if (!iHeight) {
			iHeight = parseInt((iClientInnerHeight / 10) * 7);
		}
		
		//	set the position of the window based on the dimentions provided
		var iTop    = parseInt((iClientInnerHeight - iHeight) / 3);
		var iLeft   = parseInt((iClientInnerWidth - iWidth) / 2);
		
		var sParams = "menubar=no, toolbar=no, location=no, directories=no, status=no, scrollbars=" + isScroll + ", resizable=" + isResize + ", dependent, top=" + iTop + ", left=" + iLeft + ", width=" + iWidth + ", height=" + iHeight;
		popupWin    = top.window.open(sURL, sName, sParams);
	}



//  ---------------------------------------------------------------------------
//	get inner dimensions of the browser window
//  ---------------------------------------------------------------------------

    var iClientInnerWidth = null;
	var iClientInnerHeight = null;

	function getClientInner() {
		if (document.documentElement.clientWidth) {
			iClientInnerWidth  = document.documentElement.clientWidth;
			iClientInnerHeight = document.documentElement.clientHeight;
		} else if (document.body.clientWidth) {
			iClientInnerWidth  = document.body.clientWidth;
			iClientInnerHeight = document.body.clientHeight;
		}
	}

	objectAttachEvent(window, "load", getClientInner);
	objectAttachEvent(window, "resize", getClientInner);


	
//  ---------------------------------------------------------------------------
//	get the event target element
//  ---------------------------------------------------------------------------

	function getEventTarget(e) {
		if ($.browser.msie) {
			return window.event.srcElement;
		} else {
			return (e.target.tagName ? e.target : e.target.parentNode);
		}
	}
	


//  ---------------------------------------------------------------------------
//	hide selects: internet explorer < version 7
//  ---------------------------------------------------------------------------

    var sUserAgent = navigator.userAgent;
    var sBrowserVersion	= null;

	function toggleSelects(obj, isHidden) {
	    //	get IE version number
	    if (sBrowserVersion == null) {
		    var sVersion = sUserAgent.indexOf("MSIE");
	        sBrowserVersion = (sVersion == -1) ? 0 : parseFloat(sUserAgent.substring(sVersion + 5, sUserAgent.indexOf(";", sVersion)));
	    }
		
		if (sBrowserVersion < 7) {
		    var aSelects = obj.getElementsByTagName("SELECT");
		    for (var i = 0; i < aSelects.length; i++) {
			    aSelects[i].style.visibility = (isHidden) ? "hidden" : "visible";
		    }
		}
	}
	


//  ---------------------------------------------------------------------------
//	offset positions
//  ---------------------------------------------------------------------------

	/*  return the left coordinate of an element relative to the page  http://www.brainjar.com/  */
	
	function getPageOffsetLeft(obj) {
		var i = obj.offsetLeft;
		if (obj.offsetParent != null) {
			i += getPageOffsetLeft(obj.offsetParent);
		}
		return i;
	}

	function getPageOffsetTop(obj) {
		var i = obj.offsetTop;
		if (obj.offsetParent != null) {
			i += getPageOffsetTop(obj.offsetParent);
		}
		return i;
	}
	

	
/*	--[	ajax support ]-------------------------------------------------------  */
	
	/*  send http request  */
    function SendHttpRequest(url, postData, processResponse) {
        //  create an httprequest object
        var myRequest = new HttpRequest();
        var sResponse; 
        
        //  assign callback functions, url and set request headers
        myRequest.successCallback = processResponse;
        myRequest.failureCallback = showError;
        myRequest.url = url;
        myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        myRequest.post(postData);
    }
    
/*	--[	Show/Hide object ]-------------------------------------------------------  */

function ReverseContentDisplay(obj) {
    if(obj.length < 1) { return; }
    if(document.getElementById(obj).style.display == "none") { document.getElementById(obj).style.display = "block"; }
    else { document.getElementById(obj).style.display = "none"; }
}