// alias to jQuery library, function noConflict release control of the $ variable 
// to whichever library first implemented it
var $j = jQuery.noConflict();
// if true the send button is blocked
var g_blockSendButton = false;

/***************************************
    SETUP CONTACT FORM
****************************************/

function setupInputControls()
{
    // change border color wehen controls take focus
    $j(".commonInput, .commonTextarea, .contactInputHuman").focus(
        function()
        {
            $j(this).css("border", "1px solid #7b1819");
        }
    );
    
    // restore border color wehen controls lost focus
    $j(".commonInput, .commonTextarea, .contactInputHuman").blur(
        function()
        {
            $j(this).css("border", "1px solid #ccc");
            $j(this).css("border-right", "1px solid #eee");
            $j(this).css("border-bottom", "1px solid #eee");
        }
    );
    
    // when input name lost focus, validate the value
    $j("#txtName").blur(
        function()
        {
            if($j(this).val() != "")
            {
                $j("#contactNameErrorMsg").css("visibility", "hidden"); 
            } else
            {
                $j(this).css("border", "1px solid #FF0000");
                $j("#contactNameErrorMsg").html("&nbsp;please enter your name").css("visibility", "visible");            
            }
        }
    );
    
    // when input email lost focus validate the value 
    $j("#txtEmail").blur(
        function()
        {
            
            if($j(this).val() != "")
            {
                // create regular expression object
                var regExp = new RegExp(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z]{2})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?$/i);
                // check email address, if result is null the email string dont match to pattern
                var resultExp = regExp.exec($j(this).val());
                if(resultExp == null) 
                {
                    $j(this).css("border", "1px solid #FF0000");
                    $j("#contactEmailErrorMsg").html("&nbsp;sorry, but the email address as entered is not valid").css("visibility", "visible");
                } else
                {
                    $j("#contactEmailErrorMsg").css("visibility", "hidden");
                }
                
            } else
            {
                $j(this).css("border", "1px solid #FF0000");
                $j("#contactEmailErrorMsg").html("&nbsp;please enter your email adress").css("visibility", "visible"); 
            }
        }
    );
    
    // when input subject lost focus validate the value 
    $j("#txtSubject").blur(
        function()
        {
            if($j(this).val() != "")
            {
                $j("#contactSubjectErrorMsg").css("visibility", "hidden"); 
            } else
            {
                $j(this).css("border", "1px solid #FF0000");
                $j("#contactSubjectErrorMsg").html("&nbsp;please include a subject").css("visibility", "visible");            
            }
        }
    );    

    // when input message lost focus validate the value 
    $j("#txtMessage").blur(
        function()
        {
            if($j(this).val() != "")
            {
                $j("#contactMessageErrorMsg").css("visibility", "hidden"); 
            } else
            {
                $j(this).css("border", "1px solid #FF0000");
                $j("#contactMessageErrorMsg").html("&nbsp;please include your message").css("visibility", "visible");            
            }
        }
    );
    
    // when input human lost focus validate the value 
    $j("#inputHuman").blur(
        function()
        {
            if(parseInt($j(this).val(), 10) == 4)
            {
                $j("#contactHumanErrorMsg").css("visibility", "hidden"); 
            } else
            {
                $j(this).css("border", "1px solid #FF0000");
                $j("#contactHumanErrorMsg").html("&nbsp;try again").css("visibility", "visible");            
            }
        }
    );         
    
} // end of function setupInputControl
    
