function getData(part)
{
    // alert (str);
    var div = part + '_div';
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById(div).innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","BarberShop/ajax/getData.php?part="+part,true);
    xmlhttp.send();
}
function changeData(str)
{
    // alert (str);
    var menuLinks =["services", "about_us", "location", "packages","appointments"] ; //3) literal array
    var menu;
    var div = 'body_div';
    var img_id =  str + '_img';
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById(div).innerHTML = xmlhttp.responseText;
            document.getElementById(img_id).src='BarberShop/includes/images/red.gif';
            for (menu in menuLinks)
            {
                var current_image_id = menuLinks[menu] + '_img';
                if (str != menuLinks[menu]) { document.getElementById(current_image_id).src='BarberShop/includes/images/black.gif'; }
            }
        }
    }

xmlhttp.open("GET","BarberShop/ajax/getData.php?part="+str,true);
xmlhttp.send();
}

function appointmentProcessStepOne()
{
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('body_div').innerHTML = xmlhttp.responseText;

        }
    }
//var url =  "&staff_id=" + staff_id + "&service_id=" + service_id ;
xmlhttp.open("GET","BarberShop/ajax/appointment.php?step=1",true);
xmlhttp.send();
}
function appointmentProcessStepTwo(){
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('body_div').innerHTML = xmlhttp.responseText;
        }
    }


if ($("input[@name='appointBook_Staff_Selection']:checked").val()){
    staff_id = $("input[@name='appointBook_Staff_Selection']:checked").val();
}
else {
    setTimeout(function(){ $('#appointmentProcessStepTwoError').html('<font color=red>Error:</font> No Barber/Stylist Selected').fadeOut(6000); },200,$(".appointmentProcessStepTwoError").show());
    $(".appointmentProcessStepTwoError").show();
    return false;
}

    var url =  "&staff_id=" + staff_id  ;
xmlhttp.open("GET","BarberShop/ajax/appointment.php?step=2"+url,true);
xmlhttp.send();
return true;
}
    function appointmentProcessSelectService(service_id){
        ChooseService_service_id =  'ChooseService_'+service_id;
        var selectServiceData = { "service_id": service_id }
        $.ajax({
            type : 'Post',
            url : 'BarberShop/ajax/appointmentProcessSelectService.php',
            data: selectServiceData,
            dataType : 'json',
            success:function(data){
                    if (data.returnCode == 1) {
                        document.getElementById(ChooseService_service_id).className = "bcgreen";
                        $('#ChooseServices_est_time_total').html("Time: "+data.total_services_times+" mins.").fadeIn(5000);
                        $('#ChooseServices_total_services_price').html("Value: $"+data.total_services_price).fadeIn(5000);
                        $('#make_appointment_step1_profile_descriptions').html(data.html);
                    } else {
                        document.getElementById(ChooseService_service_id).className = "";
                        $('#ChooseServices_est_time_total').html("Time: "+data.total_services_times+" mins.").fadeIn(5000);
                        $('#ChooseServices_total_services_price').html("Value: $"+data.total_services_price).fadeIn(5000);
                        $('#make_appointment_step1_profile_descriptions').html(data.html);
                    }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#Inventory_Items_SubmitNewItem_result').html('There was an error.').delay(2000);
            }
        });
}
function appointmentProcessStepThree(selected_apt_time)
{
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('body_div').innerHTML = xmlhttp.responseText;
        }
    }
if (appointmentProcessStepThree.arguments.length == 1) {
    var url =  "&selected_apt_time=" + selected_apt_time ;
}
else {
    var url = "";
}

xmlhttp.open("GET","BarberShop/ajax/appointment.php?step=3"+url,true);
xmlhttp.send();
}
function appointmentProcessStepFour()
{
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('body_div').innerHTML = xmlhttp.responseText;
        }
    }
    var url = "";
xmlhttp.open("GET","BarberShop/ajax/appointment.php?step=4"+url,true);
xmlhttp.send();
}

function changeCal(year,month) {
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('calendar').innerHTML = xmlhttp.responseText;
            var selected_date=year+'-'+month+'-01';
            selectAppointmentDate(selected_date);
        }
    }
