// ***************************************************
// Operational functions
// ***************************************************
	var myImage = new Image() 
	var divIdName = "page_overlay";
	var newDiv;
	var loadLoop;
	var theCSS;
	var theHTML;
	var topFactor;
	var theCenter;
	
	function pauseLoop(){
		if(myImage.height > 0 && myImage.width > 0){
			addLayer();
		} else{
			loadLoop = setTimeout("pauseLoop()",300);
		}
	}
	
	function recreateImgObject(){
		myImage = new Image();
	}
	
	// Master function launched after AJAX request is successful
	function addOverlay(img){
		// Create a new div (replacing the current one if necessary)
		
		wSize = getPageSize();
		topFactor = 75;
		theCenter = (wSize[0]/2)-(400/2);
		
		newdiv = (document.getElementById(divIdName)) ? document.getElementById(divIdName) : document.createElement('div');
		newdiv.setAttribute("id",divIdName);
		myImage = new Image();
		myImage.src = img; 
		
		theCSS = "position:absolute; z-index:100; padding:0; margin:0; " +
			"width: " + 400 + "px; " + // -24 reduce padding from totla width
			"height: " + 200 + "px; " + // just need to be there to fool some CSS integrations, real heigh set by lenght of content
			"left :" + theCenter + "px; " +
			"top: " + (getScroll()+topFactor) + "px; " +
			"background: #FFFFFF; " +
			"display: block;"
			
		theHTML = '<p align="center" style="padding-top:85px;">Loading <img align="absbottom" src="http://images.autodesk.com/flashassets/staticassets/overlaypopup/animated_load_gif_02.gif" width="12" height="8"></p>';
		
		// load HTML in DIV
		newdiv.innerHTML = theHTML;
		
		// load CSS for DIV
		newdiv.style.cssText = theCSS;
	
		//Put new div into document Object
		coverUp();
		document.body.appendChild(newdiv);
		
		loadLoop = setTimeout("pauseLoop()",300);

	}
	
	function addLayer(){
		// Get width, height and cent properties from the XML
		wSize = getPageSize()
		topFactor = 75;
		imageBorder = 18;
		theCenter = (wSize[0]/2)-((myImage.width+(imageBorder*2))/2);

		theCSS = "position:absolute; z-index:100; padding:0; margin:0; " +
			"width: " + (myImage.width+(imageBorder*2)) + "px; " + // -24 reduce padding from totla width
			"height: " + (myImage.height+(imageBorder*2)) + "px; " + // just need to be there to fool some CSS integrations, real heigh set by lenght of content
			"left :" + theCenter + "px; " +
			"top: " + (getScroll()+topFactor) + "px; " +
			"background: #FFFFFF; " +
			"display: block; ";

		theHTML = "<div style='text-align:right; margin: 0; padding-top:4px; padding-right: 4px; height:" + (imageBorder-4) + "px;'><a href=\"javascript:removeOverlay();\"><img src='http://images.autodesk.com/flashassets/staticassets/overlaypopup/close.gif' border='0' width='13' height='13'></a></div>" +
					"<div style=\"padding-left:" + imageBorder + "px; \"><img src=\"" + myImage.src + "\" onClick=\"removeOverlay();\">" + 
					"</div></div>";

		// load HTML in DIV
		newdiv.innerHTML = theHTML;
		
		// load CSS for DIV
		newdiv.style.cssText = theCSS;
	
		//Put new div into document Object
		coverUp();
		document.body.appendChild(newdiv);
		
		//add onscroll behavior
		window.onresize = scrollFix;
	}
	
	// THis function detroy the coverup layer and the overlay layer
	function removeOverlay(){
		document.body.removeChild(document.getElementById('page_overlay'));
		document.body.removeChild(document.getElementById('page_coverUp'));
		window.onscroll = null;
		myImage.width = 0;
		myImage.height = 0;
	}
	
	//this function appends a semi-transparent overlay to the DOM covering up ALL the document
	function coverUp(){
		
		wSize = getPageSize()
		///pageWidth,pageHeight,windowWidth,windowHeight
		var divIdName = "page_coverUp";
		var coverUp = (document.getElementById(divIdName)) ? document.getElementById(divIdName) : document.createElement('div');
		coverUp.setAttribute("id",divIdName);
		//var vHeight = getHeight();
		var vHeight = wSize[1];
		
		if((BrowserDetect.version >=7 && BrowserDetect.browser == "Explorer") || BrowserDetect.browser == "Firefox"){
			//coverUp.style.cssText = "position:absolute; left:0px; top:0px; width:100%; height:" + vHeight + "px; background:black; filter:alpha(Opacity=50); opacity:0.50; -moz-opacity:0.50; -khtml-opacity:0.50";
			coverUp.style.cssText = "height:" + vHeight + "px; position:absolute; left:0px; top:0px; right:0px; bottom:0px; background:black; filter:alpha(Opacity=50); opacity:0.50; -moz-opacity:0.50; -khtml-opacity:0.50";
		} else{
			coverUp.style.cssText = "position:absolute; left:0px; top:0px; width:100%; height:" + vHeight + "px; background:url(http://images.autodesk.com/flashassets/staticassets/consolidatedLogin/images/ie6bg.gif) repeat";
		}
		document.body.appendChild(coverUp);
	}


// ***************************************************
// Utility functions
// ***************************************************

	// this function is attached to windows.onscroll to update the overlay X position as user scrolls
	function scrollFix(){
		wSize = getPageSize()
		if(document.getElementById('page_overlay')){
			theCenter = (wSize[0]/2)-((myImage.width+(imageBorder*2))/2);
			document.getElementById('page_overlay').style.left = theCenter+"px";
		}
	}
	
	//returns the top position of the scroll status, and add X pixels to determine X position of the overlay
	function getScroll(){
		if (document.documentElement && document.documentElement.scrollTop){
			theTop = document.documentElement.scrollTop;
		} else if (document.body){
			theTop = document.body.scrollTop
		} else{
			theTop = window.pageYOffset;
		}
		return theTop;
	}
	
	//
	// 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;
	}
	
	
	//Browser detection class
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();
	
	function validateEmail(theEmail) {
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theEmail)){
			errorMsg(0);
		} else{
			errorMsg("some email error here");
		}
	}
	
	function errorMsg(msg){
		var errorItem = document.getElementById("errorMsg");
		if(msg){	
			errorItem.innerHTML = "<div class=\"error\">" + msg + "</div>";
			errorItem.style.display = "";
		} else{
			errorItem.style.display = "none";
		}
	}

