/* global variables used to indicate which browser is
   being used to view this page */
var useDOM  = document.getElementById ? 1 : 0;
var isIE    = document.all ? 1 : 0;
var isNS    = (!useDOM && navigator.appName == 'Netscape') ? 1 : 0;
//var isDHTML = useDOM || isIE || isNS;
var isWin   = (navigator.platform.indexOf('Win') != -1) ? 1 : 0;

/* will return a reference to an object on the page */
function getObject ( objectID ) {
    var objectRef = null;

    if (useDOM) {
        objectRef = document.getElementById(objectID);
    } else if (isIE) {
        objectRef = eval ('document.all.' + objectID);
    } else if (isNS) {
        objectRef = eval ('document.' + objectID);
    }

    return (objectRef);
}

function check_form( formObj) {}

/* will return a reference to the style object of an object
   based on the object id */
function getStyleObject ( objectID) {
    var objectRef = getObject(objectID);
    var styleRef  = null;

    if (objectRef == null) {
        // do nothing, there isn't any point
    } else if (useDOM || isIE) {
        styleRef = objectRef.style;
    } else {
        styleRef = objectRef;
    }

    return(styleRef);
}

/* changes the visibility of an object to hidden */
function hideObject ( objectID ) {
    var objectStyleRef = getStyleObject(objectID);
    objectStyleRef.visibility = 'hidden';
}

/* changes the visibility of an object to visible */
function showObject ( objectID ) {

     var objectStyleRef = getStyleObject(objectID);
		 if(objectStyleRef != undefined)
     {
     	objectStyleRef.visibility = 'visible';
     }
}

/* activates and deactivates tabs based on the id of
   the div for that tab */
function showTab ( objectID, codesetKey, firstTab, lastTab ) {
    for (i = 1; getObject('tab' + i); i++) {
        hideObject('tab' + i);
        var imageSrc = eval ('document.image_tab' + i + '.src');
        imageSrc = stripString(imageSrc, 'highlight_');
        swapImage('image_tab' + i, imageSrc);
        changeBackground('td_tab' + i, "#dedede");
    }

    /* turn left curve of tabs back to grey */
       changeBackground("tab_left_curve", "#dedede");

    /* turn right cuve of tabs back to grey */
       swapImage("right_curve", imageURL + "/images/right_curve.gif");

    showObject(objectID);
    swapImage("image_" + objectID ,imageURL + "/images/highlight_" + codesetKey + "_tab.gif");
    changeBackground("td_" + objectID, "#e1d7cd");

    /* flip left or right curve of tabs */
    if (objectID == firstTab) {
        changeBackground("tab_left_curve", "#e1d7cd");
    } else if (objectID == lastTab) {
        swapImage("right_curve", imageURL + "/images/right_curve2.gif");
    }
 }

 /* removes all instances of removalString that appear in
  * originalString
  */
function stripString ( originalString, removalString) {
    var returnString = originalString;

    removalStringIndex = originalString.indexOf(removalString)
    while (removalStringIndex != -1) {
        returnString = originalString.substr(0, removalStringIndex);
        removalStringIndex = removalStringIndex + removalString.length;
        returnString += originalString.substr(removalStringIndex, originalString.length)

        originalString = returnString;
        removalStringIndex = originalString.indexOf(removalString)
    }

    return (returnString);
 }


/* changes the background color of an object */
function changeBackground ( objectID, colorValue ) {
    styleObject = getStyleObject(objectID);
    if(styleObject != undefined)
    {
            styleObject.background = colorValue;
    }
}

/*
function swapImage(imageName, imageSRC) {
    imageObj = eval("document." + imageName);
    imageObj.src = imageSRC;
}
*/

function swapImage(imageName, imageSRC) {
    var i;
    for (i = 0; i < document.images.length; i++) {
        if (document.images[i].name.toUpperCase() == imageName.toUpperCase()) {
                    imageObj = document.images[i];
                    // i = document.images.length;
								    imageObj.src = imageSRC;
                    break;

        }
    }

}



/*
 * cjohnson - tokenizes a string based on delimiters present in tokens string
 * multiple sequential delimiters are ignored
 */
