var http_request = false;
var ApplicationID = "";
var AjaxReturnType = "";
var AjaxReturnId = "";
var ApplicationStarted = false;

function AjaxPost(url, parameters) {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    http_request.onreadystatechange = AjaxAlert;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
    if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
}

function AjaxAlert() {
    var StartInput;
    var EndInput;
    var Input;
    var Params;
    var Param;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            result = http_request.responseText;
            
            if (result.search(/APPID/) != -1) {
                
                StartInput = result.search(/APPID/) + 5;
                EndInput = result.search(/ENDAPPID/) - 5;
                
                Input = result.substr(StartInput, EndInput);
                document.getElementById("application_id").value = Input;
                
                result = result.substr(EndInput + 13);
            }
            
            if (result.search(/HIDIV/) != -1) {
                StartInput = result.search(/HIDIV/) + 5;
                EndInput = result.search(/ENDHIDIV/) - 5;
                
                Input = result.substr(StartInput, EndInput);
                document.getElementById(Input).style.display = "none";
                
                result = result.substr(EndInput + 13);
            }
            
            if (result.search(/PARAMS/) != -1) {
                StartInput = result.search(/PARAMS/) + 6;
                EndInput = result.search(/ENDPARAMS/) - 6;
                
                Input = result.substr(StartInput, EndInput);
                
                Params = Input.split("#");
                
                for (i=0; i < Params.length; i++) {
                    Param = Params[i].split("|");
                    document.getElementById("id_" + Param[0]).className = "form_input_error";
                    document.getElementById(Param[0] + "_error").innerHTML = Param[1];
                }
                
                result = result.substr(EndInput + 15);
            }
            
            if (AjaxReturnType == "div" && result != "") {
                document.getElementById(AjaxReturnId).innerHTML = result;
                document.getElementById(AjaxReturnId).style.display = "inline-block";
            }
            
            
            if (AjaxReturnType == "id"  && result != "") {
                document.getElementById(AjaxReturnId).value = result;
            }
            
            AjaxReturnType = "";
            AjaxReturnId = "";
        } else {
            alert('There was a problem with the request.');
        }
    }
}

function FindAddress(DivName, Action) {
    var PostString = "post_code=" + encodeURI(document.getElementById("post_code").value)
                    + "&action[]=" + Action;
    AjaxReturnType = "div";
    AjaxReturnId = DivName;
    AjaxPost("newform/address_search.php", PostString);
}

function SelectAddress(Address, DivName) {
    document.getElementById("address_error").style.display = "none";
    var PostString = "address=" + encodeURI(Address) +
                    "&application_id=" + encodeURI(document.getElementById("application_id").value) +
                    "&action[]=select";
    AjaxReturnType = "div";
    AjaxReturnId = DivName;
    AjaxPost("newform/address_search.php", PostString);
}

function AutoAddress(HouseName, HouseNumber, StreetName, District, CityTown, County, PostCode, PAddress, PHouseName, PHouseNumber, PStreetName, PDistrict, PCityTown, PCounty, PPostCode) {
    var PostString = "action[]=auto" +
                    "&house_name=" + encodeURI(HouseName) +
                    "&house_number=" + encodeURI(HouseNumber) +
                    "&street_name=" + encodeURI(StreetName) +
                    "&district=" + encodeURI(District) +
                    "&city_town=" + encodeURI(CityTown) +
                    "&county=" + encodeURI(County) +
                    "&post_code=" + encodeURI(PostCode) +
                    "&previous_address=" + encodeURI(PAddress) +
                    "&p_house_name=" + encodeURI(PHouseName) +
                    "&p_house_number=" + encodeURI(PHouseNumber) +
                    "&p_street_name=" + encodeURI(PStreetName) +
                    "&p_district=" + encodeURI(PDistrict) +
                    "&p_city_town=" + encodeURI(PCityTown) +
                    "&p_county=" + encodeURI(PCounty) +
                    "&p_post_code=" + encodeURI(PPostCode);
                    
    AjaxPost("newform/address.php", PostString);
}

function AddressForm() {
    var PostString = "application_id=" + encodeURI(document.getElementById("application_id").value);
    AjaxReturnType = "div";
    AjaxReturnId = "address";
    AjaxPost("newform/address.php", PostString);
}

