a4Tracker = {
	// internal vars, can be overwritten
	url					: 'scripts/tracker/ajax_target/form.php',
	debug				: true,
	
	// makes the tracker pop-up
	showTracker : function(){
		if(!document.getElementById('a4tracker_overlay')){
			var mainDiv = document.createElement('div');
			mainDiv.className = 'pop_overlay';
			mainDiv.id = 'a4tracker_overlay';
			mainDiv.style.display = 'none';
			mainDiv.setAttribute('style',' position: fixed; top: 0; left: 0; background: url(\'scripts/tracker/img/dimmer_bg.png\') repeat; width: 100%; height: 150%; z-index:9999;');
			var contentDiv = document.createElement('div');
			contentDiv.id = 'tracking_inner';
			contentDiv.innerHTML = 'Tracking Stuff Here!';
			
			// STYLES FOR THE INNER DIV
			contentDiv.setAttribute('style','width: 400px; margin-right: auto; margin-left: auto; position: relative; background: #393939; text-align: center; padding: 10px 30px; color: white; -webkit-border-radius: 10px; -moz-border-radius: 10px; -o-border-radius: 10px; border-radius: 10px; font-size:15px;');
			
			// add em in
			mainDiv.appendChild(contentDiv);
			window.document.body.appendChild(mainDiv);
			
			// align inner DIV to center ( w x h ) of client screen
			var viewportwidth;
			var viewportheight;
			if (typeof window.innerWidth != 'undefined'){
				viewportheight = window.innerHeight
			}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
				viewportheight = document.documentElement.clientHeight
			}else{
				viewportheight = document.getElementsByTagName('body')[0].clientHeight
			}			
			viewportheight = viewportheight / 2;
			var halfContent = Number(contentDiv.style.height.replace('px','')) / 2 + 100;
			viewportheight -= halfContent;			
			document.getElementById('tracking_inner').style.top = viewportheight + 'px';
		
			// fetch the required content
			a4Tracker.ajaxLoad(a4Tracker.url,'','tracking_inner',false,false);
			function fadeInTracker(){
				var fade = 0;
				var logo = document.getElementById('a4tracker_overlay').style;
				var ms = (logo.opacity == 0) ? 0:1;
				var pace = setInterval(Fade,10);
				function Fade(){
					if(fade<100){
						fade+=5;
						if(ms){
							logo.filter='alpha(opacity=' + vfade + ')';
						}else{
							logo.opacity=(fade/100);
						}
					}else{ 
						clearInterval(pace)
					}
				};
			}
			
			// show em
			fadeInTracker(); //mainDiv.style.display = 'block';
		}
	},
	
	// checks if the tracker SHOULD pop-up
	isIndexLoad : function(){
		if (document.cookie.indexOf('a4tracker') == -1){
			var file_name = document.location.href;
			var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
			var curFilename =  file_name.substring(file_name.lastIndexOf("/")+1, end);
			switch(curFilename){
				default :
					return a4Tracker; 
				break;
				
				case 'details.php'			:	
				case 'quote.php'				:
				case 'quick-quote.php' 	:
				case 'contact.php'			:	
					return false;
				break;
			}
		}else{
			return false;
		}
	},
	
	// laods the pop-ups content via a GET Ajax call
	//	l	=	URL
	//	d	= DATA
	//	u	= ELEMENT
	//	p = POST ( true / false )
	// 	e = EVAL result as Javascript / JSON
	ajaxLoad : function(l,d,u,p,e){
		try{
			r = new XMLHttpRequest();
		}catch(e){
			try {
				r = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				r = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
		
		if(r){
			r.onreadystatechange = function() {
				if (r.readyState == 4 && r.status == 200){
					if(u != '' && u != false && u != null && typeof(u) != 'undefined'){
						document.getElementById(u).innerHTML = r.responseText;
					}
					if(e != '' && e != false && e != null && typeof(e) != 'undefined'){
						eval('(' + r.responseText + ')');
					}
					
					// attach ajax inliner scripts to the head of the current document
					var search = r.responseText; 
					var script; 
								
					while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)){ 
						search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length); 
						if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break; 
					
						block = search.substr(0, search.indexOf(RegExp.$1)); 
						search = search.substring(block.length + RegExp.$1.length); 
						 
						var oScript = document.createElement('script'); 
						oScript.text = block; 
						oScript.language = 'javascript';
						oScript.type = 'text/javascript';
						document.getElementsByTagName("head").item(0).appendChild(oScript); 
					} 
				}
			}
			if(p){
				r.open('POST', l, true);
				r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			}else{
				r.open('GET', l+'?'+d, true);
			}
			r.send(d);
		}
	}
}

window.onload = function(){ 
	var d = new Date();
	var today = d.getDate();
	if(today == 0){
		today = 1;
	}
	if(today == 6){
		today = 7;
	}
	
	if(today%2){	// only show the popup on an odd day of the week
		if(vele = a4Tracker.isIndexLoad()){
			vele.showTracker();
		}
	}
};

