function validateDDL(Id, NotSelectedValue, Msg)
{
	if (document.getElementById(Id).value == NotSelectedValue)
	{
		alert(Msg);
		document.getElementById(Id).focus();
		return false;
	}
	return true;
}

// Prevent <label> tags from clearing the form element when clicked.
function selectElement(Id)
{
	if (document.getElementById(Id))
	{
		document.getElementById(Id).focus();
		if (document.getElementById(Id).click) document.getElementById(Id).click();
	}
	return false;
}

// XMLHttp return function.
function processReqChange()
{
	return true;
}

function gadgetSmartSearch()
{
	try
	{
		// Show/hide questions.
		if (document.getElementsByName('SearchType')[0] && document.getElementById('SmartSearch') && document.getElementById('StandardSearch'))
		{
			var IsSmartSearch = document.getElementById('SmartSearch').checked;
			var IsQuickSearch = document.getElementById('StandardSearch').checked;
			
			document.getElementById("divStandardProceed").style.display=(IsSmartSearch)?'none':'block'
			document.getElementById("divSmartProceed").style.display=(IsSmartSearch)?'block':'none'
			
			if (IsSmartSearch || IsQuickSearch)
			{
				// Log enquiry selection.
				try
				{
					//var recache = "&recache=" + Math.floor(Math.random() * 1000 + 1);
					var xmlObjSupported = loadXMLDoc("EnquirySelect.asp?ScoringType=" + ((IsSmartSearch) ? "1" : "0") + recache, processReqChange)
				} catch (ex) {}
			}
			
			//Fix IE7 bug with footer
			if (document.getElementById('maincontentbothads'))
			{
			    if (IsSmartSearch)
			    {		    
			      			     
			      document.getElementById('maincontentbothads').style.height = '';
			     
			    }
			    else
			    {
				    document.getElementById('maincontentbothads').style.height = '';
			    }
			}
						
			// Configure the gadget.
			document.getElementById("CreditQuestions").style.display = (IsSmartSearch) ? "block" : "none";
		}
	} catch (ex2) {}
}


// Validation.
function checkSpecific()
{
	// Loan amount.
	if (!isFieldValidAndFocus(document.LoansAssessment.Amount, 'Integer', false, 'Loan amount'))
		return false;	
	else
	{
		if (! isIntegerInRange(document.LoansAssessment.Amount.value, 50, 250000))
		{
			alert ("Loan amount must be between £50 and £250,000");
			return false;
		}
	}

	// Term.
	if (!validateDDL("Term", "select", "Please select the repayment period"))
		return false;

	// Residential status.
	if (!validateDDL("C9", "", "Please select your current residential status"))
		return false;

	// Search type.
	if (!document.getElementById("SmartSearch").checked && !document.getElementById("StandardSearch").checked)
	{
		alert("Please select the type of search your require");
		return false;
	}

	// SmartSearch questions.
	if (document.getElementById("SmartSearch").checked)
	{
	    // Date of birth.
	    if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBDate, 'Day', false, 'Date of birth (day)'))
		    return false;
	    if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBMonth, 'Month', false, 'Date of birth (month)'))
		    return false;
	    if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBYear, 'Year', false, 'Date of birth (year)'))
		    return false;

	    var currentTime = new Date();
	    var entDay = document.getElementById("C1DoBDate").value;         //C1DoBDate
	    var entMonth = document.getElementById("C1DoBMonth").value;       //C1DoBMonth
	    var entYear = document.getElementById("C1DoBYear").value;        //C1DoBYear
	    if(entYear > currentTime.getFullYear() ||
		    (entYear == currentTime.getFullYear() &&
		    entMonth >= (currentTime.getMonth() + 1) &&
		    entDay > currentTime.getDate()))
	    {
		    alert("Date of birth cannot be in the future");
		    document.getElementById("C1DoBYear").focus();
		    document.getElementById("C1DoBYear").select();
		    return false;
	    }
	    
		// Postcode.
		if (!isFieldValidAndFocus(document.LoansAssessment.C1, 'Postcode', false, 'Postcode'))
			return false;

		// Current address.
		if (!validateDDL("C2", "", "Please select how many years have you lived at your current address"))
			return false;

		// CCJ/Bankruptcy.
		if (!validateDDL("C3", "", "Please select if you have ever had a CCJ or bankruptcy"))
			return false;

		// Default.
		if (!validateDDL("C4", "", "Please select if you have ever defaulted on a credit account"))
			return false;

		// Credit account.
		if (!validateDDL("C5", "", "Please select if you have ever missed a payment on a credit account"))
			return false;

		// Credit applications.
		if (!validateDDL("C6", "", "Please select how many credit accounts have you applied for in the past 6 months"))
			return false;

		// Credit accounts.
		if (!isFieldValidAndFocus(document.LoansAssessment.C7a, 'Integer', false, 'Number of personal loans'))
			return false;
		if (!isFieldValidAndFocus(document.LoansAssessment.C7b, 'Integer', false, 'Number of overdrafts'))
			return false;
		if (!isFieldValidAndFocus(document.LoansAssessment.C7c, 'Integer', false, 'Number of credit/store cards'))
			return false;

		// Employment status.
		if (!validateDDL("C10", "", "Please select your current employment status"))
			return false;

		// Current account duration.
		if (!validateDDL("C11", "", "Please select how long you have held your main current account for"))
			return false;

		// Email.
		if (!isFieldValidAndFocus(document.LoansAssessment.Email, 'Email', false, 'Email address'))
			return false;
	}

	// This has to be done here for IE 5.2 Mac otherwise the form submits when you click on text in the form!!
	var IsSmartSearch = document.getElementsByName('SearchType')[0].checked;
	document.getElementById("CreditSearch").action = (IsSmartSearch) ? "results.asp" : "LoansApplicationdb.asp";

	// No validation errors.
	return true;
}

