// pop up window part

var popupwindow;

function popUp(name, filename){

	if ( (typeof popupwindow == "object") && (popupwindow.closed != true) )

	{

		popupwindow.location.href = filename;

		popupwindow.focus();

	}

	else

	{

		popupwindow = window.open(filename,name,'height=465,width=330,left=240,top=10,scrollbars=yes,resizable=no');

		if (popupwindow != null || popupwindow != undefined)
		{
			popupwindow.focus();
		}		

	}

}

// pop under window part

function popUnder(name, filename){

	popupwindow = window.open(filename,name,'height=600,width=800,left=150,top=150,scrollbars=yes,resizable=yes');

	if (popupwindow != null || popupwindow != undefined)
	{
		popupwindow.blur();
		window.focus();
	}
	

}

function popUnderEx(name, filename){

	popupwindow = window.open(filename,name,'height=600,width=800,left=150,top=150,scrollbars=yes,resizable=yes');

	if (popupwindow != null || popupwindow != undefined)
	{
		popupwindow.blur();
	}
	

	//window.focus();

}



// coookie related stuff

function DeleteCookie(name){

	var exp = new Date();

	exp.setTime (exp.getTime() - 1);

	var cval = GetCookie (name);

	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}



function getCookieVal(offset){

	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1)

		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));

}



function SetCookie(name, value){

	var argv = SetCookie.arguments;

	var argc = SetCookie.arguments.length;

	var expires = (argc > 2) ? argv[2] : null;

	var path = (argc > 3) ? argv[3] : null;

	var domain = (argc > 4) ? argv[4] : null;

	var secure = (argc > 5) ? argv[5] : false;

	document.cookie = name + "=" + escape (value) +

		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +

		((path == null) ? "" : ("; path=" + path)) +

		((domain == null) ? "" : ("; domain=" + domain)) +

		((secure == true) ? "; secure" : "");

	//alert ("cookie = " + document.cookie);

}



function GetCookie(name){

	var arg = name + "=";

	var alen = arg.length;

	var clen = document.cookie.length;

	var i = 0;

	while (i < clen)

	{

		var j = i + alen;

		if (document.cookie.substring(i, j) == arg)

			return getCookieVal(j);

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0) break;

	}

	return null;

}



function CheckCookieSetting(){

	// check if cookie is there

	var the_cookie_info = GetCookie('IGautoCookie1');

	if (the_cookie_info == null) {

		// no cookie, then set one

		var expDate = new Date();

		expDate.setTime (expDate.getTime() + Adfreetime);

		SetCookie('IGautoCookie1',expDate,expDate);

		return false;

	}

	else {

		return the_cookie_info;

	}

}



function DoPopUp(){

	var expDate = CheckCookieSetting();

	if (expDate == false) {

		popUp('pop', AdURL);

	}

	else {

		// check the current time to see if we need to show ad

		var nowDate = new Date();

		var nowSeconds = Date.parse(nowDate);

		var expSeconds = Date.parse(expDate);

		if (nowSeconds < expSeconds) {

			// nothing to do within the ad-free time

		}

		else {

			// do the pop up again for over due time

			popUp('pop', AdURL);

			nowDate.setTime (nowDate.getTime() + Adfreetime);

			SetCookie('IGautoCookie1',nowDate,nowDate);

		}

	}

}



function DoPopUnder(){

	var expDate = CheckCookieSetting();

	if (expDate == false) {

		popUnder('pop', AdURL);

	}

	else {

		// check the current time to see if we need to show ad

		var nowDate = new Date();

		var nowSeconds = Date.parse(nowDate);

		var expSeconds = Date.parse(expDate);

		if (nowSeconds < expSeconds) {

			// nothing to do within the ad-free time

		}

		else {

			// do the pop up again for over due time

			popUnder('pop', AdURL);

			nowDate.setTime (nowDate.getTime() + Adfreetime);

			SetCookie('IGautoCookie1',nowDate,nowDate);

		}

	}

}

function DoPopUnderEx(){

	var expDate = CheckCookieSetting();

	if (expDate == false) {

		popUnderEx('pop', AdURL);

	}

	else {

		// check the current time to see if we need to show ad

		var nowDate = new Date();

		var nowSeconds = Date.parse(nowDate);

		var expSeconds = Date.parse(expDate);

		if (nowSeconds < expSeconds) {

			// nothing to do within the ad-free time

		}

		else {

			// do the pop up again for over due time

			popUnderEx('pop', AdURL);

			nowDate.setTime (nowDate.getTime() + Adfreetime);

			SetCookie('IGautoCookie1',nowDate,nowDate);

		}

	}

}

function DoSetVisitedCookie()

{

	var expDate = new Date();

	expDate.setTime (expDate.getTime() + Adfreetime);

	SetCookie('IGautoCookie1',expDate,expDate);

}



