

/*
 *Javascript Functions 
 */
function setAction(form, action){
    document.getElementById(form).action.value = action;
}

function checkAll(name) {
    for (i=0; i<document.getElementsByName(name).length; i++) {
        document.getElementsByName(name)[i].checked=true;
    }
}

function changeFeaturedAds(delay,advert_start,advert_amount,show_flag,country_code,parish_state_id) {
    setTimeout("makeRequest('/more-featured-ads/','?advert_start="+advert_start+"&advert_amount="+advert_amount+"&show_flag="+show_flag+"&country_code="+country_code+"&parish_state_id="+parish_state_id+"','featuredImageWebPart')",delay);
}

function loadHomePageItems() {
    setTimeout("makeRequest('/webpart/fiwiclassifieds_logo.php','','FiWiClassifiedsLogoWebPart')",1000);
}

/*Clear field on click*/
function clickclear(field, defval) {
    if (field.value == defval){
        field.value = '';
        field.style.fontStyle="normal";
        field.style.color="#333333";
    }
}

/*Set default value when field is empty*/
function restoredefault(field, defval){
    if(field.value==''){
        field.value=defval;
        field.style.fontStyle="italic";
        field.style.color="#888888";
    }
}

function showAdvertDetails(objectId){
    /*title stores category id from category dropdown*/
    
    obj = document.getElementById(objectId);
    
    if(obj.value==1 || obj.value==3 || obj.value==129 || obj.value==130){
        showElement("vehicleInfoWebPart");
    }else{
        hideElement("vehicleInfoWebPart");
    }

    if(obj.title==5){
        showElement("propertyInfoWebPart");
    }else{
        hideElement("propertyInfoWebPart");
    }
    
    if( obj.title==1 || obj.title==2 || obj.title==6 || obj.title==13){
        showElement("conditionWebPart");
    }else{
        document.getElementsByName("condition")[0].options[0].selected = true;
        hideElement("conditionWebPart");
    }
}

/* use navigation webPart to extend browse category*/
function browseSwitch(){
    
    var bgPos = document.body.style.backgroundPosition;
    var navBrowse = document.getElementById("nav_browse");

    if((bgPos == "0px -962px" || bgPos == "") && navBrowse != null){
        makeRequest("/webpart/nav/browse_more.php","","navigationWebPart");
        document.body.style.backgroundPosition="0px -502px";
        document.getElementById("browse_icon").style.backgroundPosition = "-555px -90px";
    }else{
        makeRequest("/webpart/nav/browse_less.php","","navigationWebPart");
        document.body.style.backgroundPosition = "0px -962px";
        document.getElementById("browse_icon").style.backgroundPosition = "-555px -31px";
    }
    
}

function hideElement(objectId){
    document.getElementById(objectId).style.display = "none";
}

function showElement(objectId){
    document.getElementById(objectId).style.display = "block";
}

function showElementInline(objectId){
    document.getElementById(objectId).style.display = "inline";
}

function radioShowHide(objectId,objectRadio){
    if (document.getElementsByName(objectRadio)[0].checked){
        document.getElementById(objectId).style.display = "block";
    }else{
        document.getElementById(objectId).style.display = "none";
    }
}

/*Count the number of character and display it in box*/
function charcount(field, maxlimit) {
    var cnt = field.value.length;
    if (cnt > maxlimit)                                                         /* if too long trim text*/
        field.value = field.value.substring(0, maxlimit);
    else{
        document.getElementById("count").value = "Count: "+cnt;
    }
}

function createSearchURL() {

    var url = "/";

    /* If Region is selected the create sub folder */
    if(document.getElementById("subdomain_type").value == "country"){
        if(document.getElementById("parish_state").value.length > 0){
            url = url + "local-" + document.getElementById("parish_state").value +  "-ads/";
        }
    }else{
        if(document.getElementById("city_town").value.length > 0){
            url = url + "local-" + document.getElementById("city_town").value +  "-ads/";
        }
    }
    if(document.getElementById("category").value.length > 0){
        url = url + document.getElementById("category").value + "/";
        if(document.getElementById("sub_category").value.length > 0){
            url = url + document.getElementById("sub_category").value + "/";
        }
    }

    if(url == "/"){
        url = "/ads/";
    }
    
    if(document.getElementById("search_value").value.length > 0)
        url = url + "?q=" + escape(document.getElementById('search_value').value);


    window.location = url;

    return false;
}


/*
FormatTelNo method takes in the current element of the form and formats the phone number
in "(123 456-7890" format. This method should be fired on every key entry
(using onKeyUp method) in the current element of the form. If any key other than 0 to 9
is entered, it erases that entry rightaway. Maxlength and size of the current
element of the form should be 13.
Eg. <netui:textBox size="13" maxlength="13" onKeyUp="javascript:formatTelNo (this);" onBlur="javascript:checkTelNo (this);" onKeyDown="javascript:formatTelNo (this);"/>
 */
function formatTelNo (telNo){
    /* If it's blank, save yourself some trouble by doing nothing.*/
    if (telNo.value == "") return;

    var phone = new String (telNo.value);

    phone = phone.substring(0,14);

    if (phone.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null){
        if (phone.match (".[0-9]{2}.[0-9]{3}-[0-9]{4}|" + ".[0-9].[0-9]{3}-[0-9]{4}|" +
            ".[0-9]{3}.[0-9]{2}-[0-9]{4}|" + ".[0-9]{3}.[0-9]-[0-9]{4}") == null){
            var phoneNumeric = phoneChar = "", i;
            for (i=0;i<phone.length;i++){
                phoneChar = phone.substr (i,1);
                if (!isNaN (phoneChar) && (phoneChar != " ")) phoneNumeric = phoneNumeric + phoneChar;
            }

            phone = "";
            for (i=0;i<phoneNumeric.length;i++){
                if (i == 0) phone = phone + "(";
                if (i == 3) phone = phone + ") ";
                if (i == 6) phone = phone + "-";
                phone = phone + phoneNumeric.substr (i,1);
            }
        }
    }
    else {
        phone = "(" + phone.substring (1,4) + ") " + phone.substring (5,8) + "-" + phone.substring(9,13);
    }
    if (phone != telNo.value) telNo.value = phone;
}

/*
This method should be fired as the user attempts to leave the current element in the form.
It checks to see if the format of the phone is "(123) 456-7890".
 */
function checkTelNo (telNo){
    if (telNo.value == "") return;
    if (telNo.value.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null){
        if (telNo.value.match ("[0-9]{10}") != null)
            formatTelNo (telNo);
    }
}

/*Enter-listener*/
/*if the user presses ENTER KEY submit the Form*/
/*Added by Guruprasad Thorvey on 03/23/05 to fix issue #21632*/
function checkEnterForFindListing(e){
    var characterCode;

    if(e && e.which){
        e = e;
        characterCode = e.which ;
    }
    else{
        e = event;
        characterCode = e.keyCode;
    }

    if(characterCode == 13){ /*13 = the code for pressing ENTER*/
        document.forms[getNetuiTagName("findListingForm")].submit();
        return false;
    }
    else{
        return true ;
    }

}

function numberDollar(number) {
    if(number==''){
        return number;
    }
    return '$'+numberComma(number);
}

function numberComma(number) {
    number = '' + number;
    if (number.length > 3) {
        var mod = number.length % 3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0))
                output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
            else
                output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
        return (output);
    }
    else return number;
}

function numberDot(number) {
    if(number!=''){
        number = '' + number;
        return (number.substring(0,(number.length-1))+'.'+number.substring((number.length-1),number.length));
    }
    return number;

}