var url =  "?year=" + year + "&month=" + month ;
xmlhttp.open("GET","BarberShop/ajax/changeCal.php"+url,true);
xmlhttp.send();
}

function ValidateUser() {
    var ValidateUserData =  {  "validateUser": 1,
                               "email": document.getElementById('user_email').value
                            }
        $.ajax({
        type : 'Post',
        url : 'BarberShop/ajax/validateUser.php',
        data: ValidateUserData,
        dataType : 'json',
        success:function(data){
            if (data.count == 1) {
                    appointmentProcessStepThree();
            }
            else {
                    document.getElementById('failed_login_message').innerHTML =
                    "<font color=red> " +
                    "User " + document.getElementById('user_email').value + " not found,<br>it looks like you are not yet registered." +
                    "</font>";
                    LoadNewUserDiv(document.getElementById('user_email').value);
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
    //            $('#Inventory_Items_SubmitNewItem_result').html('There was an error.').delay(2000);
        }
    });
}
function ValidateUserQuickCheck() {
    var ValidateUserData =  {  "validateUser": 1,
                               "email": document.getElementById('user_email_quick_check').value
                            }
        $.ajax({
        type : 'Post',
        url : 'BarberShop/ajax/validateUser.php',
        data: ValidateUserData,
        dataType : 'json',
        success:function(data){
            if (data.count == 1) {
                    document.getElementById('loginPanel').innerHTML =
                    "<font color=white style=\"font-size:12;\"> " +
                    "Welcome Back "  + document.getElementById('user_email_quick_check').value + " !" +
                    "</font>";
                    doPulsate();
            }
            else {
                    document.getElementById('loginPanel_Error').innerHTML =
                    "<font color=red> " +
                    "User "  + document.getElementById('user_email_quick_check').value + " not found." +
                    "</font>";
                    NewUser();
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
    //            $('#Inventory_Items_SubmitNewItem_result').html('There was an error.').delay(2000);
        }
    });
}
function NewUser() {
    var valid;  
    var NewUserData =   {"newuser": 1,
                        "NU_user_email": document.getElementById('NU_user_email').value,
                        "NU_first_name": document.getElementById('NU_first_name').value,
                        "NU_last_name":  document.getElementById('NU_last_name').value,
                        "NU_phone_num":  document.getElementById('NU_phone_num').value
                        }
    if ( document.getElementById('NU_first_name').value == "" ) {
        document.getElementById('failed_register_message_NU_first_name').innerHTML = "<font color=red><- is first name blank?</font>";
            valid = "false";
        }
        else { document.getElementById('failed_register_message_NU_first_name').innerHTML = "" }

    if ( document.getElementById('NU_last_name').value == "" ) {
        document.getElementById('failed_register_message_NU_last_name').innerHTML = "<font color=red><- is last name blank?</font>";
            valid = "false";
        }
        else { document.getElementById('failed_register_message_NU_last_name').innerHTML = "" }

    if ( document.getElementById('NU_user_email').value == "" || validateEmail(document.getElementById('NU_user_email').value) != "" ) {
        document.getElementById('failed_register_message_NU_user_email').innerHTML = "<font color=red><- is email blank?</font>";
            valid = "false";
        }
        else { document.getElementById('failed_register_message_NU_user_email').innerHTML = "" }

    if ( document.getElementById('NU_phone_num').value == "" ) {
        document.getElementById('failed_register_message_NU_phone_num').innerHTML = "<font color=red><- is Phone # blank?</font>";
            valid = "false";
        }
        else { document.getElementById('failed_register_message_NU_phone_num').innerHTML = "" }

if ( valid != "false") {
            $.ajax({
            type : 'Post',
            url : 'BarberShop/ajax/NewUser.php',
            data: NewUserData,
            dataType : 'json',
            success:function(data){
                    if (data.returnCode == 1) {
                        appointmentProcessStepThree();
                    } else {
                          document.getElementById('failed_NewUser_message').html = data.message;
//                        $('#ChooseServices_est_time_total').html("Time: "+data.total_services_times+" mins.").fadeIn(5000);
//                        $('#ChooseServices_total_services_price').html("Value: $"+data.total_services_price).fadeIn(5000);
//                        $('#make_appointment_step1_profile_descriptions').html(data.html);
                    }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
//                    $('#Inventory_Items_SubmitNewItem_result').html('There was an error.').delay(2000);
            }
        });
    }
}
function LoadNewUserDiv() {
            $.ajax({
            type : 'Post',
            url : 'BarberShop/ajax/LoadNewUserDiv.php',
            dataType : 'json',
            success:function(data){
                  $('#registerPanel').html(data.html);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
//                    $('#Inventory_Items_SubmitNewItem_result').html('There was an error.').delay(2000);
            }
        });
}

function Logout(dimension1) {
    var dimension1 = dimension1 ;

    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            // document.getElementById('loginPanel').innerHTML = "logged out";
            appointmentProcessStepOne();
            }
        }
var url =  "?dimension1=" + dimension1 ;

xmlhttp.open("GET","BarberShop/ajax/DestroySession.php"+url,true);
xmlhttp.send();
}
function deleteEvent(event_id){
        var eventData = { "event_id": event_id }
        $.ajax({
            type : 'Post',
            url : 'BarberShop/ajax/deleteEvent.php',
            data: eventData,
            dataType : 'json',
            success:function(data){
                    if (data.returnCode == 1) {
                        //$('#registerPanel').html(data.html);
                        appointmentProcessStepThree();
                    }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#registerPanel').html('There was an error.').delay(2000);
            }
        });
}
function changeCalendarYearWidget(year) {

    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('calendarYearWidget').innerHTML = xmlhttp.responseText;
            changeCal(year,'01');   //  after page has reloaded
        }
    }
