﻿//Error box popup
var ValidMinSalary, ValidMaxSalary, ValidLocation;
var IsAllFieldsEmpty = true;
var ValidSalaryOrder = true;

function OnSearch(jobSearchFields, EmptyFieldErrorPlaceHolder, SalaryErrorPlaceHolder, LocationErrorPlaceHolder, LoadingDiv, SearchForm) {
    try {
        ClearPlaceHolders(EmptyFieldErrorPlaceHolder, SalaryErrorPlaceHolder, LocationErrorPlaceHolder);

        CleanKeywords(jobSearchFields);
        
        if (CheckIfAllFieldsAreEmpty(jobSearchFields)) {
            document.getElementById(EmptyFieldErrorPlaceHolder).innerHTML = "<div style=\"display: inline; opacity: 1;\" class=\"errorPopup\" id=\"errorBox\"><div class=\"heading\"><h3>Note:<\/h3><span><a href=\"Javascript:CloseErrorBox('errorBox')\">Close<\/a><\/span><\/div><div class=\"errorMessage\"><b>Please ensure you have entered some search criteria.<\/b><\/div><\/div>";
            return false;
        }

        minSalary = CleanSalary(document.getElementById(jobSearchFields.MinSal).value, jobSearchFields.SalFromText);
        document.getElementById(jobSearchFields.MinSal).value = minSalary;

        maxSalary = CleanSalary(document.getElementById(jobSearchFields.MaxSal).value, jobSearchFields.SalToText);
        document.getElementById(jobSearchFields.MaxSal).value = maxSalary;

        ValidMinSalary = ValidateSalary(minSalary, null, null, jobSearchFields.SalFromText);
        ValidMaxSalary = ValidateSalary(maxSalary, null, null, jobSearchFields.SalToText);

        if (ValidMaxSalary && ValidMinSalary)
            ValidSalaryOrder = ValidateSalaryOrder(maxSalary, minSalary, jobSearchFields.SalToText, jobSearchFields.SalFromText);

        ValidLocation = ValidateLocation(jobSearchFields, EmptyFieldErrorPlaceHolder, SalaryErrorPlaceHolder, LocationErrorPlaceHolder, LoadingDiv, SearchForm); //this will force a async call to validation the location on the server - and the listener will process the result

        //Valid location - but invalid salary
        if (ValidLocation && !(ValidMinSalary && ValidMaxSalary && ValidSalaryOrder)) {
            ErrorPopup = new ErrorBox("errorBox", document.getElementById(SalaryErrorPlaceHolder), !ValidMaxSalary, !ValidMinSalary, !ValidSalaryOrder, false, false, document.getElementById(jobSearchFields.Location));
            ErrorPopup.Display();
            return false;
        }

        //Everything's valid
        else if (ValidLocation && ValidMinSalary && ValidMaxSalary && ValidSalaryOrder)
            return true;
        else
            return ValidLocation;
    }
    catch (ex) { return true; }
}

function CleanKeywords(jobSearchFields) {
    document.getElementById(jobSearchFields.Keywords).value = document.getElementById(jobSearchFields.Keywords).value.replace('<', '').replace('>', '');
}

function ClearPlaceHolders(EmptyFieldErrorPlaceHolder, SalaryErrorPlaceHolder, LocationErrorPlaceHolder) {
    document.getElementById(EmptyFieldErrorPlaceHolder).innerHTML = "";
    document.getElementById(SalaryErrorPlaceHolder).innerHTML = "";
    document.getElementById(LocationErrorPlaceHolder).innerHTML = "";
}

function CheckIfAllFieldsAreEmpty(jobSearchFields) {
    if (document.getElementById(jobSearchFields.Keywords).value.length > 0)
        return false;
    if (document.getElementById(jobSearchFields.LangIds) != null) {
        if (document.getElementById(jobSearchFields.LangIds).options[document.getElementById(jobSearchFields.LangIds).selectedIndex].value.length > 0)
            return false;
    }
    if (document.getElementById(jobSearchFields.SectorIds).options[document.getElementById(jobSearchFields.SectorIds).selectedIndex].value.length > 0)
        return false;
    if (document.getElementById(jobSearchFields.Location).value.length > 0)
        return false;
    if (document.getElementById(jobSearchFields.MinSal).value.length > 0 && document.getElementById(jobSearchFields.MinSal).value != jobSearchFields.SalFromText)
        return false;
    if (document.getElementById(jobSearchFields.MaxSal).value.length > 0 && document.getElementById(jobSearchFields.MaxSal).value != jobSearchFields.SalToText)
        return false;
    if (document.getElementById(jobSearchFields.Perm).checked)
        return false;
    if (document.getElementById(jobSearchFields.Temp).checked)
        return false;
    if (document.getElementById(jobSearchFields.PartTime).checked)
        return false;
    if (document.getElementById(jobSearchFields.Contract).checked)
        return false;
    return true;
}

