//var ajax = new sack();
/*var browserName = navigator.appName;
var browserVer = parseInt(navigator.appVersion);
var version = "";
var msie4 = (browserName == "Microsoft Internet Explorer" && browserVer >= 4);
if ((browserName == "Netscape" && browserVer >= 3) || msie4 || browserName=="Konqueror" || browserName=="Opera") {
	version = "n3";
} else {
	version = "n2";
}
// Blurring links:
function blurLink(theObject)	{	//
	if (msie4)	{theObject.blur();}
}*/

// toggle visibility code

function toggle(obj) {
 var el = document.getElementById(obj);
 el.style.display = (el.style.display != 'none' ? 'none' : '');
}

function refer() {
	var TheUrl = siteurl;
	new_win = window.open(TheUrl + 'pop_refer.cfm',"new_win",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=450,height=500');
}	

// Contact Us Form Validation
	function contactsubmit()
	
	{
		if (FRMSubmit.firstname.value == '')
		{
			alert("Please enter your first name");
			FRMSubmit.firstname.focus();
			return false;
		}
		if (FRMSubmit.surname.value == '')
		{
			alert("Please enter your surname");
			FRMSubmit.surname.focus();
			return false;
		}		
		if (FRMSubmit.contact_Email.value == '')
		{
			alert("Please enter your email address");
			FRMSubmit.contact_Email.focus();
			return false;
		}			
		if (FRMSubmit.contact_subject.value == '')
		{
			alert("Please enter a subject for the enquiry");
			FRMSubmit.contact_subject.focus();
			return false;
		}
		if (FRMSubmit.contact_comments.value == '')
		{
			alert("Please enter the enquiry.");
			FRMSubmit.contact_comments.focus();
			return false;
		}
		
	return true;
	}

// search form validation
function searchsubmit() {
	if (FRMsearch.keyword.value == '')
		{
			alert("Please enter a keyword to search for");
			FRMsearch.keyword.focus();
			return false;
		}
}

// Extranet Login Form Validation
	function extranetloginsubmit()
	
	{
		if (FRMLogin.user_email.value == '')
		{
			alert("Please enter your username");
			FRMLogin.user_email.focus();
			return false;
		}
		if (FRMLogin.user_password.value == '')
		{
			alert("Please enter your password");
			FRMLogin.user_password.focus();
			return false;
		}
		
	return true;
	}

	function leftloginsubmit()
	
	{
		if (FRMLeftLogin.user_email.value == '')
		{
			alert("Please enter your username");
			FRMLeftLogin.user_email.focus();
			return false;
		}
		if (FRMLeftLogin.user_password.value == '')
		{
			alert("Please enter your password");
			FRMLeftLogin.user_password.focus();
			return false;
		}
		
	return true;
	}

// Email Password Form Validation
	
	function passreqsubmit()
	
	{
		if (FRMPasswordReq.email.value == '')
		{
			alert("Please enter your email address");
			FRMPasswordReq.email.focus();
			return false;
		}
		
	return true;
	}	
	
// Extranet Registration Form Validation
	function extranetregistrationsubmit()
	{	
			
		if (FRMRegister.Fname.value == '')
		{
			alert("Please enter your first name");
			FRMRegister.Fname.focus();
			return false;
		}
		if (FRMRegister.Sname.value == '')
		{
			alert("Please enter your surname");
			FRMRegister.Sname.focus();
			return false;
		}
		/*
		if (FRMRegister.occupation.value = '')
		{
			alert("Please enter your occupation");
			FRMRegister.occupation.focus();
			return false;
		}		
		*/
		//basic email address syntax validation
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(FRMRegister.email.value) == false) {
			alert("Invalid E-mail Address! Please re-enter.");
			return false;
		}
		
		/*
		if (FRMRegister.companyname.value == '')
		{
			alert("Please enter your companyname name");
			FRMRegister.companyname.focus();
			return false;
		}
		*/	
		if (FRMRegister.address1.value == '')
		{
			alert("Please enter your address");
			FRMRegister.address1.focus();
			return false;
		}			
		if (FRMRegister.city.value == '')
		{
			alert("Please enter your city");
			FRMRegister.city.focus();
			return false;
		}					
				
		if (FRMRegister.user_password.value == '')
		{
			alert("Please enter your password")
			FRMRegister.user_password.focus();
			return false;
		}
		
		if (FRMRegister.user_password.value.length < 3) {
			alert("Please enter a password that is more than 3 characters long.")
			FRMRegister.user_password.focus();
			return false;
		}
		if (FRMRegister.user_password_confirm.value == '')
		{
			alert("Please enter your password confirmation");
			FRMRegister.user_password_confirm.focus();
			return false;
		}
		if (FRMRegister.user_password.value != FRMRegister.user_password_confirm.value  ) {
			alert("Your password and password confirmation do not match.\nPlease make sure your password and password confirmation are the same");
			FRMRegister.user_password_confirm.focus();
			return false;
		}
		/*
		if (FRMRegister.disclaimer.checked == false) {
			alert("You must accpet the site Terms & Conditions to proceed.");
			FRMRegister.disclaimer.focus();
			return false;
		}
		*/		
		
	return true;
	}

