function HideAllSteps() {
        document.getElementById('divStep1').style.display = 'none';
        document.getElementById('divStep2').style.display = 'none';
        document.getElementById('divStep3').style.display = 'none';
      }

      function ShowStep2() {
        var strNewUrl;

        GoToStep2Clicked = true;

        if (isValidStep1('ErrorHolderStep1')) {
          //HideAllSteps();
          //document.forms[appFormName].all['divStep2'].style.display = '';

          strNewUrl =  strSSLURLtoApp + '/mortgage_form_3steps_2st.php';
          //strNewUrl += '?referrer='+FORM_DATA['referrer']+'&showstep=2';
          strNewUrl += '?referrer='+appFormName+'&showstep=2';
          strNewUrl += '&mortgage_size='+document.forms[appFormName].elements["Mortgage_Size"].value;
          strNewUrl += '&mortgage_type='+document.forms[appFormName].elements["mortgagetypebasic"].value;
          strNewUrl += '&bad_credit='+document.forms[appFormName].elements["bad_credit"].value;
          strNewUrl += '&property_value='+document.forms[appFormName].elements["Property_Value"].value;
          strNewUrl += '&country='+document.forms[appFormName].elements["country"].selectedIndex;
          strNewUrl += '&UrlReferrer='+document.forms[appFormName].elements["UrlReferrer"].value;

          var oftbyes=document.forms[appFormName].elements["ftbyes"];
          var oftbno=document.forms[appFormName].elements["ftbno"];
          var ofpyes = document.forms[appFormName].elements["fpyes"];
          var ofpno = document.forms[appFormName].elements["fpno"];

          var fp='';
          if (ofpyes.checked) {fp='1';}
          if (ofpno.checked) {fp='0';}
          strNewUrl += '&fp='+fp;

          var ftb='';
          if (oftbyes.checked) {ftb='1';}
          if (oftbno.checked) {ftb='0';}
          strNewUrl += '&ftb='+ftb;


          //strNewUrl += '&strAllErrors='+escape(strAllErrors);
          strNewUrl += '&intErrorId='+intErrorId;

          //alert(strNewUrl);
          parent.document.location.href = strNewUrl;
        }

      }

      function NextValidationStep2() {
        if (GoToStep2Clicked) {
          ShowStep2();
        }
      }

      function NextValidationSubmit() {
        if (SubmitClicked) {
          SubmitForm();
        }
      }

      function showStep3() {
        HideAllSteps();
        document.getElementById('divStep3').style.display = '';
      }

      function SubmitForm() {
        if (FormValidation('ErrorHolderStep3')) {
          document.getElementById('btnSubmit').disabled = true;
          document.getElementById('frmMain').action = strURLtoIncludes + '/form_submit.php';
          document.getElementById('frmMain').submit();
          return true;
        };

        return false;
      }

      function FormValidation(error_holder) {
        var intLeadbayMT = getMortgageType();
        var obj, obj2, numb, strErrMessage, blRes = true, isOK = true, blDateError = false;

        ClearRedMessage(error_holder);

        // ----------------------------------------------

        // loan to value validation
        var intPropertyvalue = document.forms[appFormName].elements['Property_Value'].value;
        var intMortgageSize = document.forms[appFormName].elements['Mortgage_Size'].value;
        var percentage = mortgagetypesPercentage[parseInt(intLeadbayMT)];
        var allowed_amount = (intPropertyvalue * (percentage))/100;

        if (isOK && (intMortgageSize > allowed_amount)) {
          obj = document.forms[appFormName].elements["Mortgage_Size"];
          strErrMessage = "<br/>Sorry! The amount you want to borrow is more than " + percentage + "% of the Property Value.<br/><br/>Your borrow amount needs to be £" + allowed_amount + " or less for this Property Value. Please amend below:<br />";
          ShowRedMessage(strErrMessage,"Mortgage_Size","Mortgage_Size", error_holder);
          AddClientErrorStep2('Loan to Value');
          isOK = false;
        }

        // first name
        if (isOK && (document.forms[appFormName].elements['First_Name'].value.length == 0)) {
          strErrMessage = "Sorry! Please insert a First Name";
          ShowRedMessage(strErrMessage,"First_Name","First_Name", error_holder);
          AddClientErrorStep2('First name');
          isOK = false;
        } else {
          document.forms[appFormName].elements['First_Name'].className = 'FormInputTextStep3';
        }

        // surname
        if (isOK && (document.forms[appFormName].elements['Surname'].value.length == 0)) {
          strErrMessage = "Sorry! Please insert a Surname";
          ShowRedMessage(strErrMessage,"Surname","Surname", error_holder);
          AddClientErrorStep2('Surname');
          isOK = false;
        } else {
          document.forms[appFormName].elements['Surname'].className = 'FormInputTextStep3';
        }

        // email
        if (isOK && (!checkemail(document.getElementById('Email').value))) {
          strErrMessage = "Sorry! Please insert a valid Email Address";
          ShowRedMessage(strErrMessage,"Email","Email", error_holder);
          AddClientErrorStep2('Email not valid: '+ document.getElementById('Email').value);
          isOK = false;
        } else {
          document.getElementById('Email').className = 'FormInputTextStep3';
        }

        // date of birth dd/mm/yyyy
        var dt = document.forms[appFormName].elements["dobmonth"].value + '/' + document.forms[appFormName].elements["dobday"].value + '/' + document.forms[appFormName].elements["dobyear"].value;

        if (isOK && (!isDate(dt))) {

          strErrMessage = "Sorry! Please insert a Date of Birth";
          ShowRedMessage(strErrMessage,"tdDOB","tdDOB", error_holder);
          AddClientErrorStep2('DOB');
          isOK = false;
          blDateError = true;
        } else {

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdDOB') {
              obj = arr[i];
              obj.className = 'FormSelectStep3';
            }
          }

        }

        var dtMin = new Date();
        var dtMax = new Date();
        dtMin.setYear(dtMin.getFullYear() - 18);
        dtMax.setYear(dtMax.getFullYear() - 1000);

        dt = new Date();
        dt.setDate(document.forms[appFormName].elements["dobday"].value);
        dt.setMonth(parseInt(document.forms[appFormName].elements["dobmonth"].value)-1);
        dt.setYear(document.forms[appFormName].elements["dobyear"].value);

        if (isOK && ((dt > dtMin) || (dt < dtMax))) {
          strErrMessage = "Sorry! Select and age between 18 and 100 years old";
          ShowRedMessage(strErrMessage,"tdDOB","tdDOB", error_holder);
          AddClientErrorStep2('DOB min - max');
          isOK = false;
        } else {
          if (!blDateError) {

            var arr = document.forms[appFormName].getElementsByTagName('TD');
            for(i = 0; i < arr.length; i++){
              if (arr[i].id == 'tdDOB') {
                obj = arr[i];
                obj.className = 'FormSelectStep3';
              }
            }

          }
        }
		        
		// Day phone
        if (isOK && (document.getElementById('Work_Phone').value.length < 10)) {
          strErrMessage = "Sorry! Please insert a valid Daytime phone number";
          ShowRedMessage(strErrMessage,"Work_Phone","Work_Phone", error_holder);
          AddClientErrorStep2('Day phone < 10: '+ document.getElementById('Work_Phone').value );
          isOK = false;
        } else {
          document.getElementById('Work_Phone').className = 'FormInputTextStep3';
        }

        // Home phone
        if (isOK && (document.getElementById('Home_Phone').value.length < 10)) {
          strErrMessage = "Sorry! Please insert a valid Evening phone number";
          ShowRedMessage(strErrMessage,"Home_Phone","Home_Phone", error_holder);
          AddClientErrorStep2('Home Phone < 10: ' + document.getElementById('Home_Phone').value);
          isOK = false;
        } else {
          document.getElementById('Home_Phone').className = 'FormInputTextStep3';
        }

        // postcode
        var oMTPSC = document.forms[appFormName].elements["Mortgage_Type"];
        if (oMTPSC.value != '9') {
          if (isOK && (!validatePostcode(document.forms[appFormName].elements['Postcode'].value))) {
            strErrMessage = "Sorry! Please insert a valid Postcode";
            ShowRedMessage(strErrMessage,"Postcode","Postcode", error_holder);
            AddClientErrorStep2('Postcode not valid: >'+ document.forms[appFormName].elements['Postcode'].value +'<');
            isOK = false;
          } else {
            document.forms[appFormName].elements['Postcode'].className = 'FormInputTextStep3';
          }
        }

        // address validation
        if (isOK && ((document.forms[appFormName].elements['ddlAddressList'].value == '') && ((document.forms[appFormName].elements['address_line1'].value == '') || (document.forms[appFormName].elements['town'].value == '') || (document.forms[appFormName].elements['county'].value == '')))) {
          strErrMessage = "Sorry!<br/>Address has to be filled<br/>Please click on the 'Fetch Address' button or fill Address, Town and County manually";
          ShowRedMessage(strErrMessage,"tdAddress","ddlAddressList", error_holder);
          AddClientErrorStep2('Address - not clicked ');
          isOK = false;
        } else {

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdAddress') {
              obj = arr[i];
              obj.className = 'FormSelectStep3';
            }
          }

        }

        // address line 1
        if (isOK && (document.forms[appFormName].elements['address_line1'].value == '')) {
          strErrMessage = "Sorry!<br/>Address 1 has to be filled. Please click on the Fetch Address button or fill Address manually";
          ShowRedMessage(strErrMessage,"tdaddress_line1","address_line1", error_holder);
          AddClientErrorStep2('Address 1');
          isOK = false;
        } else {

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdaddress_line1') {
              obj = arr[i];
              obj.className = 'FormSelectStep3';
            }
          }

        }

        // town
        if (isOK && (document.forms[appFormName].elements['town'].value == '')) {
          strErrMessage = "Sorry! Town has to be filled. Please click on the Fetch Address button or fill address manually";
          ShowRedMessage(strErrMessage,"tdTown","town", error_holder);
          AddClientErrorStep2('Town');
          isOK = false;
        } else {

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdTown') {
              obj = arr[i];
              obj.className = 'FormSelectStep3';
            }
          }
        }

        // county
        if (isOK && (document.getElementById('county').value == '')) {
          strErrMessage = "Sorry! County has to be filled. Please click on the Fetch Address button or fill address manually";
          ShowRedMessage(strErrMessage,"tdCounty","county", error_holder);
          AddClientErrorStep2('County');
          isOK = false;
        } else {
          document.getElementById('tdCounty').className = 'FormInputTextStep3';
        }


        // mortgage size
        obj = document.getElementById("Mortgage_Size");
        numb = parseInt(obj.value);
        if (isOK && ((numb == 'NaN') || (isNaN(numb)) || (numb < 25000) || (numb > 10000000))) {
          ShowRedMessage("Please correct the mortgage size<br/>It should be £25000 or more","Mortgage_Size","Mortgage_Size",error_holder);
          AddClientErrorStep2('Mortgage Size: '+ obj.value);
          isOK = false;
        } else {
          obj.className = 'FormInputText';
        }

        // winning broker
        obj = document.getElementById("sch_broker_id");
        obj2 = document.getElementById("sch_broker_msg");
        if (isOK && ((obj.value.length < 4) || (obj2.value.length < 50))) {
          AddClientErrorStep2('Broker id < 4 or ExpressConsent < 50');

          getBrokerFromIframe();
          document.getElementById("btnSubmit").value = 'Final confirm';

          isOK = false;
        }

        return isOK;
      }

      function isValidStep1(error_holder) {
        var numb, obj, objno, isOK = true;

        ClearRedMessage(error_holder);

        // property value
        obj = document.forms[appFormName].elements["Property_Value"];
        numb = parseInt(obj.value);
        if (isOK && ((numb == 'NaN') || (isNaN(numb)) || (obj.value < 25000) || (obj.value > 10000000))) {
          ShowRedMessage("Please correct the 'Value of Property'<br/>It should be £25000 or more<br/>","Property_Value","Property_Value",error_holder);
          AddClientErrorStep1('Property Value: ' + obj.value);
          isOK = false;
        } else {
          obj.className = 'FormInputText';
        }

        // mortgage size - first page
        obj = document.forms[appFormName].elements["Mortgage_Size"];
        numb = parseInt(obj.value);
        if (isOK && ((numb == 'NaN') || (isNaN(numb)) || (numb > 10000000))) {
            ShowRedMessage("Please correct the 'Value of Mortgage'<br/>It should be £25000 or more<br/>","Mortgage_Size","Mortgage_Size",error_holder);
            AddClientErrorStep1('Mortgage Size: '+obj.value);
            isOK = false;
        } else {
          if (numb < 25000) {

            if (confirm('Sorry! The amount you want to borrow is less than £25,000.\nThis is the minimum allowed for a mortgage.\n\nBut don\'t worry! You could get the money via a personal loan.\n\nIf you want a FREE no obligation loan quote - Click OK\n\nTo change your mortgage amount to more - Click Cancel')) {
              parent.location.href='http://www.loanfox.co.uk/personal_loans.php';
              isOK = false;
              return false;
            } else {
              ShowRedMessage("Please correct the 'Value of Mortgage'<br/>It should be £25000 or more<br/>","Mortgage_Size","Mortgage_Size",error_holder);
              AddClientErrorStep1('Mortgage Size: '+obj.value);
              isOK = false;
            }

          } else {
            obj.className = 'FormInputText';
          }
        }

        // mortgage type
        if (isOK && (document.forms[appFormName].elements["Mortgage_Type"].value == "")) {
          //document.forms[appFormName].all['mtype'].style.border="2px #FF0000 solid";
          ShowRedMessage("In the red box above<br/>Please select a 'Type of Mortgage'","tdMortgage_Type","Mortgage_Type", error_holder);
          AddClientErrorStep1('Mortgage Type');
          isOK = false;
        } else {

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdMortgage_Type') {
              obj = arr[i];
              obj.className = 'FormSelect';
            }
          }

        }

        // first time buyer
        obj = document.forms[appFormName].elements["ftbyes"];
        objno = document.forms[appFormName].elements["ftbno"];
        numb = document.forms[appFormName].elements["Mortgage_Type"].value;
        if (isOK && ((numb == '6') && (!obj.checked) && (!objno.checked))) {
          ShowRedMessage("Please select 'Yes' or 'No' for<br />Are you a first time buyer","tdFtb","ftb",error_holder);
          AddClientErrorStep1('FirstTimeBuyer');
          isOK = false;
        } else {
          //document.forms[appFormName].all['tdFtb'].className = 'FormRadio';

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdFtb') {
              obj = arr[i];
              obj.className = 'FormRadio';
            }
          }

        }

        // Other questions depending on the mortgage type
        obj = document.forms[appFormName].elements["fpyes"];
        objno = document.forms[appFormName].elements["fpno"];
        numb = document.forms[appFormName].elements["Mortgage_Type"].value;
        if (isOK && (((numb == '5') || (numb == '6')) && (!obj.checked) && (!objno.checked))) {
          ShowRedMessage("Please select 'Yes' or 'No' for<br />Have you found a property?","tdFp","fp",error_holder);
          AddClientErrorStep1('Found Property');
          isOK = false;
        } else {
          //document.forms[appFormName].all['tdFp'].className = 'FormRadio';
          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdFp') {
              obj = arr[i];
              obj.className = 'FormRadio';
            }
          }

        }

        // overseas
        if (isOK && ((document.forms[appFormName].elements["country"].value == "") && (document.forms[appFormName].elements["Mortgage_Type"].value == "9"))) {
          ShowRedMessage("Please select Country","tdCountry","country",error_holder);
          AddClientErrorStep1('Country');
          isOK = false;
        } else {
          //document.forms[appFormName].all['tdCountry'].className = 'FormSelect';
          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if (arr[i].id == 'tdCountry') {
              obj = arr[i];
              obj.className = 'FormSelect';
            }
          }

        }


        return isOK;
      }

      function pasteContent(val,nm){

        if (nm) {
          document.forms[appFormName].elements[nm].value = val;
        }

        // validation for loan to value, two fields, pass values to step1
        if (nm == 'Mortgage_Size') {
          var oMortgageSize = document.forms[appFormName].elements['Mortgage_Size'];
          oMortgageSize.value = val;
        }

        if (nm == 'Property_Value') {
          var oPropertyvalue = document.forms[appFormName].elements['Property_Value'];
          oPropertyvalue.value = val;
        }
      }

      function ShowRedMessage(strText, strObjId, strObjValueId, strErrorHolderId) {
        var objTemp;
        var strCaption = '';
		  // show or hide text box in error message
        var blTempTextField = false;
        var strNext = '';
        var strStep = '';
        var strAddBlur = '';
        var blLTV = false;

// if ((strObjValueId == 'Mortgage_Size') || (strObjValueId == 'ftb') || (strObjValueId == 'fp') || (strObjId == 'tdDOB') || (strObjId == 'tdAddress') || (strObjValueId == 'country')    ) {
        if ((strObjValueId == 'Mortgage_Size') || (strObjValueId == 'Property_Value')) {
          // hide text box in error message
          blTempTextField = true;
        }

        if (strErrorHolderId == 'ErrorHolderStep3') {
          //strNext = 'NextValidationSubmit';
          strStep = 'Step3';
        } else {
          //strNext = 'NextValidationStep2();';
          strStep = '';
        }

        if (strObjValueId == 'Postcode') {
          strAddBlur = 'ClearAddressBox(); getBrokerFromIframe();';
        }
		/* commented out by steve
        if (strObjValueId == 'Mortgage_Size') {
          strCaption = 'Mortgage Sizddde:';
        } */

        if ((strObjValueId == 'Mortgage_Size') && (strStep == 'Step3')) {
          blLTV = true;
        }

        // #########################################################
        if (blTempTextField) {

          var strOnlyNumbers = " onkeypress=\"return isNumberKey(event);\" ";
          var strOnlyNumbersPaste = " onkeypress=\"return isNumberKeyPaste(event,this.value,'"+strObjValueId+"');\" ";

          if (blLTV) {
            // ****************************************************
            // ************ LTV step 3 ****************************
			
			strCaption = 'Property Value: ';
            strObjId = 'Property_Value';
            objTemp = 'x' + strObjId;
            strText = strText + "<br /> "+strCaption+" <input style='width:100px;position:relative;left:2px;' type='text' class='FormInputText' name='" + objTemp + "' id='" + objTemp + "' ";
            strText = strText + strOnlyNumbers + " onChange=\"pasteContent(this.value,'"+strObjId+"')\" />";

            strCaption = 'Mortgage Value: ';
            strObjId = 'Mortgage_Size';
            objTemp = 'x' + strObjId;
            strText = strText + "<br /> "+strCaption+"<input style='width:100px;' type='text' class='FormInputText' name='" + objTemp + "' id='" + objTemp + "' ";
            strText = strText + strOnlyNumbers + " onChange=\"pasteContent(this.value,'"+strObjId+"')\" /><br/><br/>";

   
            objTemp = 'xMortgage_Size';

            // ****************************************************
          } else {
            objTemp = 'x' + strObjId;
            strText = strText + " "+strCaption+" <input type='text' class='FormInputText' name='" + objTemp + "' id='" + objTemp + "' ";

            if ((strObjValueId == 'Mortgage_Size') || (strObjValueId == 'Property_Value')) {
              strText = strText + strOnlyNumbers;
            } else {
              strText = strText + " onkeyup=\"pasteContent(this.value,'"+strObjValueId+"')\" ";
            }

            strText = strText + " onChange=\"pasteContent(this.value,'"+strObjId+"')\" onblur=\"javascript: " + strAddBlur + ' ' + strNext + "\" />";
          }

        }

//        alert(strText);

        //document.forms[appFormName].all[strErrorHolderId].innerHTML = strText;

        var arr = document.forms[appFormName].getElementsByTagName('DIV');
        for(i = 0; i < arr.length; i++){
          if (arr[i].id == strErrorHolderId) {
            obj = arr[i];
            obj.innerHTML = strText;
          }
        }


        //document.forms[appFormName].all['tr' + strErrorHolderId].style.display = '';
        var arr = document.forms[appFormName].getElementsByTagName('TR');
        for(i = 0; i < arr.length; i++){
          if (arr[i].id == 'tr' + strErrorHolderId) {
            obj = arr[i];
            obj.style.display = '';
          }
        }


        if (blTempTextField) {
          document.forms[appFormName].elements[objTemp].focus();

          document.forms[appFormName].elements[objTemp].focus();
        }


        // ##############################################################################
        // class
        if ((strObjId == 'tdFp') || (strObjId == 'tdFtb')) {
          //document.forms[appFormName].all[strObjId].className = 'FormRadioError'+strStep;

          var arr = document.forms[appFormName].getElementsByTagName('TD');
          for(i = 0; i < arr.length; i++){
            if ((arr[i].id == strObjId)) {
              obj = arr[i];
              obj.className = 'FormRadioError'+strStep;
            }
          }

        } else {
          if ((strObjId == 'tdMortgage_Type') || (strObjId == 'tdDOB')) {
            //document.forms[appFormName].all[strObjId].className = 'FormSelectError'+strStep;

            var arr = document.forms[appFormName].getElementsByTagName('TD');
            for(i = 0; i < arr.length; i++){
              if ((arr[i].id == strObjId)) {
                obj = arr[i];
                obj .className = 'FormSelectError'+strStep;
              }
            }


          } else {

            if ((strObjId == 'tdAddress') || (strObjId == 'tdCountry')) {
              // show address fields?

              var arr = document.forms[appFormName].getElementsByTagName('TD');
              for(i = 0; i < arr.length; i++){
                if (arr[i].id == strObjId) {
                  obj = arr[i];
                  obj.className = 'FormInputTextError'+strStep;
                }
              }

            } else {
              document.forms[appFormName].elements[strObjId].className = 'FormInputTextError'+strStep;
            }

          }
        }

      }


      function setBadCreditMax(intMax) {
        var blRes = true;
        var bc = document.getElementById("bad_credit");

        bc.value = '0';

        for (var a=1;a<intMax;a++) {
          if (document.getElementById("bc"+a).checked) {
            bc.value = '1';
          }
        }
      }


      function setBadCredit() {
        var blRes = true;
        var bc = document.getElementById("bad_credit");

        bc.value = '0';

        for (var a=1;a<7;a++) {
          if (document.getElementById("bc"+a).checked) {
            bc.value = '1';
          }
        }
      }

      function ClearRedMessage(strErrorHolderId) {
        //document.forms[appFormName].all[strErrorHolderId].innerHTML = '';
        var arr = document.forms[appFormName].getElementsByTagName('DIV');
        for(i = 0; i < arr.length; i++){
          if (arr[i].id == strErrorHolderId) {
            obj = arr[i];
            obj.innerHTML = '';
          }
        }


        //document.forms[appFormName].all['tr' + strErrorHolderId].style.display = 'none';
        var arr = document.forms[appFormName].getElementsByTagName('TR');
        for(i = 0; i < arr.length; i++){
          if (arr[i].id == 'tr' + strErrorHolderId) {
            obj = arr[i];
            obj.style.display = 'none';
          }
        }

      }

