


// *************************************************************************************************
//
// Utilities


function Delegate() {}
Delegate.create = function (o, f) {
	var a = new Array() ;
	var l = arguments.length ;
	for(var i = 2 ; i < l ; i++) {
		a[i - 2] = arguments[i] ;
	}
	return function() {
		var aP = [].concat(arguments, a) ;		
		f.apply(o, aP);
	}
}


//
// Retrieve a GET variable from the request
//
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return false;
}




// *************************************************************************************************
//
// Product popups

var theProductPop = null;
function productPopup(target, image, contentID) {

	var pop = document.createElement("div");
	pop.style.position = "absolute";
	pop.style.marginLeft = "-5px";
	pop.style.marginTop = "-190px";
	
	target.insertBefore( pop, target.firstChild)	
	
	var so = new SWFObject("product_popup.swf", "popup_swf", "250", "208", "8", "#000000");
	so.addVariable("contentID", contentID); 
	so.addVariable("image", image); 
	so.addParam("scalemode", "noscale"); 
	so.addParam("wmode", "transparent"); 
	so.write(pop);
	
	window.theProductPop = target
}

document.onmouseup = function() {
	if( window.theProductPop ) {
		window.theProductPop.removeChild(window.theProductPop.firstChild)
		window.theProductPop = null;
	}
}






// *************************************************************************************************
//
// Tabs 

var selectedTab;
function setSelectedTab ( tabID ) {
	
	var tab = document.getElementById(tabID)
	var contentID = tabID.split("tab")[1]
	
	if(tab){	
		var defaultTab = document.getElementById('defaultTab');
		if(defaultTab){
			defaultTab.className = "";
		}
		if(selectedTab) {
			selectedTab.className = "";
		}
		
		selectedTab = tab
		
		tab.className = "on";
		loadElement('subContent','default.asp','contentID='+contentID,'get')
		
	}
}







// *************************************************************************************************
//
// Setup all auto-clear text fields

function setupAutoClearFields () {
	//
	// Grab all inputs with class="txtField"
	//
	var fields = document.getElementsByTagName("input");
	for(var i=0; i<fields.length; i++) {
		if(fields[i].className == "txtField") {
			makeAutoClearField(fields[i])
		}
	}
	//
	// Grab all textareas
	//
	var fields = document.getElementsByTagName("textarea");
	for(var i=0; i<fields.length; i++) {
		makeAutoClearField(fields[i])
	}
}
//
// make text input or textarea auto-clear
//
function makeAutoClearField(field) {

	field.defaultValue = field.value
	//
	field.onclick = function() {
		if(this.value == this.defaultValue){
			this.value=""
		}
	}
}
window.onload = function() {
	//
	// When page loads setup auto-clearing fields
	window.setupAutoClearFields();
}









// *************************************************************************************************
//
// AJAX stuff