function tokenize(tokenString) {
    var arrayIndex = 0;
    var foundChar = false;
    var tokens = new Array();

    for (i = 0; i < tokenString.length; i++) {
        if (' -/.\\'.indexOf(tokenString.charAt(i)) != -1) {
            if (foundChar) {
                arrayIndex++;
                foundChar = false;
            }
        } else {
            if (tokens[arrayIndex]) {
                tokens[arrayIndex] += tokenString.charAt(i);
            } else {
                tokens[arrayIndex] = tokenString.charAt(i);
            }
            foundChar = true;
        }
    }
    return (tokens)
}

/*
 * cjohnson - tries to convert just about anything into a sql-style date (dd-mon-yyyy)
 * no zero padding on day
 */
function verifyDate (formObj) {
    var dateError = false;
    var dateErrorMsg = '';
    var dateArray = tokenize(formObj.value);
    var monthString = '';
    var month = 0;
    var day = 0;
    var year;

    if (dateArray.length < 2 || dateArray.length > 3) {
        dateError = true;
        dateErrorMsg = 'Invalid number of date pieces found in your date\n';
    }

    /* if there is some text in the date, see if it is a month abbreviation */
    if ( isLetter(dateArray[0].charAt(0)) ) {
        monthString = dateArray[0];
    }

    if (dateArray.length > 1) {
        if ( isLetter(dateArray[1].charAt(0)) ) {
            if (monthString != '') {
                dateError = true;
                dateErrorMsg += 'Date is unverifiable\n';
            } else {
                monthString = dateArray[1];
            }
        }
    }

    /* if there is no text in date, assume the month is first, otherwise, assume day is first */
    if (monthString == '') {
        while (dateArray[0].charAt(0) == '0') {
            dateArray[0] = dateArray[0].substring(1, dateArray[0].length);
        }
        month = parseInt(dateArray[0]);
            if (dateArray.length > 1) {
                while (dateArray[1].charAt(0) == '0') {
                    dateArray[1] = dateArray[1].substring(1, dateArray[1].length);
                }
                day = parseInt(dateArray[1]);
            }
    } else {
        switch (monthString.toLowerCase()) {
            case 'jan' :
                month = 1;
                break;
            case 'feb' :
                month = 2;
                break;
            case 'mar' :
                month = 3;
                break;
            case 'apr' :
                month = 4;
                break;
            case 'may' :
                month = 5;
                break;
            case 'jun' :
                month = 6;
                break;
            case 'jul' :
                month = 7;
                break;
            case 'aug' :
                month = 8;
                break;
            case 'sep' :
                month = 9;
                break;
            case 'oct' :
                month = 10;
                break;
            case 'nov' :
                month = 11;
                break;
            case 'dec' :
                month = 12;
                break;
             default :
                month = 0;
                break;
        }
        day = parseInt(dateArray[0]);
    }

    var daysInMonth = new Array(13);
    daysInMonth[1]  = 31;
    daysInMonth[2]  = 29;
    daysInMonth[3]  = 31;
    daysInMonth[4]  = 30;
    daysInMonth[5]  = 31;
    daysInMonth[6]  = 30;
    daysInMonth[7]  = 31;
    daysInMonth[8]  = 31;
    daysInMonth[9]  = 30;
    daysInMonth[10] = 31;
    daysInMonth[11] = 30;
    daysInMonth[12] = 31;

    var monthAbbreviations = new Array(13);
    monthAbbreviations[1]  = 'jan';
    monthAbbreviations[2]  = 'feb';
    monthAbbreviations[3]  = 'mar';
    monthAbbreviations[4]  = 'apr';
    monthAbbreviations[5]  = 'may';
    monthAbbreviations[6]  = 'jun';
    monthAbbreviations[7]  = 'jul';
    monthAbbreviations[8]  = 'aug';
    monthAbbreviations[9]  = 'sep';
    monthAbbreviations[10] = 'oct';
    monthAbbreviations[11] = 'nov';
    monthAbbreviations[12] = 'dec';

    var todaysDate = new Date();

    if ((month > 12) || (month < 1) || (day > daysInMonth[month]) || (day < 1)) {
        dateError = true;
        dateErrorMsg += 'Invalid month or day found\n';
    }

    if (dateArray.length == 3) {
        var year = dateArray[2];
    } else {
        year = '';
    }

    if (year.length == 0) {
        year = todaysDate.getYear();
        if (year < 1000) {
            year += 1900;
        }
    } else if (year.length == 1) {
        year = "200" + year;
    } else if (year.length == 2) {
        year = "20" + year;
    } else if ((year.length == 3) || (year.length > 4)) {
        dateError = true;
        dateErrorMsg += 'Something wrong with the year\n';
    }

    if (isNaN(month) == true) {
        dateError = true;
        dateErrorMsg += 'Month is not a valid number\n';
    }

    if (isNaN(day) == true) {
        dateError = true;
        dateErrorMsg += 'Day is not a valid number\n';
    }

    if (isNaN(year) == true) {
        dateError = true;
        dateErrorMsg += 'Year is not a valid number\n';
    }

    if (month == 2) {
        if (((year % 4) != 0) && ((year % 400) != 0)) {
            if (day > 28) {
                dateError = true;
                dateErrorMsg += 'Day is out of range for month';
            }
        }
    }

    if (dateError == true) {
        alert ("A date is invalid or in improper format. Please type date in mm/dd/yyyy format\n" + dateErrorMsg);
        return false;
    } else {
        formObj.value = day + '-' + monthAbbreviations[month] + '-' + year;
        return true;
    }
}


