var totalshareholders=0;
var vat_rate=0.175;

$(document).ready(function(){
	$("body").append("<div id=\"dialog\" title=\"AJAX Error Report\"></div>");
	$("#dialog").dialog({bgiframe:true,autoOpen:false,width:720,modal:true,zIndex:800});
	$(".numeric").numeric();
	
	/*****************************/
	/*** Check checkbox fields ***/
	/*****************************/
	calctotal();
	if ($("#Product_DomainRegistration").attr("checked")==true){$("#DomainReg_Container").show("fast");}//Domain Registration
	if ($("#vatregistration").attr("checked")==true){$("#VATRegistration_Container").show("fast");}//VAT Registration
	/*****************************/
	/*** End of Checkbox check ***/
	/*****************************/
	
	$("#id_projectNo").keyup(function(e) {
		this.value=this.value.toUpperCase();
		this.value=this.value.replace(/ /g,".");
		this.value=this.value.replace(/[^a-zA-Z0-9|\s|-]/g,'.');
		str=this.value;
		if (str.length<5){
			$("#projectNo_info").html("<img src=\"images/statusBad.gif\" /> Case Number not long enough.");
			$("#uniqueProjectNo").attr("value",0);
			return false;
			}
		if (str.charAt(str.length-1)=="."){
			$("#projectNo_info").html("<img src=\"images/statusBad.gif\" /> Case no must not end with a full stop.");
			$("#uniqueProjectNo").attr("value",0);
			return false;
			}
		$.post("ajax/checkProjectNo.asp",{"ProjectNo":str},function(xml){
			var x=new Array(
				$(xml).find("response").text(),
				$(xml).find("unique").text()
				);
			$("#projectNo_info").html(x[0]);
			$("#uniqueProjectNo").attr("value",x[1]);
			});
		return false;
		});

	$("#Product_DomainRegistration").click(function() {
		if (this.checked==false) {
			document.incorpform.domainname.value="";
			document.incorpform.domaincomments.value="";
			$("#DomainReg_Container").hide("fast");
			}
		else {
			$("#DomainReg_Container").show("fast");
			}
		});
	
	$("#id_bankaccount").click(function(e){
		if (this.checked==true) {
			var msg="Thank you for choosing our FREE bank account service\n";
			msg+="\n";
			msg+="People can only be added to the bank account subject to status, credit searches\n";
			msg+="and the terms and conditions of the bank at the time of application.\n";
			msg+="\n";
			msg+="Within 48 hours after completing this form you will be contacted by HSBC.\n";
			msg+="\n";
			msg+="Use the check boxes below each officer section to denote who will be a signatory!";
			alert(msg);
			}
		});
	
	$("#contactisvat").click(function(e){
		if (this.checked==true) {
			$("#vataddln1").attr("value",$("#cltaddln1").attr("value"));
			$("#vataddln2").attr("value",$("#cltaddln2").attr("value"));
			$("#vattown").attr("value",$("#clttown").attr("value"));
			$("#vatcounty").attr("value",$("#cltcounty").attr("value"));
			$("#vatpstcd1").attr("value",$("#cltpstcd1").attr("value"));
			$("#vatpstcd2").attr("value",$("#cltpstcd2").attr("value"));
			}
		else {
			$("#vataddln1").attr("value","");
			$("#vataddln2").attr("value","");
			$("#vattown").attr("value","");
			$("#vatcounty").attr("value","");
			$("#vatpstcd1").attr("value","");
			$("#vatpstcd2").attr("value","");
			}
		});
	
	$("#clregisteredoffice").click(function(e){
		if (this.checked==true) {
			$("#contactisregoff").attr("checked",false);
			$("#regaddln1").attr("value","ConveyIT House");
			$("#regaddln2").attr("value","28 Coity Road");
			$("#regtown").attr("value","BRIDGEND");
			$("#regcounty").attr("value","Mid Glamorgan");
			$("#regpstcd1").attr("value","CF31");
			$("#regpstcd2").attr("value","1LR");
			}
		else {
			$("#regaddln1").attr("value","");
			$("#regaddln2").attr("value","");
			$("#regtown").attr("value","");
			$("#regcounty").attr("value","");
			$("#regpstcd1").attr("value","");
			$("#regpstcd2").attr("value","");
			}
		calctotal();
		});
	
	$("#contactisregoff").click(function(e){
		if (this.checked==true) {
			$("#clregisteredoffice").attr("checked",false);
			$("#regaddln1").attr("value",$("#cltaddln1").attr("value"));
			$("#regaddln2").attr("value",$("#cltaddln2").attr("value"));
			$("#regtown").attr("value",$("#clttown").attr("value"));
			$("#regcounty").attr("value",$("#cltcounty").attr("value"));
			$("#regpstcd1").attr("value",$("#cltpstcd1").attr("value"));
			$("#regpstcd2").attr("value",$("#cltpstcd2").attr("value"));
			}
		else {
			$("#regaddln1").attr("value","");
			$("#regaddln2").attr("value","");
			$("#regtown").attr("value","");
			$("#regcounty").attr("value","");
			$("#regpstcd1").attr("value","");
			$("#regpstcd2").attr("value","");
			}
		calctotal();
		});
	
	$("#chkCompanySecretary").click(function(e){
		if (this.checked==true) {
			$("#tblSecretary").show("fast");
			}
		else {
			document.incorpform.seccompanyname.value="";
			document.incorpform.secTitle.value="Please select";
			document.incorpform.secforename.value="";
			document.incorpform.secsurname.value="";
			document.incorpform.secDOBdd.value="1";
			document.incorpform.secDOBmm.value="1";
			document.incorpform.secDOByyyy.value="19";
			document.incorpform.secnationality.value="";
			document.incorpform.secfathersfirst.value="";
			document.incorpform.seceyecolour.value="";
			document.incorpform.secmothersmaidenname.value="";
			document.incorpform.secaddln1.value="";
			document.incorpform.secaddln2.value="";
			document.incorpform.sectown.value="";
			document.incorpform.seccounty.value="";
			document.incorpform.secpstcd1.value="";
			document.incorpform.secpstcd2.value="";
			document.incorpform.seccountry.value="United Kingdom";
			document.incorpform.SecSub.checked=false;
			document.incorpform.SecShares.value="1";
			document.getElementById("tblSecSubscriber").style.display="none";
			document.incorpform.SecDir.checked=false;
			$("#tblSecretary").hide("fast");
			}
		});
	
	$("#vatregistration").click(function(e) {
		calctotal();
		if (this.checked==true) {
			$("#VATRegistration_Container").show("fast");
			}
		else {
			$("#VATRegistration_Container").hide("fast");
			}
		});
	
	$("#incorpform").submit(function(e) {
		e=false;
		d="";
		if ($("#uniqueProjectNo").attr("value")!=1) {
			d+="- You must enter a valid case number\n";e=true;
			}
		if (e==true) {
			d="There are errors with this form, please complete the following errors and try again.\n\n"+d;
			alert(d);
			return false;
			}
		else {
			return true;
				}
		});
	
	$("#SecDir").click(function(e) {
		if ($("#SecDir").attr("checked")==true && ($("#secDOByyyy").attr("value")=="19" || $("#secDOByyyy").attr("value")=="")){
			var d="As you are opting to select that this secretary is also a director please remember to complete the\n";
			d+="Secretarys Date of Birth (DOB)\n\nYou will now be sent to the relevant field(s)";
			alert(d);
			$("#secDOByyyy").focus();
			}
		});

	
		
	//End document ready.............................................................................
	//End document ready.............................................................................
	//End document ready.............................................................................
	});

