/*

<Prologue>
		<Name>			OverseasReport.js 					</Name>
		<Version>		1.0									</Version>
		<VersionDate>	2006-10-09							</VersionDate>
		<Application>	First Report main site				</Application>
		<Language>		JavaScript							</Language>
		<Owner>			First Report Ltd					</Owner>
		<CreatedDate>										</CreatedDate>
		<Copyright>		yMonda Ltd 2006						</Copyright>
		<Author>		W Charlton							</Author>
		<Output>		HTML								</Output>
		<DependsOn> </DependsOn>
		<Dependent> </Dependent>
		<Notes>
					Validates the Overseas Report HTML
		</Notes>
		<RevisionList>
			2006-10-09 WHC stopped form submission if not enough credits, removed reference to AllCountryList5
			2006-10-26 JO added new logic
			2006-10-27 JO revised logic
			2007-07-02 JO revised instant report list
			2009-09-23 JO added AutoJumpIreland
		</RevisionList>
	</Prologue>

*/
var service
service = "";
function ValidateForm(objForm){
	with (objForm)
	{

	if (coname.value == ""){
		alert("Please enter a company Name");
		coname.focus();
		return (false);
	}

	if (!goodAddress(street, "Please enter a Street Number and Name")){
		return (false);
	}

	if (towncity.value.length < 2){
		alert("Please enter a Town");
		towncity.focus();
		return (false);
	}

	if (ReportCountry.value == "00"){
		alert("Please select a country");
		ReportCountry.focus();
		return (false);
	}

	//if (service == ""){
	//  alert("Please select the service you require.");
	//  return (false);
	//}

	//if (agree.checked == false){
	//  alert("You must have read and agreed to the Terms and Conditions.");
	//  return (false);
	//}
	}
return (true);
}

function ChangeService(newservice,radioitem){
	service = newservice;
	if (service == "24hour" && document.frmOverseas.ReportCountry.value!="United States of America"){
		alert("You can not select the 24 hour service option unless the country chosen is USA.");
		radioitem.checked=false;
		service='';
	}
}

function UserID(){
	return document.getElementById('UserID').value
}

function IsCountryAllowed(objSelection, AutoJumpIreland){
	if (service == "24hour" && objSelection.value!="United States of America"){
		alert("You can only select USA if the service chosen is 24 hours.");
		objSelection.selectedIndex=0;
	}

	//Set Default ServiceID
	document.frmOverseas.ServiceID.value = document.frmOverseas.OverseasServiceID.value  //Internationals ServiceID

	if (objSelection.value == 'IRELAND' && AutoJumpIreland =='1'){ //Ireland Report
		document.frmOverseas.action="/GetReport.aspx";
		document.frmOverseas.submit();
	}

	//Hide Top Block
	document.getElementById('EireRow').style.display = 'none';
	document.getElementById('InCountryList1').style.display = 'none';
	document.getElementById('InCountryList2').style.display = 'none';
	document.getElementById('InCountryList3').style.display = 'none';
	document.getElementById('InCountryList4').style.display = 'none';
	document.getElementById('InCountryList5').style.display = 'none';
	document.getElementById('InCountryList6').style.display = 'none';

	//Hide Bottom Block
	document.getElementById('NotInCountryList1').style.display = 'none';
	document.getElementById('NotInCountryList2').style.display = 'none';
	document.getElementById('NotInCountryList3').style.display = 'none';
	document.getElementById('NotInCountryList4').style.display = 'none';
	document.getElementById('NotInCountryList5').style.display = 'none';

	//Show Bottom Text
	document.getElementById('AllCountryList1').style.display = '';
	document.getElementById('AllCountryList2').style.display = '';
	document.getElementById('AllCountryList2a').style.display = 'none'; //Please buy more
	//document.getElementById('AllCountryList3').style.display = '';
	document.getElementById('AllCountryList4').style.display = '';
	document.getElementById('AllCountryList6').style.display = '';

	document.getElementById('IntText').style.display = 'none';
	document.getElementById('EireText').style.display = 'none';

	//Uncheck all options because we have to check if they have enough credits
	document.frmOverseas.ManualReport[0].checked=true;  //Select Top option

	//var strInstantCountryList = ',Austria,Belgium,Bulgaria,Canada,Croatia,Czech Republic,France,Germany,Hungary,Ireland,Netherlands,Poland,Portugal,Romania,Slovakia,Slovenia,Spain,Sweden,United States,';
	var strInstantCountryList = ',Austria,Belgium,France,Germany,Ireland,Netherlands,Portugal,Spain,Sweden,';
	var bInstantCountry = (strInstantCountryList.toUpperCase().indexOf(',' + objSelection.value + ',')!=-1);

	if (objSelection.value == 'IRELAND' && document.frmOverseas.EireCreditsRemaining.value > 0){ //Ireland Report with enough credits
		document.frmOverseas.FixedCost[1].checked=true;
		document.getElementById('EireText').style.display = '';
		document.getElementById('EireRow').style.display = '';
		document.getElementById('InCountryList2').style.display = '';
		document.getElementById('AllCountryList1').style.display = 'none';
		document.getElementById('AllCountryList2').style.display = 'none';
		document.getElementById('AllCountryList2a').style.display = 'none'; //Please buy more
		//document.getElementById('AllCountryList3').style.display = 'none';
		document.getElementById('AllCountryList4').style.display = 'none';
		document.getElementById('AllCountryList6').style.display = 'none';
		document.frmOverseas.ServiceID.value = document.frmOverseas.EireServiceID.value   //Eire ServiceID
		CheckCredits(1); //Default selected option, check if user has 1 Eire credit
	}else{

		document.getElementById('IntText').style.display = '';
		if(bInstantCountry){// Instant Report Country List
			//Show Top Block
			document.frmOverseas.FixedCost[0].checked=true;
			document.frmOverseas.ManualReport[0].checked=true;
			document.frmOverseas.FixedCost.checked=true;
			document.getElementById('InCountryList1').style.display = '';
			document.getElementById('InCountryList2').style.display = '';
			document.getElementById('InCountryList3').style.display = '';
			document.getElementById('InCountryList4').style.display = '';
			document.getElementById('InCountryList5').style.display = '';
			document.getElementById('InCountryList6').style.display = '';
			document.getElementById('IntText').style.display = '';
			CheckCredits(5); //Default selected option, check if user has 5 credits

		}else{// NOT Instant Report Country List

			//Show Bottom Block
			document.frmOverseas.FixedCost.checked=false;
			document.frmOverseas.ManualReport[4].checked=true;
			document.getElementById('NotInCountryList1').style.display = '';
			document.getElementById('NotInCountryList2').style.display = '';
			document.getElementById('NotInCountryList3').style.display = '';
			document.getElementById('NotInCountryList4').style.display = '';
			document.getElementById('NotInCountryList5').style.display = '';
			document.getElementById('InCountryList3').style.display = 'none';
			CheckCredits(10); //Default selected option, check if user has 10 credits
		}
	}
}