function AddressTimeCheck(Years) {
    if (Years < 3) {
        document.getElementById("previous_address").style.display = "inline";
    }
}

function AddressSave() {
    //alert("hello");
    var PostString = "action[]=save" +
                "&house_name=" + encodeURI(document.getElementById("house_name").value) +
                "&house_number=" + encodeURI(document.getElementById("house_number").value) +
                "&street_name=" + encodeURI(document.getElementById("street_name").value) +
                "&district=" + encodeURI(document.getElementById("district").value) +
                "&city_town=" + encodeURI(document.getElementById("city_town").value) +
                "&county=" + encodeURI(document.getElementById("county").value) +
                "&post_code=" + encodeURI(document.getElementById("post_code").value) +
                "&accomodation_type=" + encodeURI(document.getElementById("accomodation_type").value) +
                "&time_address_years=" + encodeURI(document.getElementById("time_address_years").value) +
                "&time_address_months=" + encodeURI(document.getElementById("time_address_months").value) +
                "&application_id=" + encodeURI(document.getElementById("application_id").value);

    document.getElementById("address_error").style.display = "none";

   /* if (document.getElementById("time_address_years").value == "0" &&
        document.getElementById("time_address_months").value == "0") {
        document.getElementById("id_time_address").className = "form_input_error";
        document.getElementById("id_time_address").style.border = "1px solid red";
        document.getElementById("time_address_error").innerHTML = "You must select how long you have been at the address";
    } else {*/
        AjaxReturnType = "div";
        AjaxReturnId = "address";
        AjaxPost("newform/address_save.php", PostString);
    //}
}

function StartApplication() {
    AjaxReturnType = "id";
    AjaxReturnId = "application_id";
    AjaxPost("newform/application.php", "actions[]=new");
    ApplicationStarted = true;
}

