

//Sets the country data in env var countryInfo
var countryInfo = {"country": [{"CountryCode": "","CountryName": "","CountryFlag": "","CountryPhone": "&nbsp;", "CountryLanguages": ""}]};
var url = '/site.countryLookup.';
url += getUserCountryCode() + ".js";
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.getElementsByTagName("head").item(0).appendChild(script);


//used by Rich Text Editor
function CFC_popup(url, name, features){
    var defaultWidth = 500;
    var defaultHeight = 400;
    name = name.replace(" ", "");

    if (url.indexOf("/") == 0) url = "" + url;
    if (features.indexOf("width") == -1) features += ",width=" + defaultWidth;
    if (features.indexOf("height") == -1) features += ",height=" + defaultHeight;
    var w = window.open(url, name, features);
    w.focus();
}

function getIEVersion() {
	var dataString = navigator.userAgent;
	var index = dataString.indexOf("MSIE");
	if (index == -1) return;
	return parseFloat(dataString.substring(index + 5));
}	

function nextEmail(email, confirmation, currentHandle) {
	//   Reset the width
	$("#cluetip").width("258px");
	$("#ttEmailFormButton").width("125px");

	//   Load the content
	$("#cluetip-inner").load(email, function() {
		//   Set event handlers for the new content
		$("a.cluetip-close").click(function() {
			//   Close the popup
			$("#cluetip-inner").empty();
			$("#cluetip").hide();
			return false;
		});
				
		$("#ttEmailFormButton").bind("click", function() {
			var errorMsgTo = "";
			var errorMsgFrom = "";
			var bErrors = false;
			var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var to = document.getElementById("ttEmailFormTo");
			var from = document.getElementById("ttEmailFormFrom");
			var subject = document.getElementById("ttEmailFormSubject");
			var message = document.getElementById("ttEmailFormMessage");
			var bSendMsg = false;
			
			if (to.value == ""){
				errorMsgTo = "Please Provide Collegues Email <br />";
				bErrors = true;
			} else {
				if(emailRegEx.test(to.value) != true){
					bErrors = true;
					errorMsgTo = "Your collegues email address is not valid <br />";
				}
			} 
			
			if (from.value == ""){
				errorMsgFrom = "Please Provide Your Email <br />";
				bErrors = true;
			} else {
				if(emailRegEx.test(from.value) != true){					
					bErrors = true;
					errorMsgFrom = "Your email address is not valid";
				}
			}
			
			if (bErrors == true){	
				var errMsg = document.getElementById("errormsg");
				errMsg.innerHTML = "The following errors occured <br />" + errorMsgTo + errorMsgFrom;
			} else {
				var url = document.getElementById("ttEmailFormURL");
				url = url.value;
				bSendMsg = SendMail(url, to.value, from.value,  encodeURIComponent(subject.value), encodeURIComponent(message.value), encodeURIComponent(currentHandle));
				
								
				if(bSendMsg == true){	
					$("#cluetip-inner").load(confirmation, function() {
						//   Set event handlers for the new content
						$("a.cluetip-close").click(function() {
							//   Close the popup
							$("#cluetip-inner").empty();
							$("#cluetip").hide();
							return false;
						});
					});
					
				}
			}
		return false;
		});
	});
	//   Reset the position
	var newLeft = parseInt($("#cluetip").css("left")) - 153;
	if(document.getElementById("cluetip-head") == null) {
		$("#cluetip").css("left", newLeft + "px");
	}
	return false;
}

function nextTip(height, width, url) {
	//   Reset the width
	$("#cluetip").width(width + "px");
	$("#cluetip").height(height + "px");
	
	//   Reset the position
	var newLeft = parseInt($("#cluetip").css("left"))-(width-100);
	
	$("#cluetip").css("left", newLeft + "px");
	
	//   Load the content
	$("#cluetip-inner").load(url, function() {
	
		//   Set event handlers for the new content
		$("a.cluetip-close").click(function() {
			//   Close the popup
			$("#cluetip-inner").empty();
			$("#cluetip").hide();
			return false;
		});
	});
	return false;
}


function SendMail(url, to, from, subject, message, currentHandle) {
	var xmlHttp;
	var bStatus = true;
		
	if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    	xmlHttp = new XMLHttpRequest();
    }
	
	xmlHttp.onreadystatechange = processRequest; 
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send("to=" + to + "&from=" + from + "&subject=" + subject + "&message=" + message + "&currentHandle=" + currentHandle);		
	
    function processRequest() {
    //alert("ready state: " + xmlHttp.readyState + " status: " + xmlHttp.status)
      if (!xmlHttp.readyState == 4) {
           return bStatus = false;
        if (!xmlHttp.status == 200) {
           return bStatus = false;
        } 
      } 
    }
    
    return bStatus;
}

function getCountryIcon() {
	return "/shared-static/images/icons/flags/" + countryInfo.country[0].CountryFlag;
}

function getCountryHeaderName() {
	return countryInfo.country[0].CountryName;
}

function getCountryPhone() {
	return countryInfo.country[0].CountryPhone;
}

function getCountryLanguages() {
	return countryInfo.country[0].CountryLanguages;
}

// findValue, selectItem and formatItem all used in conjuction with autocomplete
function findValue(li) {
	if( li == null ) return alert("No match!");
	if( !!li.extra ) var sValue = li.extra[0];
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}

$(document).ready(function(){
	//   Slide the login box up and down
	$("a#slidein").click(function(){ $("div#loginWrap").find("div#loginBox:hidden").slideDown("slow"); $("div#loginExpand").hide(); $("div#loginContract").show(); });
	$("a#slideout").click(function(){ $("div#loginWrap").find("div#loginBox:visible").slideUp("slow"); $("div#loginContract").hide(); $("div#loginExpand").show(); });
	$("div#loginContract").hide();
	$("div#loginBox").hide();
	
	//   Add border to text and password input elements
	$('input[@type=password]').add('input[@type=text]').css('border', '1px solid #CCCCCC');

	//   Focus input
	$('#contentQuickForm input').focus(function(){$(this).addClass('focused')}).blur(function(){$(this).removeClass('focused')});
 	
	/* fixes for if there is no js to hide unneeded things in the left col with css */
	/* Show the things we hid with CSS */
	$("div#loginExpand").show();
	$("span.selected ~ ul").show();

	

	//   Email tooltip link
	$('a#ttEmailLink').cluetip({
		local: true,
		width: '105px',
		delay: 250
	});

	//   Print tooltip link
	$('a#ttPrintLink').cluetip({
		local: true,
		width: '105px',
		delay: 250
	});
	
	$('a.load-local').cluetip({
		local: true,
		width: '105px',
		delay: 250
	});	
	
	// begin fix for cluetips

	if($.browser.msie) {
            $("#ttEmailLink").mouseover(function() {
                $("div#topNav ul.nav").css({position: "absolute"});
                return false;
            });
            $("#ttEmailLink").mouseout(function() {
                $("div#topNav ul.nav").css({position: "static"});
                return false;
            });

            $("#ttPrintLink").mouseover(function() {
                $("div#topNav ul.nav").css({position: "absolute"});
                return false;
            });
            $("#ttPrintLink").mouseout(function() {
                $("div#topNav ul.nav").css({position: "static"});
                return false;
            });
	    }

	$("#cluetip").bgIframe();
  
});
