//****************************
// page popup functions
//****************************
var ns4=document.layers;
var ie4=document.all;
var ns6=document.getElementById&&!document.all;

function hidePop(){
	var adPop=ns6? document.getElementById("popDiv") : document.all.popDiv
	if (ie4||ns6){
		adPop.style.visibility="hidden"
	}
	else if (ns4){
		document.popDiv.visibility="hide"
	}
	//unload data from iFrame
	if(document.getElementById("quizFrame")){
		document.getElementById("quizFrame").src = "";
	}
}

function showPop(){
	var adPop=ns6? document.getElementById("popDiv") : document.all.popDiv
	if (ie4||ns6){
		adPop.style.visibility="visible"
	}
	else if (ns4){
		document.popDiv.visibility="show"
	}
}

//****************************
// iFrame quiz/poll functions
//****************************
//determine IE vs. other browsers
var isIE;
var isMOZ;

if(navigator.userAgent.indexOf("MSIE") != -1){	isIE = true; }
else{ isMOZ = true; }

function resizeDiv(div_id,w,h){
	if(!w){ w = 450; }; //default values
	if(!h){ h = 450; };
	if(isMOZ){ w += 10; h +=30; } //compensate for the scrollbars
	if(div_id){
		document.getElementById(div_id).style.width = w + "px";
		document.getElementById(div_id).style.height = h + "px";
	}
}

//loads data into the iframe that is on the popup
function loadIframeData(in_url,div_id,w,h){
	if(!w){ w = 450; }; //default values
	if(!h){ h = 450; };

    //scroll page to top
    self.scrollTo(0, 0);

    if (document.getElementById){
		qFrame = document.getElementById("quizFrame");
			qFrame.style.width = w + "px";
			qFrame.style.height = h + "px";
			qFrame.src=in_url;

		resizeDiv(div_id,w,h);
		showPop();
	}
	else{
		//open quiz in a normal popup.
		window.open(in_url,"quiz","height=200,width=400,status=no,resizable=yes,toolbar=no,menubar=no,location=no");
	}
}

function loadEmailFriend(link,brand,host,div,w,h) {
	loadIframeData('/'+brand+'/email/emailFriend.jsp' + '?pageUrl=http://'+escape(host+link),div,w,h)
}

function loadEmailFriend(link,addparams,brand,host,div,w,h) {
	loadIframeData('/'+brand+'/email/emailFriend.jsp' + '?' + addparams + '&pageUrl=http://'+escape(host+link),div,w,h)
}

//****************************
//  methods to set/get cookies
//****************************
function Set_Cookie( name, value, expires, path, domain, secure ){
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct expires time, the current script below will set
    it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires ){
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ){
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//*********************************************
//  prevents really really long email addresses
//*********************************************
function shortenString(max, text) {
    max--;
    text = "" + text;
    var temp = ""; var chcount = 0;
    for (var i = 0; i < text.length; i++){
    var ch = text.substring(i, i+1); // first character
    var ch2 = text.substring(i+1, i+2); // next character
    // if character is a hard return
    if (ch == '\n'){ temp += ch; chcount = 1; }
    else{
        // line has max chacters on this line
        if (chcount == max){ temp += '<br/>' + ch; chcount = 1; }
        else{ temp += ch; chcount++; }
    }
    }
    return (temp); // sends value of temp back
}