function submitForm(idx) {
    if (validateForm(document.forms[idx])) {
        document.forms[idx].submit();
    }
}

var requiredFields = new Array();
requiredFields[0] = 'classifieds.ad.feature_value(SAMPLE_TEXT_FEATURE)';
requiredFields[1] = 'classifieds.ad.feature_value(SAMPLE_TEXTAREA_FEATURE)';
requiredFields[2] = 'classifieds.ad.feature_value(SAMPLE_SELECT_FEATURE)';
requiredFields[3] = 'classifieds.ad.feature_value(SAMPLE_SELECTMULTIPLE_FEATURE)';

var dateFields = new Array();

var errorMessages = new Array();
errorMessages[0] = 'Please fill in a value for contact agent id';
errorMessages[1] = 'Please provide a description';
errorMessages[2] = 'Please choose a broker';
errorMessages[3] = 'Please indicate documents on file';

/*
 * cjohnson - makes sure any form elements with names that match names in requiredFields
 * array are not blank or have something selected for select objects
 */
function validateForm (formObj) {
    for (var i = 0; i < formObj.elements.length; i++) {
        for (var j = 0; j < requiredFields.length; j++) {
            if (requiredFields[j] == formObj.elements[i].name) {
                /* if element is text field or textarea and it can't be blank, first see if it is blank,
                 * if so return false, otherwise, get rid of double spaces and leading spaces then check
                 * again to see if it is blank
                 */
                if (isEmptyElement(formObj.elements[i])) {
                    alert (errorMessages[j]);
                    return false;
                }

                /* see if field should be filled in with a date and check date format */
                for (var k = 0; k < dateFields.length; k++) {
                    if (dateFields[k] == formObj.elements[i].name) {
                        if (verifyDate(formObj.elements[i]) == true){
                        } else {
                            return false;
                        }
                    }
                }
            } /* end if field must be completed logic */
        } /* end for loop through requiredFields array */
    }
    return true;
}

/* cjohnson
 * provided by tonyz from a library
 */
function spaceTrim(InString) {
    var LoopCtrl=true;
    while (LoopCtrl) {
        if (InString.indexOf("  ") != -1) {
            Temp = InString.substring(0, InString.indexOf("  "))
            InString = Temp + InString.substring(InString.indexOf("  ")+1, InString.length)
        } else
            LoopCtrl = false;
    }
    if (InString.substring(0, 1) == " ")
        InString = InString.substring(1, InString.length)
    if (InString.substring (InString.length-1) == " ")
        InString = InString.substring(0, InString.length-1)
    return (InString)
}

/* cjohnson
 * checks to see if the character given is a letter
 */
function isLetter(x) {
    var asciiCode = x.toLowerCase().charCodeAt(0);
    if ((asciiCode >= 97) && (asciiCode <= 122)) {
        return (true);
    } else {
        return (false);
    }
}


