//fbform_en.js
//version 1.1

    function checkmail(mail_str){
        // 0=corr, 1=wrong, 2=null
        if(mail_str!="") {
            var re=/(\.@)|(^[\.\-]{1})|(\.\-\.+)|(@\.\-)|(@.*_)/;
            result = re.test(mail_str);
            if(!result) {
                re=/^[(\w)\.\-]+[@]{1}[(\w)\.\-]+[\.\-]{1}[^(\W|_|\.\-)]{2,6}$/;
                result = re.test(mail_str);
                if(!result)
                    return 1;
                return 0;
            }
            return 1;
        }
        return 2;
    }

    //check all form
    function check_form_data(this_frm){
        //disable submit button
        this_frm.submit.disabled = true;

        //trim all fields
        this_frm.fb_subject.value = jstrim( this_frm.fb_subject.value );
        this_frm.fb_body.value    = jstrim( this_frm.fb_body.value );
        this_frm.fb_name.value    = jstrim( this_frm.fb_name.value );
        this_frm.fb_mail.value    = jstrim( this_frm.fb_mail.value );
        this_frm.fb_phone.value   = jstrim( this_frm.fb_phone.value );
        this_frm.fb_geo.value     = jstrim( this_frm.fb_geo.value );

        if (this_frm.fb_name.value=="" || this_frm.fb_subject.value=="" || this_frm.fb_body.value=="" ) {

            if(this_frm.fb_name.value==""){
                alert("Present, please");
                this_frm.fb_name.focus();

                //enable submit button
                this_frm.submit.disabled = false;
                return false;
            }else{
                if (this_frm.fb_subject.value == "") {
                    alert("Please, enter subject");
                    this_frm.fb_subject.focus();

                    //enable submit button
                    this_frm.submit.disabled = false;
                    return false;
                }else{
                    if (this_frm.fb_body.value == "") {
                        alert("Please, enter message");
                        this_frm.fb_body.focus();

                        //enable submit button
                        this_frm.submit.disabled = false;
                        return false;
                    }
                }
            }
            //enable submit button
            this_frm.submit.disabled = false;
            return false;
        }else{
            if(this_frm.fb_theme.options[this_frm.fb_theme.options.selectedIndex].value==0){
                alert('Indicate your question themes');
                document.frm_fb.fb_theme.focus();

                //enable submit button
                this_frm.submit.disabled = false;
                return false;
            }

            //save theme text
            this_frm.fb_theme_txt.value = this_frm.fb_theme.options[this_frm.fb_theme.options.selectedIndex].text;

            //save branch text
            this_frm.fb_branch_txt.value = this_frm.fb_branch.options[this_frm.fb_branch.options.selectedIndex].text;

            if(this_frm.fb_phone.value==jstrim(this_frm.fb_phone.defaultValue)){
                this_frm.fb_phone.value = this_frm.fb_phone.value.replace(jstrim(this_frm.fb_phone.defaultValue), '');
                jstrim(this_frm.fb_phone.value);

                //enable submit button
                this_frm.submit.disabled = false;
                //return false;
            }

            if(document.frm_fb.fb_isclient.checked && document.frm_fb.fb_branch.selectedIndex==0){
                alert("Indicate your branch or representation office");
                document.frm_fb.fb_branch.focus();

                //enable submit button
                this_frm.submit.disabled = false;
                return false;
            }else{
                if(document.frm_fb.fb_geo.value.length==0){
                    alert("Indicate geo location: country, city");
                    document.frm_fb.fb_geo.focus();

                    //enable submit button
                    this_frm.submit.disabled = false;
                    return false;
                }
            }

            this_frm.fb_phone.value = this_frm.fb_phone.value.replace(/\s+/gi, '');


            if(/[^\#\(\)\*\+\-\d]/.test(this_frm.fb_phone.value)) {
                //alert('Phones is specified not faithfully: '+this_frm.fb_phone.value);
                this_frm.fb_phone.value = this_frm.fb_phone.value.replace(/[^\#\(\)\*\+\-\,\f\?\:\d]/gi, '');
            }

            //lower case mail
            this_frm.fb_mail.value = this_frm.fb_mail.value.toLowerCase();

            //check email
            if( checkmail(this_frm.fb_mail.value)==1 ){
                alert("May be wrong mailbox: "+this_frm.fb_mail.value + "");
                this_frm.fb_mail.focus();

                //enable submit button
                this_frm.submit.disabled = false;
                return false;
            }
            if( (this_frm.fb_mail.value+this_frm.fb_phone.value).length == 0 ){
                if ( !confirm("You didn't leave contact information (mailbox/phones).\n\nSo we don't be able answer to you.\n\nAre you agree?\n") ) {
                    this_frm.fb_phone.value=jstrim(this_frm.fb_phone.defaultValue);
                    this_frm.fb_mail.focus();

                    //enable submit button
                    this_frm.submit.disabled = false;
                    return false;
                }else{
                }
            }

            var sbuf="";
            sbuf+="name: "+ this_frm.fb_name.value +"\n";

            if(this_frm.fb_mail.value.length!=0){  sbuf+="email: "+ this_frm.fb_mail.value +"\n"; }
            if(this_frm.fb_phone.value.length!=0){ sbuf+="phones: "+ this_frm.fb_phone.value +"\n"; }
            if(this_frm.fb_geo.value.length!=0){   sbuf+="location: "+ this_frm.fb_geo.value +"\n\n"; }
            sbuf+="theme: "+this_frm.fb_theme.options[this_frm.fb_theme.options.selectedIndex].text+"\n";
            sbuf+="subject: "+this_frm.fb_subject.value + "\n";
            sbuf+="------\n";
            sbuf+=this_frm.fb_body.value + "\n";

            //last confirm
            if ( confirm("Check once again, are all correctly?\n\n"+sbuf+"\n") ) {
                return true;
            }else{
                //enable submit button
                this_frm.submit.disabled = false;
                return false;
            }

        }
    }

    //set fb_theme selectedIndex
    function set_theme() {
        var prm = new String(location.search); // ?param1=value1&param2=value2
        if (prm.length!=0) prm = prm.substring(1,prm.length); //remove ?
        var aprm = prm.split('&'); //param1=value1

        //set selectedIndex
        if(aprm.length!=0 && /\d+/.test(aprm) ) {
            for(var i=0; i<document.frm_fb.fb_theme.options.length; i++){
                if(document.frm_fb.fb_theme.options[i].value == aprm){
                    document.frm_fb.fb_theme.options.selectedIndex = i;
                    break;
                }
            }
        }
    }

    function get_prm(p) {
        for (var i=0; i<aprm.length; i++)
            if (aprm[i].split('=')[0]==p)
                return(unescape(aprm[i].split('=')[1]));
        return('');
    }
    set_theme();
    document.frm_fb.fb_name.focus();
/**/