function DirSubDetails(DirNumber){
	if($("#Dir"+DirNumber+"Sub").attr("checked")==true){
		$("#tblDir"+DirNumber+"Sub").show("fast");
		totalshareholders+=1;
		alert("This director has now been defined as a subscriber and given a share issue of 1\n\nPlease remember to change the number of shares issued for the director if this wrong!");
		$("#Dir"+DirNumber+"Shares").focus();
		}
	else {
		$("#tblDir"+DirNumber+"Sub").hide("fast");
		totalshareholders-=1;
		}
	$("#totalshareholders").attr("value",totalshareholders);
	}

function SecSubDetails(){
	if($("#SecSub").attr("checked")==true){
		$("#tblSecSubscriber").show("fast");
		totalshareholders+=1;
		alert("This director has now been defined as a subscriber and given a share issue of 1\n\nPlease remember to change the number of shares issued for the director if this wrong!");
		$("#SecShares").focus();
		}
	else {
		$("#tblSecSubscriber").hide("fast");
		totalshareholders-=1;
		}
	$("#totalshareholders").attr("value",totalshareholders);
	}

function fnBankAccSig(tblname,fldname){
	if ($("#"+tblname).css("display")=="none"){
		$("#"+tblname).show("fast");
		var msg="";
		msg+="People can only be added to the bank account subject to status, credit searches\n";
		msg+="and the terms and conditions of the bank at the time of application.\n\n";
		msg+="We will also need their contact number(s) to verify their consent\n\n";
		msg+="Please complete accordingly\n\n";
		alert(msg);
		$("#"+fldname).focus();
		}
	else {
		$("#"+tblname).hide("fast");
		$("#"+fldname).attr("value","");
		}
	}