/* checks to see if a formElementObj is empty */
function isEmptyElement ( formElementObj ) {
    var isEmpty = false;
    if ((formElementObj.type == "text") || (formElementObj.type == "textarea") || (formElementObj.type == "hidden") || (formElementObj.type == "password")) {
        if (formElementObj.value == '') {
            isEmpty = true;
        } else {
            formElementObj.value = spaceTrim(formElementObj.value);
            if (formElementObj.value == '') {
                isEmpty = true;
            }
        }
    } /* end checking for text and textarea */

    /* if element is select element make sure something is selected and whatever is selected has a value */
    if (formElementObj.type.indexOf("select") == 0) {
        if (formElementObj.selectedIndex < 0) {
                isEmpty = true;
        } else if (formElementObj.options[formElementObj.selectedIndex].value == '') {
                isEmpty = true;
        }
    } /* end checking for select elements */

    return (isEmpty);
}
function lockValueMulti ( featureKey, featureIndex ) {
    var lockElement = null;
    var pairElement = null;
    var elementName = null;
    var lockName    = null;
    if (featureIndex == null) {
        elementName = 'classifieds.ad.feature_value(' + featureKey + ')';
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + ')';
    } else {
        elementName = 'classifieds.ad.feature_value(' + featureKey + '):' + featureIndex;
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + '):' + featureIndex;
    }
    for (i = 0; i < document.forms[1].elements.length; i++) {
        if (document.forms[1].elements[i].name == elementName) {
            pairElement = i;
        } else if (document.forms[1].elements[i].name == lockName) {
            lockElement = i;
        }

        if (pairElement != null && lockElement != null) {
            i = document.forms[1].elements.length;
        }
    }

    if (pairElement == null || lockElement == null) {
        alert ("Warning, one of the matching lock fields is missing");
    } else {

        if (isEmptyElement(document.forms[1].elements[pairElement]) && document.forms[1].elements[lockElement].value != "ADMIN_TOOL") {
            alert ("You can't lock the element for " + featureKey + " because you have not set the value");
        } else {
            if (document.forms[1].elements[lockElement].value == "ADMIN_TOOL") {
                swapImage(lName, imageURL + '/images/lock_off.gif');
                document.forms[1].elements[lockElement].value = "";
            } else {
                swapImage(lName, imageURL + '/images/lock_on.gif');
                document.forms[1].elements[lockElement].value = "ADMIN_TOOL";
            }
        }
    }
}


function lockValue ( featureKey, featureIndex, fNum ) {
    var lockElement = null;
    var pairElement = null;
    var elementName = null;
    var lockName    = null;

    if (fNum == null)
    {
       fNum = 0;
    }

    if (featureIndex == null) {
        elementName = 'classifieds.ad.feature_value(' + featureKey + ')';
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + ')';
        lName = featureKey;
    } else {
        elementName = 'classifieds.ad.feature_value(' + featureKey + '):' + featureIndex;
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + '):' + featureIndex;
        lName = featureKey + featureIndex;
    }

    for (i = 0; i < document.forms[fNum].elements.length; i++) {
        if (document.forms[fNum].elements[i].name == elementName) {
            pairElement = i;
        } else if (document.forms[fNum].elements[i].name == lockName) {
            lockElement = i;
        }

        if (pairElement != null && lockElement != null) {
            i = document.forms[fNum].elements.length;
        }
    }

    if (pairElement == null || lockElement == null) {
        alert ("Warning, one of the matching lock fields is missing");
    } else {

        if (isEmptyElement(document.forms[fNum].elements[pairElement]) && document.forms[fNum].elements[lockElement].value != "ADMIN_TOOL") {
            alert ("You can't lock the element for " + featureKey + " because you have not set the value");
        } else {
            if (document.forms[fNum].elements[lockElement].value == "ADMIN_TOOL") {
                swapImage(lName, imageURL + '/images/lock_off.gif');
                document.forms[fNum].elements[lockElement].value = "";
            } else {
                swapImage(lName, imageURL + '/images/lock_on.gif');
                document.forms[fNum].elements[lockElement].value = "ADMIN_TOOL";
            }
        }
    }
}

