//**********************CONFIGS***************************

//To turn on and off the interstitial or floater set the variable to 1 for OFF and 0 for ON
var hideFloat = 1;
var hideInter = 1;
//set the two vars to the number of seconds before timeout to hide the elements
var interTimeout = 20;
var floaterTimeout = 10;
// set the two vars to the number of days the cookie should expire
var floaterDays = 8;
var interDays = 1;

//********************************************************

//This will set a function to be triggered for the onLoad event of body equivalant of hard coding onLoad=somefunction(); in the body tag
function addLoadEvent(func,e,s) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func(e,s);
		}
	}
}
//Counter for Interstitial
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function InitializeTimer(s)
{
    secs = s;
    StopTheClock();
    StartTheTimer();
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false;
}

function StartTheTimer()
{
    if (secs==1)
    {
        StopTheClock();
        hideIt("inter");
    }
    else
    {
        self.status = secs;
        secs = secs - 1;
        timerRunning = true;
        document.getElementById('secondHolder').innerHTML=secs;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}


//Sets an element to hidden based on the ID that you pass it.
function hideIt(e) {
	if (!document.getElementById) return false;
	if (!document.getElementById(e)) return false;
	var mask = document.getElementById(e);
	if (mask.style.display != 'none') {
		mask.style.display = 'none';
	}
	
}
//Sets an element to Visible based on the ID that you pass it.
function showIt(e,s) {

	if (!document.getElementById) return false;
	if (!document.getElementById(e)) return false;
	var mask = document.getElementById(e);
	if (mask.style.visibility != 'visible' || mask.style.display != 'block') {
		mask.style.visibility = 'visible';
		mask.style.display = 'block';
	}
		//if the seconds arent passed set it to 10
	if (s==null||s==""||s==undefined){
			s=10000;
					}
	//if the element is inter update the div with the seconds
	if (e == "inter"){
	InitializeTimer(interTimeout);
		}				
if(s != "neverHide"){
enoughalready = setTimeout("hideIt('"+e+"')", s);

}
}

//Validation for email field
function validate_required(field,alerttxt){
	with (field)	{
		if (value==null||value==""){
			alert(alerttxt);
			return false;
		}
		else{
			return true;
		}
	}
}

function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			alert(alerttxt);
			return false;
			alert("should return false");
		}
		else {return true;}
	}
}
//Validate the field with the above 2 functions, if  returned true then set action of form and sed it to clickshare.
function addEmailSubmit(thisform){
	with (thisform){
		var formid = thisform.id;

	 if(formid == "reportForm"){
			var CSaction = "confirmFreeReport";

		}
 	 if(formid == "printForm"){
		 var CSaction = "requestFreeIssues";

	 }
 if (validate_email(user_email,"Valid Email address is required.")==false){
        user_email.focus();
  		return false;
  	}
	else{
		var user_emailStr = thisform.user_email.value;
		thisform.action = "https://home.rcrwireless.com/clickshare/"+CSaction+".do?CSUsername="+user_emailStr+"";
		thisform.submit();
		return true;
		}
	}
}

//Check to see if the user has recieved an ofie in the last 7 days. If they havent show the ofie and set the cookie with seven days from the current day, otherwise skip ofie and move forward with page load or interstitial.
var showingFloat = 0;
var csUser = readCookie("CSDemos");
var str = "nameFirst:John-Reg|nameLast:Fouse|age:0|gend:0|FreeRpt=";

if (csUser != null){
var tmp = csUser.split("|");
var tmp0 = tmp[0].split(":");
var nameFirst = tmp0[1];
}
else{
addLoadEvent(showIt, "ofieplaceholder" , "neverHide");
}

var cookedFloat = readCookie("floaterRCR");
var cookedInter = readCookie("interstitialRCR");
if(cookedFloat == null && csUser == null && hideFloat == 0){
createCookie("floaterRCR", "1", floaterDays);
s=floaterTimeout*1000;
addLoadEvent(showIt,"mask",s);
showingFloat = 1;
}
//Check to see that the floater wasnt just shown and they havent seen the interstitial in 1 day. if they havent just recieved the ofie then show interstial and set cookie to block the interstitial for 1 day.
else if(cookedInter == null && showingFloat == 0 && hideInter == 0){
createCookie("interstitialRCR", "1", interDays);
s=interTimeout*1000;

addLoadEvent(showIt,"inter",s);
}


//Swap Divs for OFIE
function swapDivs() { 
     // get the empty div 
     var a = document.getElementById("ofieplaceholder"); 
     var b = document.getElementById("ofieparent");
     a.innerHTML = b.innerHTML;
}
