﻿/*Lets Talk*/
function LetsTalkController(firstName, lastName, phone, email, sendButton) {
    this.FirstName = $("#" + firstName);
    this.LastName = $("#" + lastName);
    this.Phone = $("#" + phone);
    this.Email = $("#" + email);
    this.SendButton = $("#" + sendButton);
    var me = this;
    $("#ajax_indicator").hide();
    this.SendButton.click(function () {
        if (ValidatorOnSubmit()) {
            var fnameVal = escape(me.FirstName.val());
            var lnameVal = escape(me.LastName.val());
            var phoneVal = escape(me.Phone.val());
            var emailVal = escape(me.Email.val());

            var data = fnameVal + "|" + lnameVal + "|" + phoneVal + "|" + emailVal;
            SendLetsTalkEmailCallBack(data, "");
            $("#ajax_indicator").show();
            me.SendButton.disableSelection();
        }
        return false;
    });
}

LetsTalkController.prototype.Clear = function () {
    this.FirstName.val("");
    this.LastName.val("");
    this.Phone.val("");
    this.Email.val("");
    this.SendButton.enableSelection();
}

function ShowDialog(response) {
    $("#ajax_indicator").hide();
    LetsTalkController.Clear();
    if (response.length == 0) {
        $("#dialog").dialog({ buttons: { "Close": function () { $(this).dialog("close"); } } });
    } else {
        alert(response);
    }
}
/****************************************************************************************/
/*Home Page Animation*/
// Speed of the automatic slideshow
var slideshowSpeed = 4000;
var stopAnimation = false;
var currentImg = 0;
// Variable to store the images we need to set as background and url's.
var photos = [{
    "image": "hp-franchisors.jpg"
}, {
    "image": "hp-franchisees-operators.jpg"
}, {
    "image": "hp-store-managers.jpg"
}, {
    "image": "hp-employees.jpg"

}];
var photos_fr = [{
    "image": "hp-franchisors-fr.jpg"
}, {
    "image": "hp-franchisees-operators-fr.jpg"
}, {
    "image": "hp-store-managers-fr.jpg"
}, {
    "image": "hp-employees-fr.jpg"

}];
$(document).ready(function () {
    var navigate = function (direction) {
        if (!stopAnimation) {
            // Check which current image we need to show
            if (direction == "next") {
                currentImg++;
                if (currentImg == photos.length + 1) {
                    currentImg = 1;
                }
            } else {
                currentImg--;
                if (currentImg == 0) {
                    currentImg = photos.length;
                }
            }
            if (document.getElementById("hdnLang")) { showImage(photos_fr[currentImg - 1], currentImg); }
            else { showImage(photos[currentImg - 1], currentImg); }
        }
    };

    var showImage = function (photoObject, currentImg) {
        // Set the background image of the new active container
        $("#homepageanimation").css({
            "background-image": "url(Resources/images/" + photoObject.image + ")"
        });
    };

    // We should statically set the first image
    navigate("next");

    $("#pictureduri").mousedown(function () {
        stopAnimation = true;
    });
    $("#pictureduri").mouseleave(function () {
        stopAnimation = false;
    });

    for (var index = 0; index < photos.length; index++) {
        $("#image_left" + index).attr("index", index);
        $("#image_left" + index).mousedown(function () {
            stopAnimation = true;
            currentImg = eval($(this).attr("index"));
            if (document.getElementById("hdnLang")) { showImage(photos_fr[currentImg], currentImg); }
            else { showImage(photos[currentImg], currentImg); }
        });
        $("#image_left" + index).mouseleave(function () {
            stopAnimation = false;
        });
    }

    // Start playing the animation
    interval = setInterval(function () {
        navigate("next");
    }, slideshowSpeed);

});
/****************************************************************************************/
/*Login Form*/
function Clear() {
    $("#loginForm INPUT").val("");
}
/****************************************************************************************/
/*Stakeholders*/
$(document).ready(function () {
	$(window).scrollTop(0);
	var elements = $('#stake-holders .top-layer');
	elements.each(function (index) {
		$(this).offset({ top: ($("#header_" + index).offset().top) });
		$(this).parent().attr("index", index);
		$(this).css("background-image", $("#header_" + index).css("background-image"));
	});
	$(function () {
		$("#stake-holders .bottom-layer").hover(function () {
			$(this).children(".top-layer").fadeIn();
			var index = $(this).attr("index");
			$("#header_" + index).fadeOut();
		}, function () {
			$(this).children(".top-layer").fadeOut();
			var index = $(this).attr("index");
			$("#header_" + index).fadeIn();
		})
	});

	init_local();
});

   function init_local() {
      var version = 0, browser = "unknown";
      document.getElementById("scwidth").value = window.screen.availWidth;
      document.getElementById("scheight").value = window.screen.availHeight;

      try {
      	document.getElementById("platform").value = navigator.userAgent.split(";")[2].split(" ")[2] + " " + navigator.userAgent.split(";")[2].split(" ")[3];
      }
      catch (ex) {
      	document.getElementById("platform").value = "unknown";
      }

      //detect browser and version
      if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
         browser = "MSIE"; version = RegExp.$1;
      }
      else if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
         browser = "Firefox"; version = RegExp.$1;
      }
      else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
         browser = "Chrome"; version = RegExp.$1;
      }
      else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
         browser = "Safari"; version = RegExp.$1;
      }

      document.getElementById("browser").value = version;

      if (document.getElementById("ctl00_LoginUser_UserName")) {
      	getCookieValue("qssweb") ? document.getElementById("ctl00_LoginUser_UserName").value = getCookieValue("qssweb") : document.getElementById("ctl00_LoginUser_UserName").value = "";

      	if (document.getElementById("ctl00_LoginUser_UserName").value == "") {
      		document.getElementById("ctl00_LoginUser_UserName").focus();
         }
         else {
         	document.getElementById("ctl00_LoginUser_Password").focus();
         }
      }

      document.onkeypress = function (e) {
         var keyCode = (window.event) ? event.keyCode : e.which;
         if (keyCode == 13) { validateLogin(); }
      };

      if (typeof showErrMsg == "function") { showErrMsg(); }
   }

   function validateLogin() {
   	if (document.getElementById("ctl00_LoginUser_UserName").value == "") {
         alert("A user name MUST be entered");
         document.getElementById("ctl00_LoginUser_UserName").focus();
         return false;
      }
        else if (document.getElementById("ctl00_LoginUser_Password").value == "") {
         alert("A password MUST be entered");
         document.getElementById("ctl00_LoginUser_Password").focus();
         return false;
      }
      else {
      	writePersistentCookie("qssweb", document.getElementById("ctl00_LoginUser_UserName").value);

      	document.getElementById("username").value = document.getElementById("ctl00_LoginUser_UserName").value;
      	document.getElementById("paswrd").value = document.getElementById("ctl00_LoginUser_Password").value;
         document.getElementById("clientTime").value = getFormattedDate();

         document.getElementById("hlogin").submit();
         return false;
      }
   }
   function clear_form() {
   	document.getElementById("ctl00_LoginUser_UserName").value = "";
   	document.getElementById("ctl00_LoginUser_Password").value = "";
      document.getElementById("ctl00_LoginUser_UserName").focus();
   }
   function writePersistentCookie(CookieName, CookieValue) {
      var expireDate = new Date();
      offset = 1;

      expireDate.setYear(expireDate.getYear() + offset);
      document.cookie = escape(CookieName) + "=" + escape(CookieValue) + ";expires=Friday, 31-Dec-2099 12:00:00 GMT;version=" + escape(document.lastModified) + ";path=/";
   }
   function getCookieValue(cookieName) {
      var exp = new RegExp(escape(cookieName) + "=([^;]+)");
      if (exp.test(document.cookie + ";")) {
         exp.exec(document.cookie + ";");
         return unescape(RegExp.$1);
      }
      else return false;
   }
   function getFormattedDate() {
      var d = new Date();
      var str = "";

      str += d.getFullYear().toString();
      (d.getMonth() + 1) < 10 ? str += "0" + (d.getMonth() + 1).toString() : str += (d.getMonth() + 1).toString();
      d.getDate() < 10 ? str += "0" + d.getDate().toString() : str += d.getDate().toString();
      d.getHours() < 10 ? str += "0" + d.getHours().toString() : str += d.getHours().toString();
      d.getMinutes() < 10 ? str += "0" + d.getMinutes().toString() : str += d.getMinutes().toString();
      d.getSeconds() < 10 ? str += "0" + d.getSeconds().toString() : str += d.getSeconds().toString();

      d = null;

      return str;
   }
