// JavaScript Document
$(document).ready(function()
 {
  $('.fontreplace').each(function(i){var strHTML=this.innerHTML;this.innerHTML="";this.innerHTML="<img src=" + curdir() + "_images/_titles/"+fnString2File(strHTML)+".jpg />";$(this).removeClass("fontreplace");});
  $("form#entryforma input#strEmailConf").attr("equalto","#strEmailAddress");
  $("form#entryforma").validate({
	errorPlacement: function(error, element) {
		error.appendTo( element.parent("div") );
		$('.validationinfo').show();
	}
  });
  $("form#entryformc").validate({
	errorPlacement: function(error, element) {
		error.appendTo( element.parent("div") );
		$('.validationinfo').show();
	}
  });  
  $("form#entryformb").validate({
	rules: {
		cv2: {
			minlength: 3
		},
		dblAlcoholicStrength: {
			range: [0.01,100]
		},
		boolMarketDomestic: {
			required: function(element) { return !($('#boolMarketDomestic').attr("checked") || $('#boolMarketDutyfree').attr("checked") || $('#boolMarketOfftrade').attr("checked") || $('#boolMarketOntrade').attr("checked")); }
		},
		boolRegionEurope: {
			required: function(element) { return !($('#boolRegionEurope').attr("checked") || $('#boolRegionAfrica').attr("checked") || $('#boolRegionAustralasia').attr("checked") || $('#boolRegionAsia').attr("checked") || $('#boolRegionNAmerica').attr("checked") || $('#boolRegionSAmerica').attr("checked")); }
		},
		boolDesignPackageAwards: {
      		required: function(element) {
				if ($("#strDesignPackageAwardsStatement").val() != "") {
					return true;
				} else if ($("#strDPAwardsCategory").val() == "" || $("#strDPAwardsCategory_fake").val() == "Please Select") {
					return false;
				} else {
					return true;
				}
			}
    	},
		strDPAwardsCategory: {
			required: function(element) {
				if ($("#strDesignPackageAwardsStatement").val() != "") {
					return true;
				} else if ($("#boolDesignPackageAwards").attr("checked")) {
					return true;
				} else {
					return false;
				}
			}
		},
		strDesignPackageAwardsStatement: {
			required: function(element) {
				if ($("#boolDesignPackageAwards").attr("checked")) {
					return true;
				} else if ($("#strDPAwardsCategory").val() != "" || $("#strDPAwardsCategory_fake").val() != "Please Select") { 
					return true;
				} else {
					return false;
				}
			}
		}
    },
	errorPlacement: function(error, element) {
		error.appendTo( element.parent("div") );
	},
	invalidHandler: function(form, validator) {
		$('.validationinfo').show();
    }
  });
  NavHovers();
  $('.styledform').jForms({
	imagePath: curdir() + '_images/niceforms/',
	listSize: 10
  });
  $('.styledform > fieldset').dropShadow({
	left    : 1,
	top     : 1,
	blur    : 1,
	opacity : 0.45,
	color   : "#000000",
	swap    : false
  });
  PaymentOption();
  $("a.newwin").attr({target:"_blank"});
  $('tr#rows4').hide();
  $("#boolMarketAll,#boolMarketAll_fake").click(function(){
	AllChecksMarket();
  });
  $("#boolRegionAll,#boolRegionAll_fake").click(function(){
	AllChecksRegion();
  });
 });