// Extranet Registration Form Validation
	function extranetregistrationupdate()
	{	
			
		if (FRMRegister.Fname.value == '')
		{
			alert("Please enter your first name");
			FRMRegister.Fname.focus();
			return false;
		}
		if (FRMRegister.Sname.value == '')
		{
			alert("Please enter your surname");
			FRMRegister.Sname.focus();
			return false;
		}
			
		
		//basic email address syntax validation
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(FRMRegister.email.value) == false) {
			alert("Invalid E-mail Address! Please re-enter.");
			return false;
		}
		
		
		if (FRMRegister.address1.value == '')
		{
			alert("Please enter your address");
			FRMRegister.address1.focus();
			return false;
		}			
		if (FRMRegister.city.value == '')
		{
			alert("Please enter your city");
			FRMRegister.city.focus();
			return false;
		}					
				
		if (FRMRegister.user_password.value == '')
		{
			alert("Please enter your password")
			FRMRegister.user_password.focus();
			return false;
		}
		
		if (FRMRegister.user_password.value.length < 3) {
			alert("Please enter a password that is more than 3 characters long.")
			FRMRegister.user_password.focus();
			return false;
		}
		if (FRMRegister.disclaimer.checked == false) {
			alert("You must accpet the site Terms & Conditions to proceed.");
			FRMRegister.disclaimer.focus();
			return false;
		}		
		
	return true;
	}	
	