var url =  "?year=" + year ;
xmlhttp.open("GET","BarberShop/ajax/changeCalWidget.php"+url,true);
xmlhttp.send();
}

function selectAppointmentDate(selected_date) {
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==1 ) {
                    document.getElementById("calendarLoading").innerHTML="<font color=red>Loading...</font>";
                    document.getElementById("make_appointment_step2_choose_apt").innerHTML="<font color=red>Loading Appointment Data...</font>";
                }
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var current_selected_date_ID = "DateTD_" + current_selected_date ;
            document.getElementById(current_selected_date_ID).className = "dateNotSelected";

            var selected_date_ID = "DateTD_" + selected_date ;
            document.getElementById(selected_date_ID).className = "dateSelected";

            document.getElementById('current_selected_date').value = selected_date ;
            document.getElementById("calendarLoading").innerHTML="&nbsp;";
            document.getElementById('make_appointment_step2_choose_apt').innerHTML = xmlhttp.responseText;
        }
    }
var current_selected_date = document.getElementById("current_selected_date").value;
var url =  "?selected_date=" + selected_date ;
xmlhttp.open("GET","BarberShop/ajax/selectDate.php"+url,true);
xmlhttp.send();
}
function selectAppointmentTime(selected_time) {
    if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else    {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('make_appointment_step2_choose_apt').innerHTML = xmlhttp.responseText;

        }
    }
var url =  "?selected_date=" + selected_date ;
xmlhttp.open("GET","BarberShop/ajax/selectAppointment_chooseTime.php"+url,true);
xmlhttp.send();
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(email_address) {
    var error="";
    var tfld = trim(email_address);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (email_address == "") {
        //email_address.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        //email_address.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (email_address.match(illegalChars)) {
        //email_address.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        //email_address.style.background = 'White';
    }
    return error;
}
function AppointmentAlreadyBooked(){
    alert('You still have an open appointment which hasen\'t been completed yet.  You can cancel it on the right-hand panel on this page.');
}
function doPulsate() {
  var i = 0;
  function pulsate() {
    if(i >= 5) return;
    $(".pulsate").
      animate({opacity: 0.2}, 1000, 'linear').
      animate({opacity: 1}, 1000, 'linear', pulsate);
    i++;
  }
  pulsate();
}