function PostCheck(DivName) {
    //AddressSave();//kham added this
    var PostString = "";
    var SendUpdate = false;
    var UpdateCount = 0;
    
    if (document.getElementById("application_id").value == "new") {
        PostString += "actions[]=new";
    } else {
        PostString += "application_id=" + document.getElementById("application_id").value;
        //AjaxReturnType = "div";
        //AjaxReturnId = "floating_errors";
    }
    PostString += "&actions[]=update";
    
    if (DivName == "confirm_contact") {
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&confirm_contact=" + document.getElementById("confirm_contact").value +
                    "&update[]=confirm_contact" +
                    "&home_phone=" + encodeURI(document.getElementById("home_phone").value) +
                    "&mobile_phone=" + encodeURI(document.getElementById("mobile_phone").value) +
                    "&email_address=" + encodeURI(document.getElementById("email_address").value) +
                    //"&confirm_email_address=" + encodeURI(document.getElementById("confirm_email_address").value) +
                    "&update[]=contact_details";
    }
    
    if (DivName == "contact_details" &&
        (document.getElementById("home_phone").value != "" ||
        document.getElementById("mobile_phone").value != "") &&
        document.getElementById("email_address").value != "") {
        //document.getElementById("confirm_email_address").value != "") {
        SendUpdate = true;
        UpdateCount++;
    
        PostString += "&home_phone=" + encodeURI(document.getElementById("home_phone").value) +
                    "&mobile_phone=" + encodeURI(document.getElementById("mobile_phone").value) +
                    "&email_address=" + encodeURI(document.getElementById("email_address").value) +
                    //"&confirm_email_address=" + encodeURI(document.getElementById("confirm_email_address").value) +
                    "&update[]=contact_details";
    }
    
    if (DivName == "iframe_apply" &&
        document.getElementById("title").value != "") {
        SendUpdate = true;
        UpdateCount++;

        PostString += "&title=" + encodeURI(document.getElementById("title").value) +
                      "&first_name=" + encodeURI(document.getElementById("first_name").value) +
                      "&last_name=" + encodeURI(document.getElementById("last_name").value) +
                      "&home_phone=" + encodeURI(document.getElementById("home_phone").value) +
                      "&mobile_phone=" + encodeURI(document.getElementById("mobile_phone").value) +
                      "&email_address=" + encodeURI(document.getElementById("email_address").value) +
                      "&dob_day=" + encodeURI(document.getElementById("dob_day").value) +
                      "&dob_month=" + encodeURI(document.getElementById("dob_month").value) +
                      "&dob_year=" + encodeURI(document.getElementById("dob_year").value) +
                      "&loan_purpose=" + encodeURI(document.getElementById("loan_purpose").value) +
                      "&loan_amount=" + encodeURI(document.getElementById("loan_amount").value) +
                      "&update[]=iframe_apply";
    }

    
    
    if (DivName == "personal_details" &&
        document.getElementById("title").value != "" &&
        document.getElementById("first_name").value != "" &&
        document.getElementById("last_name").value != "" &&
        document.getElementById("dob_day").value != "" &&
        document.getElementById("dob_month").value != "" &&
        document.getElementById("dob_year").value != "") {
        /*&&
        document.getElementById("marital_status").value != "" &&
        document.getElementById("joint_application").value != "" &&
        document.getElementById("number_of_dependants").value != "") {*/
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&title=" + encodeURI(document.getElementById("title").value) +
                    "&first_name=" + encodeURI(document.getElementById("first_name").value) +
                    "&last_name=" + encodeURI(document.getElementById("last_name").value) +
                    "&dob_day=" + encodeURI(document.getElementById("dob_day").value) +
                    "&dob_month=" + encodeURI(document.getElementById("dob_month").value) +
                    "&dob_year=" + encodeURI(document.getElementById("dob_year").value) +
                   /* "&marital_status=" + encodeURI(document.getElementById("marital_status").value) +
                    "&joint_application=" + encodeURI(document.getElementById("joint_application").value) +
                    "&number_of_dependants=" + encodeURI(document.getElementById("number_of_dependants").value) +*/
                    "&update[]=personal_details";
    }
    
     if (DivName == "address") {
        //AddressSave();//kham added this
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&update[]=address";
        //AddressSave();//kham added this
    }
    
       
    
    if (DivName == "employment_details" &&
        document.getElementById("employment_type").value != "" &&
        document.getElementById("monthly_income").value != "" &&
        document.getElementById("job_title").value != "" &&
        document.getElementById("company_name").value != "" &&
        document.getElementById("time_employer_years").value != "" &&
        document.getElementById("time_employer_months").value != "" &&
        document.getElementById("hours").value != "") {
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&employment_type=" + encodeURI(document.getElementById("employment_type").value) +
                    "&monthly_income=" + encodeURI(document.getElementById("monthly_income").value) +
                    "&job_title=" + encodeURI(document.getElementById("job_title").value) +
                    "&company_name=" + encodeURI(document.getElementById("company_name").value) +
                    "&time_employer_years=" + encodeURI(document.getElementById("time_employer_years").value) +
                    "&time_employer_months=" + encodeURI(document.getElementById("time_employer_months").value) +
                    "&hours=" + encodeURI(document.getElementById("hours").value) +
                    "&update[]=employment_details";
    
    }
    
    if (DivName == "financial_details" &&
        document.getElementById("cheque_card").value != "") {
        
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&cheque_card=" + encodeURI(document.getElementById("cheque_card").value) +
                    "&update[]=financial_details";
    }
    
    if (DivName == "loan_details" &&
        document.getElementById("loan_purpose").value != "" &&
        document.getElementById("loan_amount").value != "") {
        
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&loan_purpose=" + encodeURI(document.getElementById("loan_purpose").value) +
                    "&loan_amount=" + encodeURI(document.getElementById("loan_amount").value) +
                    "&update[]=loan_details";
    }
    
    if (DivName == "accept_agreement") {
        SendUpdate = true;
        UpdateCount++;
        
        PostString += "&accept_agreement=" + encodeURI(document.getElementById("accept_agreement").value) +
                    "&update[]=accept_agreement";
    }
    
    if (SendUpdate == true) {
        PostString += "&sequence=" + document.getElementById("sequence").value;
        document.getElementById("sequence").value = parseInt(document.getElementById("sequence").value) + UpdateCount;
        
        AjaxPost("newform/application.php", PostString);
    }
    
}

function NoEnter() {
    if (window.event && window.event.keyCode == 13) {
        FindAddress('address_search','search');
        window.event.keyCode = 0;
    }
    return !(window.event && window.event.keyCode == 13);
}