function AllChecksMarket() {
	if ($("#boolMarketAll").attr("checked") == true) {
		$(".marketcheck").each(function(i){
			$(this).attr("checked","checked");
			id = $(this).attr("id");
			$("#"+id+"_fake").addClass("checkboxAreaChecked");
		});
	} else {
		$(".marketcheck").each(function(i){
			$(this).removeAttr("checked");
			id = $(this).attr("id");
			$("#"+id+"_fake").removeClass("checkboxAreaChecked");
			$("#"+id+"_fake").addClass("checkboxArea");
		});
	}
}
function AllChecksRegion() {
	if ($("#boolRegionAll").attr("checked") == true) {
		$(".regioncheck").each(function(i){
			$(this).attr("checked","checked");
			id = $(this).attr("id");
			$("#"+id+"_fake").addClass("checkboxAreaChecked");
		});
	} else {
		$(".regioncheck").each(function(i){
			$(this).removeAttr("checked");
			id = $(this).attr("id");
			$("#"+id+"_fake").removeClass("checkboxAreaChecked");
			$("#"+id+"_fake").addClass("checkboxArea");
		});
	}
}
function CopyAddress()
 {
  if($("form#entryforma input#boolCopyInvoiceAddress").attr("checked") == true)
   {
    $("form#entryforma input#strBillingAddress1").val($("form#entryforma input#strContactAddress1").val());
    $("form#entryforma input#strBillingAddress2").val($("form#entryforma input#strContactAddress2").val());
    $("form#entryforma input#strBillingTownCity").val($("form#entryforma input#strTownCity").val());
    $("form#entryforma input#strBillingStateCounty").val($("form#entryforma input#strStateCounty").val());
    $("form#entryforma input#strBillingZIPPostCode").val($("form#entryforma input#strZIPPostCode").val());
    $("form#entryforma select#strBillingCountry").val($("form#entryforma select#strCountry").val());
    $("form#entryforma input#strBillingCountry_fake").val($("form#entryforma input#strCountry_fake").val());
    $("label[for=boolCopyInvoiceAddress]").removeClass('chosen');
    $("#boolCopyInvoiceAddress_fake").addClass($("#boolCopyInvoiceAddress").attr("checked")?"checkboxArea":"checkboxAreaChecked").removeClass($("#boolCopyInvoiceAddress").attr("checked")?"checkboxAreaChecked":"checkboxArea");
   }
 }
function TaxEntryBox()
 {
  var strCountry = $("form#entryforma select#strCountry").val();
  if (strCountry.substr(0,3)=="EU-" && strCountry!="EU-United Kingdom")
   {
    $(".rowfortax").css({"display":"","visibility":"visible"});
		$('.styledform > fieldset').redrawShadow();
   }
  else
   {
    $(".rowfortax").css({"display":"none","visibility":"hidden"});
		$('.styledform > fieldset').redrawShadow();
   }
  $("form#entryforma input#strVATNumber").val("");
 }
function SpiritsSelectSecondary()
 {
  var strPrimaryCategory = $("form#entryformb select#primarycategory").val();
  $('tr#rows2').hide();
  $('tr#rows3').hide();
  if (strPrimaryCategory!="")
   {
    $('tr#rows4').show();
    fnDropDownReduce(document.getElementById("secondarycategory"),0);
    if (strPrimaryCategory!="RUMC") document.getElementById("secondarycategory").options[document.getElementById("secondarycategory").options.length]=new Option("Please select","");
    $.post("_xml/spiritsecondarycategory.php", { Primary: strPrimaryCategory }, function(data){ fnDropDownReplace(document.getElementById("secondarycategory"),1,data); SpiritsSelectTertiary(); }, "json");
   }
 }
function SpiritsSelectTertiary()
 {
  var strPrimaryCategory = $("form#entryformb select#primarycategory").val();
  var strSecondaryCategory = $("form#entryformb select#secondarycategory").val();
  $('tr#rows3').hide();
  if (strSecondaryCategory!="")
   {
    $('tr#rows4').show();
    $.post("_xml/spirittertiarycategory.php", { Primary: strPrimaryCategory, Secondary: strSecondaryCategory }, function(data){ fnDropDownReplace(document.getElementById("strBrandSpiritTypeCode"),1,data); $('tr#rows2').show(); $('tr#rows3').show(); $('tr#rows4').hide(); $('.fake_list').remove(); $('.selectArea').remove(); RemakeDropDowns(); }, "json");
   }
  else
   {
    $('tr#rows2').show(); 
    $('tr#rows4').hide();
    $('.fake_list').remove();
    $('.selectArea').remove();
    RemakeDropDowns();
   }
 }