function ValidateLocation(jobSearchFields, EmptyFieldErrorPlaceHolder, SalaryErrorPlaceHolder, LocationErrorPlaceHolder, LoadingDiv, SearchForm) {
    var reg = /^\w{1,2}\d+/;
    var isValid = true;
    locationBox = document.getElementById(jobSearchFields.Location);

    if ((locationBox.value != '') && (!reg.test(locationBox.value))) {
        try {
            loadingDiv = document.getElementById(LoadingDiv);
            loadingDiv.style.display = "inline";
            listener = new TownsListener(loadingDiv, "errorBox", document.getElementById(SearchForm), document.getElementById(LocationErrorPlaceHolder), document.getElementById(SalaryErrorPlaceHolder), ValidMaxSalary, ValidMinSalary, ValidSalaryOrder, locationBox);
            TownProvider = new TownsProvider(listener);
            TownProvider.validate(locationBox.value);
            isValid = false;
        }
        catch (ex) { }
    }
    return isValid;
}

function LogSeacrhParams(jobSearchFields) 
{
    try 
    {
        var Max             = document.getElementById(jobSearchFields.MaxSal).value;
        var Min             = document.getElementById(jobSearchFields.MinSal).value;
        var Location        = document.getElementById(jobSearchFields.Location).value;
        var Keywords        = document.getElementById(jobSearchFields.Keywords).value;
        var ParentSector    = document.getElementById(jobSearchFields.SectorIds).options[0].text;
        var SubSector       = document.getElementById(jobSearchFields.SectorIds).options[document.getElementById(jobSearchFields.SectorIds).selectedIndex].text;
        var SearchTitleOnly = "";

        if (document.getElementById(jobSearchFields.TitleOnly).checked == true)
            SearchTitleOnly = "Yes";
        else
            SearchTitleOnly = "No";

        if (SubSector.length > 0) 
        {
            if (SubSector.indexOf('-') == 0) 
            {
                ParentSector    = ParentSector.substring(4, ParentSector.length);
                SubSector       = SubSector.substring(1, SubSector.length);
            }
            else
                ParentSector = SubSector;
        }
        else 
        {
            ParentSector    = "All sectors";
            SubSector       = "All roles";
        }

        var Proximity   = document.getElementById(jobSearchFields.LocationProx).options[document.getElementById(jobSearchFields.LocationProx).selectedIndex].text;
        var SalType     = document.getElementById(jobSearchFields.SalType).options[document.getElementById(jobSearchFields.SalType).selectedIndex].text;
        var JobType     = "";
        var addComma    = "";

        if (document.getElementById(jobSearchFields.Perm).checked) 
        {
            JobType += "Permenent";
            addComma = ",";
        }

        if (document.getElementById(jobSearchFields.Temp).checked) 
        {
            if (JobType.length > 0)
                addComma = ",";
            JobType += addComma + "Temporary";
            addComma = "";
        }
        
        if (document.getElementById(jobSearchFields.PartTime).checked) 
        {
            if (JobType.length > 0)
                addComma = ",";
            JobType += addComma + "PartTime";
            addComma = "";
        }

        if (document.getElementById(jobSearchFields.Contract).checked) 
        {
            if (JobType.length > 0)
                addComma = ",";
            JobType += addComma + "Contract";
            addComma = "";
        }

        dcsMultiTrack('DCS.dcsuri', '/job/search.html', 'DCSext.LogType', 'QuickSearch', 'DCSext.Keywords', Keywords, 'DCSext.MinSal', Min, 'DCSext.MaxSal', Max, 'DCSext.SalType', SalType, 'DCSext.LocationName', Location, 'DCSext.JobType', JobType, 'DCSext.Sector', ParentSector, 'DCSext.SubSector', SubSector, 'DCSext.Proximity', Proximity, 'DCSext.SearchTitleOnly', SearchTitleOnly);
    }
    catch (e) {}
}