//Standard Functions
function open_new(url) {
	new_win = window.open(url,"new_win",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
	}			
function open_email(url) {
	new_email = window.open(url,"new_email",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0, width=450, height=500');
	}
function open_print(url) {
	new_print = window.open(url,"new_print",'toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0, width=630, height=600');
	}	
function open_subscription(url) {
	new_sub = window.open(url,"new_sub",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=0,width=450,height=500');
	}	
	
// navigation
function sendToPage(toLocation) {
	if (toLocation != "")
		top.location.href=toLocation;
	}

// open view photo window

function viewphoto (photoid,winwidth,winheight,viewthumb) {
	var TheUrl = siteurl;
	var newwinwidth = winwidth + 30;
	var newwinheight = winheight + 30;
	//alert (viewthumb);
	if (viewthumb == 1) {
		window.open(TheUrl + 'pop_viewphoto.cfm?photoid=' + photoid+'&viewthumb=yes', "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
	else {
		window.open(TheUrl + 'pop_viewphoto.cfm?photoid=' + photoid, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
}

// open view photo window

function viewslideshow (useURL,photoid,winwidth,winheight,categoryid,viewthumb) {
	
	var TheUrl = useURL;
	var newwinwidth = winwidth + 50;
	var newwinheight = winheight + 50;
	//alert (TheUrl);
	if (viewthumb == 1) {
		window.open(TheUrl + 'pop_viewslideshow.cfm?photoid=' + photoid+'&categoryid='+categoryid+'&viewthumb=yes', "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
	else {
		window.open(TheUrl + 'pop_viewslideshow.cfm?photoid=' + photoid+'&categoryid='+categoryid, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
}

//existing site functions
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// Global variables

var cookieQueries = new Array();
var isKonqueror = (navigator.vendor == 'KDE')
var isIE6 = ((window.ActiveXObject && !window.XMLHttpRequest) ? true : false);
var adHiders = [false, false, false]; // First element: Search suggestions, second: Browse categories menu, third: Community menu
var _nameidx, _lastbold; // Variables used to make new torrents on the today page bold


// Calculate real offset values of elements

function realOffset(el, offsetType) {

 var offset = 0;

 while(el) {
  offset += el[offsetType]; 
  el = el.offsetParent;
 }

 return offset;

}


// Array.indexOf fix

if(!Array.indexOf) {
 Array.prototype.indexOf = function(elValue) {

  for(var i=0; i<this.length; i++) {
   if(this[i] === elValue)
    return i;
  }

  return -1;

 };
}

// XMLHttpRequest fix for Internet Explorer

if(!window.XMLHttpRequest) {
 window.XMLHttpRequest = function() {
  var types = [
   'Microsoft.XMLHTTP',
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP'
  ];

  for(var i = 0; i < types.length; i++) {
   try {
    return new ActiveXObject(types[i]);
   } catch(e) {}
  }

  return false; // XMLHttpRequest not supported
 };
}

function googlemapsnewwin(URL,addressid) {
	var newwinwidth = 620;
	var newwinheight = 600; 
	window.open(URL+"inc_googlemaps.cfm?addressid="+addressid, "sitelocation", "width="+newwinwidth+",height="+newwinheight);
}	
function printtabs(URL,pageid) {
	var newwinwidth = 920;
	var newwinheight = 1000; 
	window.open(URL+"inc_print_tabs.cfm?pageid="+pageid, "Print Page", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
}	

// get distributors

function populatedistributors(region,viewtype) {
	//document.getElementById('distributorlist-divider').className = '';
	ajax.requestFile = '/include/inc_distributors_region_return.cfm?region='+region+'&ViewProduct='+viewtype;	// Specifying which file to get
	ajax.onCompletion = populatedistributorhtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatedistributorhtml () {
	var obj = document.getElementById('distributorOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}


function populatedistributordetails(distributorid) {
	ajax.requestFile = '/include/inc_distributor_return.cfm?distributorid='+distributorid;	// Specifying which file to get
	ajax.onCompletion = populatedistributordetailshtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatedistributordetailshtml () {
	document.getElementById('distributorlist-divider').className = 'distributorlist-divider';
	var obj2 = document.getElementById('distributorDetailsOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

// regional links

function populatelinks(region) {
	ajax.requestFile = '/include/inc_links_region_return.cfm?region='+region;	// Specifying which file to get
	ajax.onCompletion = populatelinkhtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatelinkhtml () {
	var obj = document.getElementById('linkOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}


function ChangeCurrentRegion(region,viewtype) {
	//document.getElementById('branchtable').innerHTML = '<img src="/images/LoadingGraphic.gif" alt="" border="0">';
	document.getElementById('distributorOutput').innerHTML = '<div align="center"><img src="/images/LoadingGraphic.gif" alt="" border="0" align="middle"></div>';
	document.getElementById('CurrentdistributorRegion').innerHTML = '<h2 class="caps">Distributors in ' + region + '</h2>';
	
	populatedistributors(region,viewtype);
	
}

function ActivateBranchName () {
	document.getElementById('branchtable-inital').style.visibility='hidden';
	document.getElementById('branchtable').style.visibility='visible';
}

function ChangeCurrentLinkRegion(region) {
	document.getElementById('CurrentLinkRegion').innerHTML = '<h2 class="small">' + region + ' Links</h2>';
	//document.getElementById('LinkDetailsOutput').innerHTML = '';
	populatelinks(region);
	
}

function ActivateBranchName () {
	document.getElementById('branchtable-inital').style.visibility='hidden';
	document.getElementById('branchtable').style.visibility='visible';
}



$(function()
{
	$("a.showvideo").click(function(e)
	{
		e.preventDefault();
		$thevideofile = $(this).attr("rel");
		/*$message = "<div id='videodiv'><p>Please enter the password to view this video</p>" +
					"<input type='text' id='videopassword'>" +
					"<input type='hidden' id='thevideofile' value='"+$thevideofile+"'>" +
					"<input type='button' value='Go' id='videopasswordgo'>"+
					"<input type='button' value='Cancel' id='canceloverlay'></div>";
					
		$.blockUI({
			message:$message,
			css:{
				"padding":"10px",
				width:"400px",
				left:"50%",
				"margin-left":"-200px",
				top:"100px",
				cursor:"default"
			},
			overlayCSS:{
				cursor:"default"
			}
		});*/
		$message = $("<div id='videodiv'>").load("/_showvideo.cfm",{thevideofile:$thevideofile});
		$.blockUI({
			message:$message,
			focusInput:false,
			css:{
				"padding":"10px",
				width:"480px",
				left:"50%",
				"margin-left":"-240px",
				top:"100px",
				cursor:"default"
			},
			overlayCSS:{
				cursor:"default"
			}
		});
		
	});
	
	$("#videopasswordgo").livequery("click", function(e){
		e.preventDefault();
		$password = $("#videopassword").val();
		$thevideofile = $("#thevideofile").val();
		$.post("/_checkvideopassword.cfm",{password:$password},
			function(data)
			{
				if(data.allow == 1)
				{
					$vid = $("#videodiv");
					$vid.load("/_showvideo.cfm",{thevideofile:$thevideofile});
					$.blockUI({
						message:$vid,
						focusInput:false,
						css:{
							"padding":"10px",
							width:"480px",
							left:"50%",
							"margin-left":"-240px",
							top:"100px",
							cursor:"default"
						},
						overlayCSS:{
							cursor:"default"
						}
					});
				}
				else
				{
					alert("That password is wrong");
				}
			},"json"
		);
	});
	
	$("#canceloverlay").livequery("click", function(e){
		e.preventDefault();
		$.unblockUI();
	});
	
	$("a[href$=jpg]").lightBox();
	
	$(".distributorregion").click(
		function(e)
		{
			e.preventDefault();
			
			var region = $(this).attr("region");
			var viewtype = $(this).attr("viewtype");
			
			//hide intial tables	
			$("#branchtable-inital").hide();
			$("#branchtable").show();
			
			
			$("#distributorOutput").html('<div align="center"><img src="/images/LoadingGraphic.gif" alt="" border="0" align="middle"></div>').show();
			$("#CurrentdistributorRegion").html('<h2 class="caps">Distributors in ' + region + '</h2>');
			
			//populatedistributors(region,viewtype);
			$.post("/include/inc_distributors_region_return.cfm",
					{
						"region":region,
						"ViewProduct":viewtype
					},
					function(data)
					{
						$('#distributorOutput').html(data);
					});
		}
	);
});




/*******************************
***** CHECK EMAIL VIA AJAX *****
*******************************/

$(function()
{
	var checkemailtimer;
	$(".checkemail").keyup(function(e)
	{
		$theemail = $(this).val();
		$thisparent = $(this).parent();
		
		$("div#prompt",$($thisparent)).remove();
		
		if(checkemailtimer)
		{
			clearTimeout(checkemailtimer);
		}
		checkemailtimer = setTimeout(function()
		{
			if($.trim($theemail) != "")
			{
				if($("#customeremail").size() > 0)
				{
					if($.trim($theemail) != $.trim($("#customeremail").val()))
					{
						$.post(
							"/_checkemail.cfm",
							{
								emailaddress:$theemail,
								returnpath:$("#returnpath").val()
							},
							function(data)
							{
								$thisparent.append(data);
							}
						);
					}
				}
				else
				{
					$.post(
						"/_checkemail.cfm",
						{
							emailaddress:$theemail,
							returnpath:$("#returnpath").val()
						},
						function(data)
						{
							$thisparent.append(data);
						}
					);
				}
				
			}
		},500);
	});
	
	$(".autopasswordrequest").livequery("click",function(e)
	{
		e.preventDefault();
		$.post(
			"/_autopasswordrequest.cfm",
			{
				autopasswordrequest:$(this).attr("id"),
				returnpath:$("#returnpath").val()
			},
			function(data)
			{
				$("div#prompt").removeClass("notice").addClass("success").html(data);
			}
		);
	});
	
	$(".closeprompt").livequery("click",function(e)
	{
		e.preventDefault();
		$("div#prompt").remove();
	});
});


