// CONTACT
 
// PAGE LOAD 

$(document).ready(function()
	{
  	   
  	   checkBrowser();
  	   
  	   //$("#id_contact_name").focus(); jumps directly to form but we don't want this to happen as the page scrolls to it
  	   
  	   $('#id_contact_email').change(function() {
  	        validateEmail();
       });
        
        $('#id_contact_telephone').change(function() {
            validatePhone();
        });
        
        $('#form_request_screening').submit(function() {
              if ( !validateform() ) return false;
        });
        
        //$.timeEntry.setDefaults({show24Hours: true});
        
        $('#time_entry').timeEntry({spinnerImage: ''}).change(function() { updateDateTime()   });
        $('#date_entry').calendarsPicker().change(function() { updateDateTime()   });; 
                  
       //setupAccordion();
       $('#hiddenDateTime').hide();
       
        
        var contactHeight = $("#contact").height();
         
        if (  $('#message').innertHTML == "Please correct the errors below." ) alert("found errors");
         
         
         updateUIHeight(1200); 
        
	}
);

function checkBrowser()
{
    var is_chrome =  navigator.userAgent.toLowerCase().indexOf('chrome')  > -1;
    if (is_chrome )
    {
        //alert("hi chrome");
        //$("#request_screening_div").hide();
        //$("#request_screening_div").css({"top": "-300px", "left": "100px"});
        $("#form_request_screening").css({"top": "0px", "left": "0px"});
        // $("#form_request_screening").hide();
    }
}


function updateDateTime()
{
   var dateTime_str;
   var time_str = $('#time_entry').val();
   
   if ( time_str.indexOf("PM") > 0 ) 
   {
        var pmIndex = time_str.indexOf("PM");
        time_str = time_str.substring(0,pmIndex); 
        
        var hourTime = time_str.split(":")[0];
        var minTime = time_str.split(":")[1];
        
        if ( hourTime.substr(0,1) == "0" )
        {
           hourTime = hourTime.substr(1,1);
        }
        
         var hourTimeInt = parseInt(hourTime) + 12;
        
        time_str = hourTimeInt + ":" + minTime;
        
   }
   
   if ( time_str.indexOf("AM") > 0 ) 
   {
        var amIndex = time_str.indexOf("AM");
        time_str = time_str.substring(0,amIndex); 
          
        
    }
   
    dateTime_str = $('#date_entry').val() + " " + time_str;
    
    $('#id_date').val( dateTime_str );
}

function validateEmail()
{
          var validEmail = false;
          
            var emailStr = $('#id_contact_email').val();
            var apos = emailStr.indexOf("@");
            
            var dotpos = emailStr.lastIndexOf(".");
      
            if (apos<1||dotpos-apos<2)
            {
                $("#id_contact_email").css({"font-family": "Arial, Helvetica, sans-serif", "color": "Red", "width":"250" });
                $("#id_email_label").css({"font-family": "Arial, Helvetica, sans-serif", "color": "Red", "width":"150" });
             }
            else 
            {
                 $("#id_contact_email").css({"font-family": "Arial, Helvetica, sans-serif", "color": "#000", "width":"250"});
                 $("#id_email_label").css({"font-family": "Arial, Helvetica, sans-serif", "color": "White", "width":"150"});
                 
                 validEmail = true;
            }
            
            return validEmail;
}

function validatePhone()
{
// validate phone // just make its not blank
   
    var validPhone = false;
   
    var telephoneStr = $('#id_contact_telephone').val();
    
    if (telephoneStr == "")
    {
        $("#id_contact_telephone").css({"font-family": "Helvetica, Arial, sans-serif", "color": "Red", "width":"250" });
        $("#id_telephone_label").css({"font-family": "Helvetica, Arial, sans-serif", "color": "Red", "width":"150" });
     }
    else 
    {
      $("#id_contact_telephone").css({"font-family": "Helvetica, Arial, sans-serif", "color": "#000", "width":"250"});
      $("#id_telephone_label").css({"font-family": "Helvetica, Arial, sans-serif", "color": "White", "width":"150"});
      
      validPhone = true;
    }
    
    return validPhone;
}

function validateform()
{
    //
    //alert("validate form!");
    var validForm = false;
    
    validForm = validatePhone();
    validForm = validateEmail();
   
    return validForm
}

function setupAccordion()
{
    $('#contact').accordion({ autoHeight: false });
 
    $('#contact').bind('accordionchange', function(event, ui) {
               
         var selectedPanelIdS = $(ui.newContent).attr("id");
         var selectedPanelIdA = selectedPanelIdS.split("_");
         selectedPanelId = '#'+ String(selectedPanelIdA[0]);
        
        //alert("select panel: " +  selectedPanelId);    
        
        switch(String(selectedPanelIdA[0]))
        {
        case "directions" :
            document.getElementById("request_plus").innerHTML = "+ ";
            document.getElementById("directions_plus").innerHTML = "- ";
            break;
        case "request" :
            document.getElementById("request_plus").innerHTML = "- ";
            document.getElementById("directions_plus").innerHTML = "+ ";
            break;     
        }
        
        
    });
    
     document.getElementById("directions_plus").innerHTML = "- ";

}

function updateUIHeight(mainContentHeight)
{
           
           // SLICE
           
            var sliceNewTop = mainContentHeight;
            $('.main_slice_bg').css( { "top" :  String(mainContentHeight) + "px" } );
           
            // SLICE BENEATH
                
            var sliceBoxNewTop = sliceNewTop +  $('.main_slice_bg').height() - 20;
            $('.main_whitebox_beneathslice').css( { "top" :  String(sliceBoxNewTop) + "px" } );
           
           
           // COMING SOON
           $('#comingsoon').css( { "top" :  String( sliceNewTop + 75 ) + "px" } );  
                             
           // FOOTER
           var newFooterTop = sliceBoxNewTop + $('.main_whitebox_beneathslice').height() - 100;
           $('#footer').css( { "top" :  String(newFooterTop) + "px" } );
           
           // SIDEBAR
            var sidebarNewHeight = mainContentHeight + $('.main_slice_bg').height() +  $('.main_whitebox_beneathslice').height() -  $('.sidebar_top').height() - 125;
           $('.sidebar_block').height(sidebarNewHeight);
           
           // MAIN INNNER
           var mainInnerNewHeight = mainContentHeight +  $('.main_slice_bg').height() +   $('.main_whitebox_beneathslice').height() + $('#footer').height() - 40;
           
           // MAIN HEIGHT
           
            //alert("main_inner: " + mainInnerNewHeight);
           
            var mainNewHeight = mainInnerNewHeight - 100;
           
           //alert("mainInnerNewHeight: " + mainNewHeight);
           
           $('#main').height(mainNewHeight);
}