function setupSendButton()
{
    $j("#btnContact").click(
        function()
        {
            // prevent multiple send call by user
            if(true == g_blockSendButton)
            {
                return;
            }
            
            g_blockSendButton = true;
            // get all data from contact form and save it in local variables
            var inputName = $j("#txtName").val();
            var inputEmail = $j("#txtEmail").val();
            var inputSubject = $j("#txtSubject").val();
            var inputMessage = $j("#txtMessage").val();
            var inputHuman = $j("#inputHuman").val();
           
            // create regular expression object
            var regExp = new RegExp(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z]{2})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?$/i);
            // check email address, if result is null the email string dont match to pattern
            var resultExp = regExp.exec(inputEmail);
            // check user answer, resultHuman = true if ok, false if answer is bad
            var resultHuman = parseInt(inputHuman, 10) == 4;
            // check the error by logical sum
            var error = (resultHuman != true) || (resultExp == null) || (inputName == "") || (inputEmail == "") ||
                (inputSubject == "") || (inputMessage == "");

            // if there was an error we must display some informotion and mark
            // input cotrol with wrong data    
            if(error)
            {                
                $j("#contactNameErrorMsg").css("visibility", "hidden");
                $j("#contactEmailErrorMsg").css("visibility", "hidden");
                $j("#contactSubjectErrorMsg").css("visibility", "hidden");
                $j("#contactMessageErrorMsg").css("visibility", "hidden");
                $j("#contactHumanErrorMsg").css("visibility", "hidden");
                $j("#contactErrorPanel").slideUp(300);
                
                // errors processing
                if(inputName == "")
                {
                    $j("#txtName").css("border", "1px solid #FF0000");
                    $j("#contactNameErrorMsg").html("&nbsp;please enter your name").css("visibility", "visible");
                }
                if(inputEmail == "")
                {
                    $j("#txtEmail").css("border", "1px solid #FF0000");
                    $j("#contactEmailErrorMsg").html("&nbsp;please enter your email adress").css("visibility", "visible"); 
                } else                
                if(resultExp == null) 
                {
                    $j("#txtEmail").css("border", "1px solid #FF0000");
                    $j("#contactEmailErrorMsg").html("&nbsp;sorry, but the email address as entered is not valid").css("visibility", "visible");
                }
                if(inputSubject == "")
                {
                    $j("#txtSubject").css("border", "1px solid #FF0000");
                    $j("#contactSubjectErrorMsg").html("&nbsp;please include a subject").css("visibility", "visible"); 
                }
                if(inputMessage == "")
                {
                    $j("#txtMessage").css("border", "1px solid #FF0000");
                    $j("#contactMessageErrorMsg").html("&nbsp;please include your message").css("visibility", "visible");
                }
                if(resultHuman != true)
                {
                    $j("#inputHuman").css("border", "1px solid #FF0000");
                    $j("#contactHumanErrorMsg").html("&nbsp;try again").css("visibility", "visible"); 
                }
                // unblock send button
                g_blockSendButton = false;                
            } else // if no error, if all data is set correctly
            {
                // let's define function called after ajax successfull call 
                function phpCallback(data)
                {   
                    // if success        
                    if(data == "ok")
                    {   
                        $j("#contactErrorPanel").text("");            
                        $j("#contactErrorPanel").css("background-color", "#ccFFcc");
                        $j("#contactErrorPanel").append("Your email was sent.");
                        $j("#contactErrorPanel").css("border", "1px solid #339933");
                        $j("#contactErrorPanel").slideDown(300, function(){  g_blockSendButton = false;});
                        
                        $j("#txtName").val("");
                        $j("#txtEmail").val("");
                        $j("#txtSubject").val("");
                        $j("#txtMessage").val("");
                        $j("#inputHuman").val(""); 
                    } else // if error/problem during email sending in php script
                    {
                        $j("#contactErrorPanel").text("");
                        $j("#contactErrorPanel").css("background-color", "#FFcccc");
                        $j("#contactErrorPanel").css("border", "1px solid #993333");
                        $j("#contactErrorPanel").append("There was an error sending the email.");
                        $j("#contactErrorPanel").slideDown(300, function(){  g_blockSendButton = false;});               
                    }
                } // end of function phpCallback            
            
            
                // all data is correct so we can hide error/success panel
                $j("#contactErrorPanel").slideUp(300);
                
                // build data string for post call
                var data = "txtName="+inputName;
                data += "&"+"inputEmail="+inputEmail;
                data += "&"+"inputSubject="+inputSubject;
                data += "&"+"inputMessage="+inputMessage; 
                
                // try to send email via php script executed by server
                //$j.post("php/contact/sendMessage.php", data, phpCallback, "text");
                // unblock send button
            } // end else all dara
        }
    );
} // end of function setupSendButton
    
/***************************************
    MAIN CODE - CALL THEN PAGE LOADED
****************************************/
       
// binding action to event onload page
$j(document).ready(
    function()
    {
        // common.js
        setupGlobal();
        setupCommunityButtons();            
        setupToolTipText();
        setupSearchBox();
        setupCufonFontReplacement();
        setupSideBarMiniSlider();
        setupMultiImageLightBox();
        setupSidebarTabsPanel();
        setupLoadingAsynchronousImages();
        setupToolTipImagePreview();
        setupTextLabelImagePreview();
        // this file
        setupInputControls();
        //setupSendButton();
    }
);




    
