// Copyright (C) 2010 Aaron Merriman. All rights reserved.

var showing_tour     = false,
    tour_position    = -400,
    current_image    = 1,
    background_image = '';

    temp_image_01     = new Image();
    temp_image_01.src = "images/tour_01.jpg";
    temp_image_02     = new Image();
    temp_image_02.src = "images/tour_02.jpg";
    temp_image_03     = new Image();
    temp_image_03.src = "images/tour_03.jpg";
    temp_image_04     = new Image();
    temp_image_04.src = "images/tour_04.jpg";
    temp_image_05     = new Image();
    temp_image_05.src = "images/tour_05.jpg";
    temp_image_06     = new Image();
    temp_image_06.src = "images/tour_06.jpg";

function testimonials() {
 var quotes = new Array("Using RDS Logic, online orders now account for 91% of all my restaurant orders.  My customers love it, I love it.~Andrew~foodfriendz.com",
                        "I was hesitant to change my online ordering software.  BigTree blew away my expectations.  The online ordering is easy for my customers, delivery drivers and restaurants.~Corey~912food2go.com",
                        "I have a large food delivery service, so I needed something that can handle thousands of orders and scale to multiple cities.  RDS Logic is helping me to grow my business.~Josh~dine123.com",
                        "I love the ability to offer online ordering and rewards to my corporate lunch clients.~Chantel~branchlunch.com");
  quote_num = Math.floor(Math.random()*quotes.length),
  quote_val = quotes[quote_num].split('~');

  document.getElementById('testimonials').innerHTML = '<i>"' + quote_val[0] + '"</i><br /><br /><span>' + quote_val[1] + '</span><br />' + '<b>' + quote_val[2] + '</b><br />';
}

function move_tour(tour_value) {
 if (tour_position < 0) {
  document.getElementById('tour_image').style.marginTop = tour_position + "px";
  tour_position = tour_position + 20;
 } else {
  showing_tour = false;
  tour_position = -400;
  clearInterval(moving);
  document.getElementById('tour_image').style.marginTop = tour_position + "px";
  document.getElementById('tour_container').style.backgroundImage = background_image;
 }
}

function show_tour(tour_number) {
 if (!showing_tour && current_image != tour_number) {
  background_image = "url('images/tour_0" + tour_number + ".jpg')";
  showing_tour = true;
  document.getElementById('tour_image').style.backgroundImage = background_image;
  current_image = tour_number;
  moving = setInterval("move_tour(' + tour_number + ')",10);
 }
}

function validate_contact() {
 var con_val = new Array(0,document.forms['bg_contact'].first_name.value,
                           document.forms['bg_contact'].last_name.value,
                           document.forms['bg_contact'].email.value,
                           document.forms['bg_contact'].phone.value,
                           document.forms['bg_contact'].message.value),
     chk_pos = 0,
     chk_nam = /[^A-Za-z0-9 ]/,
     err_val = new Array();

 for (var tmp_val = 1; tmp_val < con_val.length; tmp_val++) {
  con_val[tmp_val] = con_val[tmp_val].replace(/^\s+|\s+$/g,'');
 }
 for (var tmp_val = 0; tmp_val < con_val[3].length; tmp_val++) {
  if (con_val[3].charAt(tmp_val) == "@") {
   chk_pos++;
  }
 }
 if (con_val[1].length < 1 || chk_nam.test(con_val[1])) {
  document.getElementById('bg_01').className = "input_error";
  err_val[1] = 1;
 } else {
  document.getElementById('bg_01').className = "";
 }
 if (con_val[2].length < 1 || chk_nam.test(con_val[2])) {
  document.getElementById('bg_02').className = "input_error";
  err_val[2] = 1;
 } else {
  document.getElementById('bg_02').className = "";
 }
 if (con_val[3].length < 5 || /[^A-Za-z0-9\-\.@_]|\.@|@\.|\.\.|^\.|\.$|!\./.test(con_val[3]) || chk_pos != 1 || con_val[3].lastIndexOf('@') > con_val[3].lastIndexOf('.') || con_val[3].lastIndexOf('-') > con_val[3].lastIndexOf('.') || con_val[3].lastIndexOf('_') > con_val[3].lastIndexOf('.')) {
  document.getElementById('bg_03').className = "input_error";
  err_val[3] = 1;
 } else {
  document.getElementById('bg_03').className = "";
 }
 if (con_val[5].length < 1) {
  document.getElementById('bg_05').className = "input_error";
  err_val[5] = 1;
 } else {
  document.getElementById('bg_05').className = "";
 }
 if (err_val.length > 0) {
  alert('Oops! One or more required fields are invalid or incomplete (noted in red).\nPlease revise and re-click the \'Send Now\' button.');
  if (err_val[1] == 1) {
   document.forms['bg_contact'].first_name.focus();
  } else
   if (err_val[2] == 1) {
    document.forms['bg_contact'].last_name.focus();
   } else
    if (err_val[3] == 1) {
     document.forms['bg_contact'].email.focus();
    } else
     if (err_val[5] == 1) {
      document.forms['bg_contact'].message.focus();
     }
  } else {
   document.forms['bg_contact'].submit();
  }
}