function lockField ( featureKey, fType, featureIndex, fNum ) {
    var lockElement = null;
    var pairElement = null;
    var elementName = null;
    var lockName    = null;

    if (fNum == null)
    {
       fNum = 0;
    }

    if (featureIndex == null) {
        elementName = 'classifieds.ad.feature_' + fType + '(' + featureKey + ')';
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + ')';
        lName = featureKey;
    } else {
        elementName = 'classifieds.ad.feature_' + fType + '(' + featureKey + '):' + featureIndex;
        lockName = 'classifieds.ad.feature_created_by(' + featureKey + '):' + featureIndex;
        lName = featureKey + featureIndex;
    }

    for (i = 0; i < document.forms[fNum].elements.length; i++) {
        if (document.forms[fNum].elements[i].name == elementName) {
            pairElement = i;
        } else if (document.forms[fNum].elements[i].name == lockName) {
            lockElement = i;
        }

        if (pairElement != null && lockElement != null) {
            i = document.forms[fNum].elements.length;
        }
    }

    if (pairElement == null || lockElement == null) {
        alert ("Warning, one of the matching lock fields is missing");
    } else {

        if (isEmptyElement(document.forms[fNum].elements[pairElement]) && document.forms[fNum].elements[lockElement].value != "ADMIN_TOOL") {
            alert ("You can't lock the element for " + featureKey + " because you have not set the value");
        } else {
            if (document.forms[fNum].elements[lockElement].value == "ADMIN_TOOL") {
                swapImage(lName, imageURL + '/images/lock_off.gif');
                document.forms[fNum].elements[lockElement].value = "";
            } else {
                swapImage(lName, imageURL + '/images/lock_on.gif');
                document.forms[fNum].elements[lockElement].value = "ADMIN_TOOL";
            }
        }
    }
}

function lockCustomer (formnum )
{
    var lockElement = null;
    var lockName    = null;

    if (formnum == null)
    {
        formnum = 0;
    }

    lockName = 'classifieds.ad_customer.updated_by';

    for (i = 0; i < document.forms[formnum].elements.length; i++)
    {
        if (document.forms[formnum].elements[i].name == lockName)
        {
            lockElement = i;
        }

        if (lockElement != null)
        {
            break;
        }
    }
    if (lockElement == null)
    {
        alert ("Warning, one of the matching lock fields is missing");
    }
    else
    {
            if (document.forms[formnum].elements[lockElement].value == "ADMIN_TOOL")
            {
                swapImage('customer_lock', imageURL + '/images/lock_off.gif');
                document.forms[formnum].elements[lockElement].value = "";
            }
            else
            {
                swapImage('customer_lock', imageURL + '/images/lock_on.gif');
                document.forms[formnum].elements[lockElement].value = "ADMIN_TOOL";
            }
   }
}

function lockAd (formnum )
{
    var lockElement = null;
    var lockName    = null;

    if (formnum == null)
    {
        formnum = 0;
    }

    lockName = 'classifieds.ad.updated_by';

    for (i = 0; i < document.forms[formnum].elements.length; i++)
    {
        if (document.forms[formnum].elements[i].name == lockName)
        {
            lockElement = i;
        }

        if (lockElement != null)
        {
            break;
        }
    }
    if (lockElement == null)
    {
        alert ("Warning, one of the matching lock fields is missing");
    }
    else
    {
            if (document.forms[formnum].elements[lockElement].value == "ADMIN_TOOL")
            {
                swapImage('ad_lock', imageURL + '/images/lock_off.gif');
                document.forms[formnum].elements[lockElement].value = "";
            }
            else
            {
                swapImage('ad_lock', imageURL + '/images/lock_on.gif');
                document.forms[formnum].elements[lockElement].value = "ADMIN_TOOL";
            }
   }
}


/* dreamweaver produced function */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function verifyDigits (formElementObj) {
    for (i = 0; i < formElementObj.value.length; i++) {
        c = formElementObj.value.substr(i, 1);
        if ("1234567890.".indexOf(c) == -1) {
            alert ("You must enter only digits for values requiring numbers\n");
            i = formElementObj.value.length;
            formElementObj.focus();
        }
    }
}

//-->
