// Copyright Acro Media Inc. 1998-2009, www.acromediainc.com

var copyrightYear = {
	init: function(){
		if(document.getElementById('insertYear')) document.getElementById('insertYear').innerHTML = new Date().getFullYear();
	}
}

var externalLinks = {
	init: function(){
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") anchors[i].target = "_blank";
		}
	}
}

/*
var externalImageMapLinks = {
	init: function(){

		if (!document.getElementById) return;

		var imgMap1 = document.getElementById("imgMap1");
		var imgMap2 = document.getElementById("imgMap2");
		var imgMap3 = document.getElementById("imgMap3");
		var imgMap4 = document.getElementById("imgMap4");
		var imgMap5 = document.getElementById("imgMap5");
		var imgMap6 = document.getElementById("imgMap6");
		var imgMap7 = document.getElementById("imgMap7");
		var imgMap8 = document.getElementById("imgMap8");
		var imgMap9 = document.getElementById("imgMap9");
		var imgMap10 = document.getElementById("imgMap10");

		imgMap1.target = "_blank";
		imgMap2.target = "_blank";
		imgMap3.target = "_blank";
		imgMap4.target = "_blank";
		imgMap5.target = "_blank";
		imgMap6.target = "_blank";
		imgMap7.target = "_blank";
		imgMap8.target = "_blank";
		imgMap9.target = "_blank";
		imgMap10.target = "_blank";
	}
}
*/

var cookie_enable = false;

function check_cookie_enable()
{
	var expiry = new Date();
	expiry.setHours(expiry.getHours()+1);
	setCookie('rfTestCookie', 'none', expiry);

	// if getCookie succeeds, cookies are enabled, since the cookie was successfully created.
	testCookie = getCookie('rfTestCookie');
	cookie = getCookie(name);
	if(!(testCookie == null || testCookie == ""))
	{
		cookie_enable = true;
		deleteCookie('rfTestCookie');
	}
	// if the getCookie test fails, cookies are not enabled for this session.
	else
	{
		cookie_enable = false;
	}
}

function checkVisitLength()
{
	check_cookie_enable();

	if(cookie_enable == true)
	{
		/*
		rfAlertPopped = getCookie('rfAlertPopped');
		if(rfAlertPopped == null || rfAlertPopped == "")
		{
			alert("We are currently experiencing problem with our phone system. If you need an assistance,\nfor tech support: please open a ticket through our Customer Service Center (https://support.rackforce.com)\nfor sales support: please use the live chat button or send an email to sales@rackforce.com.\n Thank you.");
			var expiry = new Date();
			expiry.setHours(expiry.getHours()+12);
			setCookie('rfAlertPopped', 1, expiry);
		}
		*/

		var TimezoneOffset = -7;
		var localTime = new Date();
		var milliseconds = localTime.getTime() + (localTime.getTimezoneOffset() * 60000) + TimezoneOffset * 3600000;

		var pstTime = new Date(milliseconds);
		var day = pstTime.getDay();
		var hour = pstTime.getHours();

		// Sales hours:
		// Monday to Friday 8:00am to 4:00pm
		if( day > 0 && day < 6 ) // if Monday to Friday
		{
			if( hour >= 8 && hour <= 15 )
			{
				rfVisitTime = getCookie('rfVisitTime');
				if(!(rfVisitTime == null || rfVisitTime == ""))
				{
					rfChatPopped = getCookie('rfChatPopped');
					if(rfChatPopped == null || rfChatPopped == "")
					{
						trackLengthOfStay();
					}
				}
				else
				{
					var now = new Date();
					var expiry = new Date();
					expiry.setHours(expiry.getHours()+12);
					setCookie('rfVisitTime', now, expiry);
				}
			}
		}
	}
}

function getCookie(name)
{
	if (document.cookie.length > 0)
	{
		start_index = document.cookie.indexOf(name + "=");

		if (start_index != -1)
		{
			start_index = start_index + name.length + 1;
			end_index = document.cookie.indexOf(";", start_index);
			if (end_index == -1)
			{
				end_index = document.cookie.length;
			}
			return unescape(document.cookie.substring(start_index,end_index));
		}
	}

	return "";
}

function setCookie(name, value, expiry)
{
	document.cookie = name + "=" + escape(value) + ";expires=" + expiry + ";path=/";
}

function deleteCookie(name)
{
	cookie = getCookie(name);
	if(!(cookie == null || cookie == ""))
	{
		document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/";
	}
}

function trackLengthOfStay()
{
	rfVisitTime = getCookie('rfVisitTime');
	if(!(rfVisitTime == null || rfVisitTime == ""))
	{
		var now = new Date();
		now_in_ms = now.getTime();
		rfVisitTime_in_ms = Date.parse(rfVisitTime);

		diff_in_ms = now_in_ms - rfVisitTime_in_ms;
		if(diff_in_ms > 30000)
		{
			var expiry = new Date();
			expiry.setHours(expiry.getHours()+12);
			setCookie('rfChatPopped', 1, expiry);

			// pop the chat window
			initializeChat();
		}
	}
	else
	{
		checkVisitLength();
	}

	rfChatPopped = getCookie('rfChatPopped');
	if(rfChatPopped == null || rfChatPopped == "")
	{
		setTimeout(trackLengthOfStay, 1000);
	}
}

function initializeChat()
{
	var chatDiv = document.getElementById("initiate_chat_image");

	// get window width
	/*
	var window_width = 0;
   	if (self.innerHeight)
    {
   		window_width = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	window_width = document.documentElement.clientWidth;
    }
    else if (document.body)
    {
    	window_width = document.body.clientWidth;
    }
    */

    // get window height
    var window_height = 0;
    if (self.innerHeight)
    {
    	window_height = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	window_height = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
    	window_height = document.body.clientHeight;
    }

    //chatDiv.style.position = "fixed";
    //chatDiv.style.top = ((screen.height)?(screen.height-window_height)/2 + "px" : "350px");
	chatDiv.style.display = "";
}

function desposeChat()
{
	var chatDiv = document.getElementById("initiate_chat_image");
	chatDiv.style.display = "none" ;
}

function siteOnload(){
	copyrightYear.init();
	externalLinks.init();
  	//externalImageMapLinks.init();
  	checkVisitLength();
}

function addLoadEvent(func){
	var oldOnLoad = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		window.onload = function(){
			oldOnLoad();
			func();
		}
	}
}

addLoadEvent(siteOnload);