function checkWhois(){
	var dom=document.incorpform.domainname.value;
	if (dom!=null && dom!=""){
		$.post("ajax/WhoisLookup.asp",{"domain":dom},function(d){
			showDialog(d,"WHOIS Results");
			});
		}
	else {
		alert("Please enter a domain name to check.");
		}
	}

function fillDomainName(str) {
	str=str.toLowerCase();
	str=str.replace(/ /g,"");
	str=str.replace(/[^a-zA-Z0-9|-]/g,"");
	document.incorpform.domainname.value=str;
	}

function calctotal() {
	var CompanyPackE1=$("#CompanyPackE1").attr("checked");
	var CompanyPackB1=$("#CompanyPackB1").attr("checked");
	var CompanyPackS1=$("#CompanyPackS1").attr("checked");
	var CompanyPackS2=$("#CompanyPackS2").attr("checked");
	var CompanyPackF1=$("#CompanyPackF1").attr("checked");
	var CompanyPackF2=$("#CompanyPackF2").attr("checked");
	var RegisteredOffice=$("#clregisteredoffice").attr("checked");
	var VatRegistration=$("#vatregistration").attr("checked");
	$.post(
		"ajax/CheckIncorporationPrice.asp"
		, {
			"CompanyPackE1":CompanyPackE1
			,"CompanyPackB1":CompanyPackB1
			,"CompanyPackS1":CompanyPackS1
			,"CompanyPackS2":CompanyPackS2
			,"CompanyPackF1":CompanyPackF1
			,"CompanyPackF2":CompanyPackF2
			,"RegisteredOffice":RegisteredOffice
			,"VatRegistration":VatRegistration
			}
		, function(xml) {
			var s=$(xml).find("status").text();
			var d=$(xml).find("description").text();
			if (s==0){$("#IncorporationPrice").html(d);}else{alert(d)};
			}
		);
	}


function clearDomain(thisField) {
	if (thisField==false) {
		document.incorpform.domainname.value="";
		document.incorpform.domaincomments.value="";
		document.getElementById("domainReg_container").style.display="none";
		}
	else {
		document.getElementById("domainReg_container").style.display="";	
		}
	}
	
function LoadProductInfo(id) {
	$.post("ajax/LoadProductInfo.asp",{"Product":id},function(d) {
		$("#dialog").html(d);
		$("#dialog").dialog("option","title","Product Information");
		$("#dialog").dialog("option","height",480);
		$("#dialog").dialog("open");
		});
	}
		
	
//Misc functions...
function roundNumber(n,dp) {
	var x=Math.round(n*Math.pow(10,dp))/Math.pow(10,dp);
	x=x.toFixed(dp);return x;
	}

function showDialog(str,title){
	$("#dialog").html(str);
	$("#dialog").dialog("option","title",title);
	$("#dialog").dialog("open");
	}