function CheckCredits(intCreditsRequired){
	var intCreditsRemaining = document.frmOverseas.IntCreditsRemaining.value;
	var EireCreditsRemaining = document.frmOverseas.EireCreditsRemaining.value;
	var strReportCountry = document.frmOverseas.ReportCountry.value.toUpperCase();
	document.frmOverseas.CreditsRequired.value = intCreditsRequired;

	if (strReportCountry == 'IRELAND' && EireCreditsRemaining > 0){ //Ireland Report with enough credits

		document.getElementById('SubmitButton').style.display = '';
		document.getElementById('Submit1').disabled= false;

	}else{  //Not Ireland Report - check credits

		var blnEnoughCredits = false;
		var arrIntCreditsRemaining = document.frmOverseas.IntCreditsRemaining.value.split(',');
		var strResults = ''
		var iNeededCredits = 0;
		var iMoreCredits = 0;

		for ( var i=0, len=arrIntCreditsRemaining.length; i<len; ++i ){

			if(intCreditsRequired > arrIntCreditsRemaining[i+1]){ //Not enough of these credits left

				if (arrIntCreditsRemaining[i+2]=='True'){ //Allowed to purchase these credits

					if(arrIntCreditsRemaining[i+1]==0){ //None of these credits left
						strCredits = intCreditsRequired + ' ';
						iMoreCredits = intCreditsRequired;
					}else{
						iNeededCredits = intCreditsRequired - arrIntCreditsRemaining[i+1]
						strCredits = iNeededCredits + ' more '
						iMoreCredits = iNeededCredits;
					}
					strResults = strResults + strCredits + arrIntCreditsRemaining[i] +' credits'
					strResults = strResults + ' or '
				}

			}else{
				blnEnoughCredits = true;
			}
			++i
			++i
		}

		if(strResults.length>0){
			strResults = String(strResults).substring(0,strResults.length-4);
			var strLink =  'To place your order please add ' + strResults + ' <a href="#?intCredits=' + iMoreCredits + '">here</a>.'
			if (UserID()=='1')
				strLink = strLink.replace("#", "/openaccount.asp");
			else
				strLink = strLink.replace("#", "/account/AddCredits.asp");
			
			document.getElementById('BuyMoreCredits').innerHTML = strLink
		}else{
			document.getElementById('BuyMoreCredits').innerHTML = '';
		}

		if(blnEnoughCredits){// Enough Credits
			document.getElementById('AllCountryList2a').style.display = 'none';
			document.getElementById('AllCountryList2').style.display = '';
			//document.getElementById('AllCountryList3').style.display = 'none';
			document.frmOverseas.CreditOption.checked=true;
			document.getElementById('SubmitButton').style.display = '';
			document.getElementById('Submit1').disabled= false;
			document.getElementById('RequestDetails').style.display = '';
			document.getElementById('AllCountryList4').style.display = 'none';
			document.getElementById('AllCountryList6').style.display = 'none';
		}else{ // Not Enough Credits
			document.getElementById('AllCountryList2').style.display = 'none';
			document.getElementById('AllCountryList2a').style.display = '';			
			document.getElementById('RequestDetails').style.display = 'none';
			document.getElementById('AllCountryList4').style.display = '';
			document.getElementById('AllCountryList6').style.display = '';
			

/*
			if(intCreditsRemaining==0){
				strCredits = intCreditsRequired;
			}else{
				iNeededCredits = intCreditsRequired - intCreditsRemaining
				strCredits = iNeededCredits + ' more '
			}
			//document.getElementById('BuyMoreCredits').innerHTML = 'Please buy ' + strCredits + ' International Credits to place this order.';
*/
			document.getElementById('SubmitButton').style.display = 'none';
			document.getElementById('Submit1').disabled= true;
		}
	}
}