var varMajigWin = null;
function createOverlay()
{
	if(!document.getElementById('overlay'))
	{
		var objBody = document.getElementsByTagName("body").item(0);

		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { displayTeams(); return false; }
		objBody.appendChild(objOverlay);
	}
}


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function loadForm (officeid,email,url)
{
	displayTeams();
	//$('contact_form').innerHTML = "<img align='left' valign='middle' src='"+shared_url + "/images/meettheteam/loader.gif'>";

	var arrayPageSize = getPageSize();

	new Ajax.Request('http://'+url+'/?page=contact_office',
     {
		onSuccess : function(response, json)
		{
		    $('contact_form').innerHTML  = response.responseText;
		},
		onFailure : function(response)
		{
		    alert('Error loading agents (load form)');
		},
		parameters : "loadForm=yes&content=main&officeid"+officeid+"&email"+email
	}
    );
}

function loadContactForm (officeid,email,url)
{
	var arrayPageSize = getPageSize();

	new Ajax.Request("http://"+url+"/?page=contact_office&loadForm=yes&content=main&officeid="+officeid+"&email="+email,
     {
		onSuccess : function(response, json)
		{
			//alert(response.responseText);
			varMajigWin = new MaJiG_BOX(response.responseText);
		},
		onFailure : function(response)
		{
		    alert('Error loading agents (load form)');
		},
		parameters : ""
	}
    );
}

function closeContactForm()
{
    varMajigWin._close();
}

function  submitForm(email,officeid,url)
{
        
       
       var params = Form.serialize($('form'));
      
      
        new Ajax.Request('http://'+url+'/Buy-A-Franchise/Content/585/',
	{
		onSuccess:function(response, json)
		{
            //var ele = document.getElementById('contact_form');
            //ele.innerHTML = "";
			//ele.innerHTML = response.responseText;
			//$('contactoffice').style.display = 'none';
            
            document.getElementById("MaJiG_BOX").style.display = 'none';
            document.getElementById("MaJiG_BOX_canvas").style.display = 'none';
            //window.location = 'http://'+url+'/Contact-Us/';
		},
		onFailure : function(response)
		{
			alert('failed.');
		},
		method: 'post',
		parameters : "content=main&"+params
	});
	
}

function displayTeams()
{
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	if($('contact_form').visible())
	{
		Effect.Fade($('contact_form'),{queue: 'end', scope: 'togglebar', duration:1})
		$('overlay').style.display = 'none';
		showSelectBoxes();
	}
	else
	{
		createOverlay();
		hideSelectBoxes();

		//determine position of window
		$('contact_form').style.top = (arrayPageScroll[1] + (arrayPageSize[3] / 15)) + "px";
		//$('contact_form').style.left = (arrayPageSize[2] / 2) + "px";
		$('contact_form').style.left = (arrayPageSize[2] / 4) + "px";

		Effect.Appear($('contact_form'),{queue: 'end', scope: 'togglebar', duration:1})

		$('overlay').style.height = (arrayPageSize[1]) + "px";
		$('overlay').style.display = 'block';
	}
}

function DisplayRequiredFields(ClassID ,email,officeid,url)
{
    var blnVal = true;
    var DisplayDivs = document.getElementsByClassName(ClassID);
    DisplayDivs.each(function(Div)
    {
        if($(Div).value == '' || $(Div).value === '000')
        {
           $(Div).style.backgroundColor = "#FF9E9E";
           blnVal = false;
        }
        else
        {
            $(Div).style.backgroundColor = "";
            blnVal = true;
        }
    }); 
    if(blnVal)
    {
        submitForm(email,officeid,url)
    }
    return blnVal
}