//
// create a cross-browser XMLHttpRequest
//
function createXMLHttpRequest()
{
 var request = false;
 if(navigator.appName.indexOf("Explorer") > -1){
  request = new ActiveXObject("Msxml2.XMLHTTP");
  if(!request){
   request = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }else{
  request = new XMLHttpRequest();
 }
 if (!request){
  alert("Error initializing XMLHttpRequest!");
 }
 return request;
}


//
// load a URL using XMLHttpRequest and
// pass output to a given element on the page
//
function loadElement(elem, url, params, method)
{
 var request = createXMLHttpRequest()

 window.pendingRequest = request
 window.pendingElement = elem
 request.onreadystatechange = onResponse
 
 
 if(method == "POST") {
  request.open(method, url, true);
  //Send the proper header information along with the request
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.setRequestHeader("Content-length", params.length);
  request.setRequestHeader("Connection", "close");

  request.send(params);
 }else{
  request.open(method, url + "?" + params, true);
  request.send(null);
 }
}



//
// response from XMLHttpRequest
//
function onResponse()
{
 var request = window.pendingRequest
 var elem = window.pendingElement

 //
 // response from server (readyState: 4)
 // check for status code 200 OK
 //
 if (request.readyState == 4 && request.status == 200) {
  var elemTag = document.getElementById(elem)
  
  elemTag.innerHTML = request.responseText
    
  if(window.popup) {
  	window.popup.center();
  	setupAutoClearFields();
  }
 }
};


// *************************************************************************************************





//
// BEGIN js popup class
//
var Popup = function(id,content)
{

 this.dim = document.createElement("div");
 this.dim.id = "dim"
 this.dim.onclick = function(){ 
 	window.popup.close(); 
 }
 
 document.body.appendChild(this.dim)

 this.popupObj = document.createElement("div");
 this.popupObj.id=id
 this.popupObj.className="popup"
 this.popupObj.owner = this
 if(content){
	 this.popupObj.appendChild(content)
 }
 document.body.appendChild(this.popupObj)
 window.popup = this;
}

Popup.prototype.hide = function()
{
 this.popupObj.style.visibility = "hidden"
}
Popup.prototype.show = function()
{
 this.popupObj.style.visibility = "visible"
}
Popup.prototype.moveTo = function(x,y)
{
 this.popupObj.style.left = x + "px";
 this.popupObj.style.top = y + "px";
}
Popup.prototype.moveBy = function(x,y)
{
 this.popupObj.style.left = ( parseInt(this.popupObj.style.left) + x ) + "px";
 this.popupObj.style.top = ( parseInt(this.popupObj.style.top) + y ) + "px";
}
Popup.prototype.center = function()
{
 if(navigator.appName.indexOf("Explorer") > -1){
  var x = ( document.body.offsetWidth / 2 ) - ( this.popupObj.offsetWidth / 2 );
  var y = ( document.body.offsetHeight / 2 ) - ( this.popupObj.offsetHeight / 2 );
 }else{
  var x = ( window.innerWidth / 2 ) - ( this.popupObj.offsetWidth / 2 )
  var y = ( window.innerHeight / 2 ) - ( this.popupObj.offsetHeight / 2 )
 }
 this.popupObj.style.left = x + "px"
 this.popupObj.style.top = y + "px"
}

Popup.prototype.close = function(x,y)
{
 document.body.removeChild(this.popupObj)
 document.body.removeChild(this.dim)
}
//
// END js popup
//




function makePopUpPage(url,params) {
	var p = new Popup('popup',null);
	loadElement('popup',url,params,'get');
 }


function makePopUpImage(src) {	
	//
	// Make container for image
	var frame = document.createElement("div")
	frame.className="popupFrame"

	//
	// Make header
//	var header = document.createElement("div")
//	header.className="popupHeader"
//	frame.appendChild(header)
	
	//
	// Attach Close Button
	var btn_close = document.createElement("img")
	btn_close.src = "/img/btn_close.gif";
	btn_close.className = "btn_close2";
	btn_close.onclick = function() {
		window.popup.close();
	}
//	header.appendChild(btn_close)
	frame.appendChild(btn_close)

	var br = document.createElement("br")
	frame.appendChild(br)

	
	//
	// Attach Image
	var img = document.createElement("img")
	var t = new Date().getTime();
	img.src = src + "?time=" + t
	img.style.cssFloat = "left";
	img.onload = function() {
		window.popup.center();
		window.popup.show();
	}
	frame.appendChild(img)
	//alert(frame.innerHTML)
	//
	// Make popup
	var p = new Popup('popup',frame);
	
	//
	// hide popup
	p.hide();
	
	// img.onload() will reveal and center the popup once the image has loaded
}

function swapImg(id, newSrc) {
	var img = document.getElementById(id)
	if(img) {
		img.src = newSrc
	}
}


function submitFranchiseeInfoForm(page) {

	if (page == 'bug'){
		var form = document.getElementById("cmsf");
	} else {
		var form = document.getElementById("franchiseeForm");
	}

	/*if(form.f10.value == "" || form.f10.value == form.f10.defaultValue) {
		alert("Please enter your name");
		return false;
	}
	else if(form.f19.value == "" || form.f19.value == form.f19.defaultValue) {
		alert("Please enter your email address");
		return false;
	}
	else if(form.f11.value == "" || form.f11.value == form.f11.defaultValue) {
		alert("Please enter your address");
		return false;
	}
	else if(form.f13.value == "" || form.f13.value == form.f13.defaultValue) {
		alert("Please enter your city");
		return false;
	}
	else if(form.f14.value == "" || form.f14.value == form.f14.defaultValue) {
		alert("Please enter your province");
		return false;
	}
	else if(form.f15.value == "" || form.f15.value == form.f15.defaultValue) {
		alert("Please enter your postal code");
		return false;
	}
	
	*/
	
	// *************************************************************************************************
	//
	//	The Metrick System - Yohann Paris - developer@metricksystem.com
	//	March 15 2011
	//  
	//  Change the Franchisee information package form due to robot submit abuse
	//
	// *************************************************************************************************
	
	// Regular expression to test email and postal code format
	var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var regPostal = /^\s*[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z](\s)?\d[a-ceghj-npr-tv-z]\d\s*$/i;
	
	if(form.f19.value == "" || form.f19.value == form.f19.defaultValue || regEmail.test(form.f19.value) == false ) {
		alert("Please enter your email address");
		return false;
	}
	else if(form.f15.value == "" || form.f15.value == form.f15.defaultValue || regPostal.test(form.f15.value) == false ) {
		alert("Please enter your postal code");
		return false;
	}
	else {
		form.submit();
		return true;
	}
		
}

// Mootools
$(window).load(function() {
	
	// *************************************************************************************************
	//
	//	The Metrick System - Yohann Paris - developer@metricksystem.com
	//	December 01 2010
	// 	jQuery library
	//
	//  - Changing the image on the home page every 10 secondes
	//  - Button to control the change for the user
	//
	// *************************************************************************************************

	function InfiniteRotator(startItem)
    {
        //initial fade-in time (in milliseconds)
        var initialFadeIn = 500;

        //interval between items (in milliseconds)
        var itemInterval = 7000;

        //cross-fade time (in milliseconds)
        var fadeTime = 500;

        //count number of items
        var numberOfItems = $('#homePagePromotion img').length;

        //set current item
        var currentItem = startItem;

        //show first item
        $('#homePagePromotion img').eq(currentItem).fadeIn(initialFadeIn);
        $('#homePagePromotionLink a').eq(currentItem).css('background-position', 'bottom');

        //loop through the items
        var infiniteLoop = setInterval(function(){
            
            $('#homePagePromotion img').eq(currentItem).fadeOut(fadeTime);
			$('#homePagePromotionLink a').eq(currentItem).css('background-position', 'top');
				
            if(currentItem == numberOfItems -1){
                currentItem = 0;
            }else{
                currentItem++;
            }
                        
            $('#homePagePromotion img').eq(currentItem).fadeIn(fadeTime);
            $('#homePagePromotionLink a').eq(currentItem).css('background-position', 'bottom');

        }, itemInterval);

    }
    
    // Automatically start with the fist one
    InfiniteRotator(0);    
    
    // *************************************************************************************************
	//
	//	The Metrick System - Yohann Paris - developer@metricksystem.com
	//	March 22 2011
	//  
	//  Change the Franchisee information package form due to robot submit abuse
	//  on page http://www.mrsub.ca/form.asp?i=4
	//
	// *************************************************************************************************
    
    if(document.getElementById("cmsf")){
    	submitFranchiseeInfoForm('bug');
    }
	
	
    // *************************************************************************************************
	//
	//	The Metrick System - Yohann Paris - developer@metricksystem.com
	//	November 29 2011
	//  
	//  Redesign the Marketing page
	//
	// *************************************************************************************************
	
	var posters = $('#posters li img');
	posters.css('display', 'none');
	
	var nbPosters = posters.length;
	var menuPoster = $('#posters li h1');
	
	menuPoster.each(function(index){
		if ($(this).hasClass('selected')){
			posters.eq(index).css('display', 'block');
			posters.eq(index).fadeIn();
		}	
	});
	
	menuPoster.click(function(){
	
		if (!$(this).hasClass('selected')){
			menuPoster.removeClass('selected');
			posters.css('display', 'none');
			$(this).addClass('selected');
		}
		
		menuPoster.each(function(index){
			if ($(this).hasClass('selected')){
				posters.eq(index).css('display', 'block');
				posters.eq(index).fadeIn();
			}	
		});
	
	});

});
















