// Validation for fields in the AltosCRM Service

var residenceTypeDropdownProvided = false;



// Ensure the user of this form has entered the required fields.

function validateData() {

    var txtFirstName = getElement("FIRST_NAME");

    var txtLastName = getElement("LAST_NAME");

    var txtEmail1 = getElement("EMAIL1");

    var txtPhoneAreaCode = getElement("PHONE_AREA_CODE");

    var txtPhoneLocalCode = getElement( "PHONE_LOCAL_CODE");

    var txtPhoneNumber = getElement( "PHONE_NUMBER");


    var txtComments = getElement( "COMMENTS");

    var cityzipSelect = getElement( "cityzip" );



    var sPhoneValue = txtPhoneAreaCode.value + txtPhoneLocalCode.value + txtPhoneNumber.value;

    var bRequirementsMet = true;

    var txtFocusField;



    // Trim all the fields and reset those

    txtFirstName.value = txtFirstName.value.trim();

    txtLastName.value = txtLastName.value.trim();

    txtEmail1.value = txtEmail1.value.trim();

    txtFirstName.value = txtFirstName.value.trim();

    txtPhoneAreaCode.value = txtPhoneAreaCode.value.trim();

    txtPhoneLocalCode.value = txtPhoneLocalCode.value.trim();

    txtPhoneNumber.value = txtPhoneNumber.value.trim();

    txtComments.value = txtComments.value.trim();



    if( bRequirementsMet && ( txtEmail1.value.length == 0 ) ) {

        bRequirementsMet = false;

        window.alert( "Please enter an email address." );

        txtEmail1.focus();

    } else if ( bRequirementsMet && !isValidEmail( txtEmail1.value ) ) {

        bRequirementsMet = false;

        window.alert( "You have entered an invalid email address.\nPlease enter a correct value." );

        txtEmail1.focus();

    }



    if( bRequirementsMet && typeof( txtComments ) != "undefined" && txtComments.value.length > 2000 ) {

        bRequirementsMet = false;

        window.alert( "Your comments are too long.\nPlease try to limit the length of your comments to 2000 characters." );

        txtComments.focus();

    }



    if ( bRequirementsMet && typeof( cityzipSelect ) != "undefined" ) {

        var selectedValue = cityzipSelect.options[ cityzipSelect.selectedIndex ].value;

        if ( selectedValue == null || selectedValue == "" ) {

            bRequirementsMet = false;

            window.alert( "Please select the interested market.\nIf you don't find what you are looking for, select 'NOT-IN-THIS-LIST' \nand then enter the city in the comments field and we will get back to you." );

            cityzipSelect.focus();

        }

    }



    return bRequirementsMet;

}



function isInteger( s ) {

    var i;

    for ( i = 0; i < s.length; i++) {

        // Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }



    // All characters are numbers.

    return true;



}



function moveToNextField( id ) {

    if( window.event && window.event.keyCode != 9 ) {

        if( getElement( id ).value.length == 3 ) {

            if( id == "PHONE_AREA_CODE" ) {

                getElement( "PHONE_LOCAL_CODE" ).focus();

            }

            else if( id == "PHONE_LOCAL_CODE" ) {

                getElement( "PHONE_NUMBER" ).focus();

            }

        }



    }

}



function allowKeys( obj, keys ) {

    var bAllowKey = false;

    for( var i = 0; i < keys.length; i++ ) {

        if( window.event && keys.charAt( i ) == String.fromCharCode( window.event.keyCode ) ) {

            bAllowKey = true;

            break;

        }

    }

    if( !bAllowKey && window.event ) {

        window.event.cancelBubble = true;

        window.event.keyCode = 0;

    }

}



function validateInput( obj, keys ) {

    if( typeof( window.clipboardData ) != "undefined" ) {

        var sData = window.clipboardData.getData( "Text" )

        var sTemp = "";

        if( typeof( sData ) != "undefined" ) {

            for( var i = sData.length; --i >= 0; ) {

                for( var j = keys.length; --j >= 0; ) {

                    if( sData.charAt( i ) == keys.charAt( j ) ) {

                        sTemp += sData.charAt( i );

                    }

                }

            }

        }



        window.clipboardData.setData( "Text", sTemp );

    }

}



// Set the <option>s of the given <select> to the

// zips for the given restype.

function refreshCityZips( pElem ) {

    var resType = pElem.options[ pElem.selectedIndex ].value;

    var resTypeEle = findEntryForResType( resType );

    var cityzipSelect = getElement( "cityzip" );

    var options = cityzipSelect.options;

    options.length = 1;

    if (resTypeEle != null) {

        var i;

        for (i = 1; i < resTypeEle.length; i++) {

            var cityzip = resTypeEle[i][0];

            var len = cityzip.length;

            var seperatorChar = cityzip.indexOf('~:~');

            var cityzipvalue = cityzip.substring(0, seperatorChar);

            var cityziplabel = cityzip.substring( seperatorChar + 3, len );

            options[options.length] = new Option(cityziplabel, cityzipvalue);

        }

        options[options.length] = new Option("-NOT-IN-THIS-LIST-", "-1");

    }

}



// Find the ["restype", [value~:~label]] entry for a given make

function findEntryForResType( resType ) {

    var i;

    for (i = 0; i < cityzips.length; i++) {

        if (cityzips[i][0] == resType) {

            return cityzips[i];

        }

    }

    return null

}



function onSubmit( elem ) {

    if( validateData() ) {

        var hashVal = $.sha1( $("#EMAIL1").val() + $("#cityzip").val());
        $("#hash").attr("val", hashVal);
        $("#hash").val(hashVal);

        submitted = true;

        elem.disabled = true;

        elem.form.submit();

    }

}