// Car Loans Validation.
function checkCar()
{
	// Loan amount.
	if (!isFieldValidAndFocus(document.LoansAssessment.Amount, 'Integer', false, 'Loan amount'))
		return false;	
	else
	{
		if (! isIntegerInRange(document.LoansAssessment.Amount.value, 50, 250000))
		{
			alert ("Loan amount must be between £50 and £250,000");
			return false;
		}
	}

	// Term.
	if (!validateDDL("Term", "select", "Please select the repayment period"))
		return false;

	// Residential status.
	if (!validateDDL("C9", "", "Please select your current residential status"))
		return false;

	// No validation errors.
	return true;
}

/* Old gadget validation */
function submitPLA()
{
  if (!isFieldValidAndFocus(document.LoansAssessment.Amount,'Integer', false, "Loan Amount"))
  {
    return false;
  }

  if ((document.LoansAssessment.Amount.value < 500) || (document.LoansAssessment.Amount.value > 100000))
  {
  	alert("You must enter a loan value between 500 and 100000");
	document.LoansAssessment.Amount.focus;
	document.LoansAssessment.Amount.select;
	return false;
  }
  if (document.LoansAssessment.Term.selectedIndex == 0)
	{
		alert("Please select the period of time over which you would like to repay your loan.");
		return false;
	}
  if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBDate,"Day", false, 'Date of Birth'))
		{
			return false;
		}
  if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBMonth,"Month", false, 'Date of Birth'))
		{
			return false;
		}		
  if (!isFieldValidAndFocus(document.LoansAssessment.C1DoBYear,"Year", false, 'Date of Birth'))
		{
			return false;
		}
 if (!(document.LoansAssessment.HomeOwner[0].checked) && !(document.LoansAssessment.HomeOwner[1].checked))
  		{
			alert("Please indicate if you own your home or not.");
			return false;
		}
 if (!(document.LoansAssessment.PPI[0].checked) && !(document.LoansAssessment.PPI[1].checked))
  		{
			alert("Please indicate if you wish to see the payment including Payment Protection Insurance or not.");
			return false;
		}
		
/* if (!(document.LoansAssessment.PayOffExisting[0].checked) && !(document.LoansAssessment.PayOffExisting[1].checked))
  		{
			alert("Please indicate if you have existing credit you wish to consolidate/refinance.");
			return false;
		}*/
if (document.LoansAssessment.LoanCCJC1[1]) 
	{
	 if (!(document.LoansAssessment.LoanCCJC1[0].checked) && !(document.LoansAssessment.LoanCCJC1[1].checked))
	  		{
				alert("Please indicate if you have any history of adverse credit.");
				return false;
			}
	}
return true;  
}

function HideCreditQuestions()
{
	if (document.getElementById('CreditQuestions'))
	{
		var creditQuestions = document.getElementById('CreditQuestions');
		creditQuestions.style.display = "none";
		
		var quickSearchObject = document.getElementById('StandardSearch');
		quickSearchObject.checked = true;
		
		document.getElementById("divStandardProceed").style.display='block'
			document.getElementById("divSmartProceed").style.display='none'
	}
}