function PrevSelectSecondary()
 {
  var strPrimaryCategory = $("form#previouswinners select#CategoryName").val();
  $.post("_xml/prevsecondarycategory.php", { Primary: strPrimaryCategory }, function(data){ fnDropDownReplace(document.getElementById("SubCategoryName"),1,data); $('.fake_list').remove(); $('.selectArea').remove(); RemakeDropDowns(); }, "json");
 }
function RemakeDropDowns()
 {
$('select').each(function ()
  {
	$('body').append('<ul id = "'+this.id+'_fake_list" class="fake_list"></ul>');
	$(this).children().each(function()
	{
		$('#'+$(this).parent().attr('id')+'_fake_list').append('<li>'+$(this).text()+'</li>');
	});
	$('#'+this.id+'_fake_list').css('height',($('#'+this.id+'_fake_list li:first').height()+4)*($('#'+this.id+'_fake_list li').size()<10?$('#'+this.id+'_fake_list li').size():10)).css('width',$(this).width()+18).toggle();

$('#'+this.id+'_fake_list > li').hover(function(){$(this).addClass('selected')},function(){$(this).removeClass('selected');}).each(function(i)
	{
		$(this).click(function(){
		var id = $(this).parent().attr('id');
		$('#'+id).toggle();
		$('#'+id.replace('_list','')).attr('value',$(this).text());
		$('#'+id.replace('_fake_list','')).attr('selectedIndex',i).change();
		$('#'+id+' >  li').removeClass('selected');
		$(this).addClass('selected');
		if(id.replace('_fake_list','')=="strCountry")TaxEntryBox();
	});
	});

$(this).before('<div class="selectArea" style="width:'+($(this).width()+43)+'px"><div class="left"></div><div class="right"><a class="selectButton" href="javascript:;" onclick="$(\'#'+this.id+'_fake\').focus().click()"></a></div><input type = "text" class="center" style="width:'+($(this).width())+'px;cursor:default" id="'+this.id+'_fake"  value="'+this[this.selectedIndex].text+'"  /></div>').css('position','absolute').css('left','-6000px');

	$('#'+this.id+'_fake').click(function(){
		$('#'+this.id+'_list').focus();
		$('#'+this.id+'_list').css('left',$(this).offset().left-8);
		
		if(($('#'+this.id+'_list').height()+$(this).offset().top -document.documentElement.scrollTop) > $(window).height())
		{
			$('#'+this.id+'_list').css('top',$(this).offset().top-$('#'+this.id+'_list').height()-4);
		}
		else
		{
			$('#'+this.id+'_list').css('top',$(this).offset().top+$(this).height()+4);
		}
		$('#'+this.id+'_list > li').removeClass('selected');
		$('#'+this.id+'_list > li').get($('#'+this.id.replace('_fake','')).attr('selectedIndex')).className='selected';

		
		$('.fake_list:visible:not(#'+this.id+'_list)').toggle();
		$list = $('#'+this.id+'_list');
		$list.toggle();
		var offSet = (($('.selected', $list).length>0? $('.selected', $list).offset().top:0)- $list.offset().top);
		$list.animate({scrollTop: offSet});
		}).css('font-size',$(this).css('font-size')).css('padding','2px 0px 2px 2px').keyup(function(e)
		{
			var pressedKey = e.charCode || e.keyCode || -1;
			var $dd = $('#'+this.id.replace('_fake',''));
			$('#'+this.id+'_list > li').removeClass('selected');
			switch(pressedKey)
			{
				//down
				case 40:
					var curr = ($dd.attr('selectedIndex')+1>=$('option',$dd).length?0:$dd.attr('selectedIndex')+1);
				break;
				case 38:
					var curr = ($dd.attr('selectedIndex')-1<0?$('option',$dd).length-1:$dd.attr('selectedIndex')-1);
				break;
				case 13:
					$('#'+this.id+'_list').toggle();
					return false;
				break;
				default:
				var t = new Date();
				if(t.getTime()-lastKeypress>1000)
				{
					lastKeypress=t.getTime();
					keyBuffer ='';
				}
					keyBuffer +=String.fromCharCode(pressedKey).toLowerCase();
					curr=-1;
					$('#'+this.id+'_list > li').each(function(i)
					{
						if($(this).text().toLowerCase().indexOf(keyBuffer)==0&&curr==-1)
						{
							curr = i;
							return;
						}
					});
					break;
			}
			if(curr==-1)
			{
				curr=0;
			}
			$($('#'+this.id+'_list > li').get(curr)).addClass('selected').focus();
			$list = $('#'+this.id+'_list');
			var offSet = (($('.selected', $list).length>0? $('.selected', $list).offset().top:0)- $list.offset().top);
			$list.attr('scrollTop',offSet);
			$dd.attr('selectedIndex',curr).change();
			$(this).attr('value', $($('#'+this.id+'_list > li').get(curr)).text());
			if(this.id.replace('_fake','')=="strCountry")TaxEntryBox();
			return false;
						
		}).focus(function()
		{
			$(document).keypress(function(e)
			{
				var pressedKey = e.charCode || e.keyCode || -1;
				if(pressedKey==13)
				{
					return false;
				}
			});
		}).blur(function()
		{
			$(document).unbind('keypress').unbind('click');
		});
});
 }
