﻿(function($){
        $.fn.bindValidators = function() { 
            return this.each(function(){              
                $(this).find('.digits').bind('keydown',function(event){                        
                        var keyCode = event.keyCode;                                                
                        if ( keyCode == 46 || keyCode == 8 || keyCode == 9 ||  //allow only delete, backspace and tab
                                ( event.shiftKey == false && ((keyCode >= 48 && keyCode <= 57) || (keyCode >=96 && keyCode <= 105)))
                           )
                        {
                            //let it happen, don't do anything
                        } 
                        else
                        {
                            //Ensure that it is a number and stop the keypress                       
                            event.preventDefault();                                
                        }                                            
                });            
                $(this).find('[class*=max]').bind('keydown', function(event){
                       var keyCode = event.keyCode;                                                
                       if (keyCode == 46 || keyCode == 8 || keyCode == 9) //allow delete, backspace and tab
                           return;
                     
                       var classNames = $(this).attr("className").split(" ");
                       for (i = 0; i < classNames.length; i++)
                       {
                           className = classNames[i];
                           if (/^max/i.test(className))
                           {
                                value = $(this).val();
                                max = parseInt(className.substr(3));
                                if (value.length == max)
                                {
                                     event.preventDefault();     
                                     return;
                                }  
                           }
                       }
                });
            });  
            
                      
        } 
        
        $.fn.isValidForm = function(){
            $form = $(this);
            var valid = true;
            var message = '';
            
            var name = $form.find('#tbName').val();               
            var phone = $form.find('#tbPhone').val();
            var prefix = $form.find('#tbPrefix').val();
            var email = $form.find('#tbEmail').val();  
        
            if (name == '')
            { 
                message += nameRequired + '<br />'; 
                valid = false;
            }                               
            if (email == '' && prefix == '' && phone == '')
            {
                message += phoneOrEmailRequired + '<br />';
                valid = false;
            }
            else if (prefix == '' ^ phone == '')
            {
                message += prefixAndPhoneRequired + '<br />';
                valid = false;
            }                
            if (email != '' && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+\s*([-.]\w+\s*)*$/i.test(email)))
            {
                message += emailInvalid + '<br />';
                valid = false;                
            }
            if (phone != '' && prefix != '' && (!(/^[0-9]{4,20}$/i.test(phone))  || !(/^[0-9]{1,4}$/i.test(prefix))))
            {
                message += phoneInvalid + '<br />';
                valid = false;                
            }
                                
            if (message !== '')
                $form.find('#message').html(message);
            else 
                $form.find('#message').html('');
           
            return valid;
        }       
})(jQuery);
   
function OpenContactFormDialog(contactFormID,dialogTitle)
{  
    JQ('#'+contactFormID).dialog('option', 'title', dialogTitle.trim())
                         .dialog('option', 'position', ['center','center'])
                         .dialog('open');
}
function SaveBizFormInfo(contactFormID, webServiceUrl)
{
    $contactForm = JQ(contactFormID);
    if ($contactForm.isValidForm())
    {        
        $name = $contactForm.find('#tbName');  
        $phone = $contactForm.find('#tbPhone');
        $prefix = $contactForm.find('#tbPrefix');
        $email = $contactForm.find('#tbEmail');
            
        var parameters = "{'name':'" + $name.val() + "','phone':'" + '+' + $prefix.val() + ' ' + $phone.val() + "','email':'" + $email.val() + "','buttonText':'" + $contactForm.dialog('option','title') + "'}";
        JQ.ajax({
                type: "POST",        
                url: webServiceUrl,
                data: parameters,
                contentType: "application/json; charset=utf-8",                   
                dataType: "json",
                success: function(msg) {        
                    if (msg.d.success == true)    
                    {
                         $contactForm.find('.fields').hide();                             
                    }   
                    $contactForm.find('#message').html(msg.d.message);                   
               }
        });       
    }
    }
    function pageLoad() {
        document.forms[0].action = window.location.href.replace(/html/g, "aspx"); ;
    }
    // function pageLoad() {
    //                var form = Sys.WebForms.PageRequestManager.getInstance()._form;
    //                form._initialAction = form.action = window.location.href.replace(/html/g, "aspx");
    //            }