function NavHovers() {
	$('ul.mainnav > li.inactive').hover(function(i){
		$(this).css({'background-image' : 'url('+curdir() + '_images/menu-bg-left.jpg)', 'background-repeat' : 'no-repeat'});
		$(this).find('a').css({'background-image' : 'url('+curdir() + '_images/menu-bg-right.jpg)', 'background-repeat' : 'no-repeat', 'background-position' : 'right'});
		$(this).append("<img src='"+curdir() + "_images/menu-bg-centre.jpg' alt='Menu Background' title='' class='menubackgroundhover' />");
		imgwidth = $(this).find("a").width();
		$('img.menubackgroundhover').width(imgwidth+3);
		$('img.menubackgroundhover').height(24);
		},
		function(i) {
		$(this).css({'background-image' : 'none'});
		$(this).find('a').css({'background-image' : 'none'});
		$(this).find("img.menubackgroundhover").remove();
	});
	imgwidth = $('ul.mainnav > li.active > a').width();
	$('img.menubackground').width(imgwidth+3);
	$('img.menubackground').height(24);
 }
 
 function PaymentOption(){
	 if ($('#BoolInvoice1:checked')) {
		 $('.hiddeninv').parent().hide();
		 $('.hiddentnc').hide();
	 }
	 $("[id^='BoolInvoice1']").click(function(){
			$('input.hiddeninv').parent().hide();
			$('.hiddeninv').hide();
			$('.hiddentnc').hide();
			$('input.hiddenccv').parent().show();
			$('.hiddencc').show();
	 });
	 $("[id^='BoolInvoice2']").click(function(){
		document.getElementById("RequestType").value="Invoice";
		$('input.hiddenccv').parent().hide();
		$('.hiddencc').hide();
		$('input.hiddeninv').parent().show();
		$('.hiddeninv').show();
		$('.hiddentnc').show();
	 });
	 $("[id^='BoolInvoice3']").click(function(){
		document.getElementById("RequestType").value="ManualRequest";
		$('input.hiddenccv').parent().hide();
		$('.hiddencc').hide();
		$('input.hiddeninv').parent().show();
		$('.hiddeninv').show();
		$('.hiddentnc').show();
	 }); 
 }
 
 
 function curdir() {
	dir = window.location.pathname;
	dirlength = dir.length;
	newlength = dirlength - dir.replace(/\//g, "").length;
	if (window.location.hostname == "eupds01.prowebgroup.co.uk" || window.location.hostname == "eupds01")  {
		newlength = newlength - 1;
	}
	newdir = "./";
	newlength = newlength - 1;
	while (newlength > 0) {
		newdir = newdir + "../";
		newlength = newlength - 1;
	}
	return newdir;
 }