var host="http://www.poppydog.com";
//var host="http://localhost:9090";
//var host='http://192.168.0.2:9090';
var platformCheck = false;
var isOfficeLive = false;
var pollRes;
var buyabeerWin;
var boughtabeer=new Object();
var debug_once=0




function pd_buyabeer(tiid,beerCount,i){
document.write('<a href="javascript:openbuyabeer3(\''+tiid+'\');"><img src="'+i+'" title="Its free!" border="0"/></a><br />');
document.write('<b>Beers bought: '+beerCount+'</b><br />');
}


function openbuyabeer3(tiid){
//if (boughtabeer[tiid]){
if (document.cookie.indexOf("pd_buyabeer_"+tiid)>-1){
	alert("No thanks, still drinking the first beer");
	return;
}
boughtabeer[tiid]=1;
if (buyabeerWin){
	buyabeerWin.close();	
	}else{
		buyabeerWin = window.open('','buyabeer');
		buyabeerWin.close();
	}
	var d = new Date();
	buyabeerWin = window.open("/buyabeer.do?tiid="+tiid+"&asdft="+d.getMilliseconds(),"buyabeer","menubar=yes,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=200,height=200");
	buyabeerWin.focus();
}



//POLL
function openpollw(f){
if (pollRes){
	pollRes.close();	
	}else{
		pollRes = window.open('','pollRes');
		pollRes.close();
	}
	pollRes = window.open("","pollRes","menubar=yes,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=700,height=450");
	pollRes.focus();
	f.submit.disabled=true;
	return true;
}




function pd_poll_t(hid,tiid,title,borderColor,titleText,titleBg,titleSize,bodyText,bodyBg,bodySize,width){
document.write('<style type="text/css">');
document.write('.poll {padding:5px 0px 5px 0px;}');
document.write('.poll div {margin-bottom:3px}');
document.write('.poll h1 {padding:5px;text-align:center;margin:0px;border-width: 1px 1px 0px 1px;border-style:solid}');
document.write('.poll .body {padding:5px;text-align:left;}');
document.write('.poll .powered {font-size:10px;margin-bottom:0px;text-align:center}');
document.write('</style>');
document.write('<form action="'+host+'/poll.do" method="post" onSubmit="openpollw(this);" target="pollRes">');
document.write('<input type="hidden" name="tiid" value="'+tiid+'">');
document.write('<input type="hidden" name="k" value="'+hid+'">');
document.write('<input type="hidden" name="action" value="vote">');
document.write('<table cellpadding="0" cellspacing="0" border="0"><tr><td>');
document.write('<div class="poll">');
document.write('<h1 style="width:'+(width-2)+'px;background-color:'+titleBg+';color:'+titleText+';font-size:'+titleSize+';border-color:'+borderColor+'">'+title+'</h1>');
document.write('<div class="body" style="font-size:'+bodySize+'; width:'+(width-2)+'px;background-color:'+bodyBg+';color:'+bodyText+';border:1px '+borderColor+' solid; ">');

}
 

function pd_poll_q(question){
document.write('<div>'+question+'</div>');
}

function pd_poll_r(type,val,label){
document.write('<div><input type="'+type+'" name="resp" value="'+val+'"/>  '+label+'</div>');
}


function pd_poll_b(hid,tiid,viewRes,labelVote,labelView,borderColor,bodySize,bodyText){
document.write('<div style="text-align:center;margin-top:10px">');
document.write('<input type="submit" name="submit" style="border:1px '+borderColor+' solid; margin-bottom:3px;font-size:'+bodySize+'" id="thebutton'+tiid+'" value="'+labelVote+'"/>');
if (viewRes)
document.write('<br><a href="'+host+'/poll.do?action=viewRes&tiid='+tiid+'&k='+hid+'" onclick="openpollw();" target="pollRes" style="color:'+bodyText+';">'+labelView+'</a>');
document.write('</div>');
}


function pd_poll_f(isProf,textColor){
if (!isProf)
document.write('<div class="powered"><a href="'+host+'/prod/poll.jsp" style="color:'+textColor+'" target="_blank">powered by poppydog.com</a></div>');
document.write('</div>');
document.write('</div>');
document.write('</td></tr></table></form>');
doFixIt("write");
}


//END POLL


function showLoginMessage(){
document.write(getShowLoginMessage());
	doFixIt("write");
}



function getShowLoginMessage(){
var res='';
var args=pd_getArgs();

	if (document.location.search.indexOf("pd_invalidLogin=")>-1){
		if (document.location.search.indexOf("pd_invalidLogin=u")>-1){
			res +='Respond to the email sent to you <a href="'+args.pd_resendConf+'" onClick="window.open(\'\',\'resend\',\'width=580,height=450\');" target="resend">(help)</a>';
		}
		else if (document.location.search.indexOf("pd_invalidLogin=a")>-1){
			res+='Your registration hasn\'t been approved yet';
		}
		else{
			res+='Login details are incorrect';
		}
	}
	return res;
}




function pd_validate(f, nou, messageHandler){
//note: this is checked on the server side as well
var isValid=true;
var isUrl=false;
	for (var i=0; i<f.elements.length; i++){
		if (f.elements[i].name.indexOf("qrm_")>-1||f.elements[i].name.indexOf("qr_")>-1||f.elements[i].name.indexOf("form_image")>-1||f.elements[i].name.indexOf("tac")>-1){	
			document.getElementById("e_"+f.elements[i].name).style.visibility="hidden";
			if (!isChecked(f.elements[i],f)){
				document.getElementById("e_"+f.elements[i].name).style.backgroundColor = "#FF0000";
				document.getElementById("e_"+f.elements[i].name).style.color = "#FFFFFF";
				document.getElementById("e_"+f.elements[i].name).style.visibility = "visible";
				isValid=false
			}
		}
		if (nou && f.elements[i].value.indexOf("href=")>-1){
			isUrl=true;
		}

	}
	
	
	if (!isValid){
		if (!messageHandler)
				alert("You haven't filled in all the required fields");
			else 
				messageHandler("You haven't filled in all the required fields",1);
				return false;
			}
			
				else if (isUrl){
				if (!messageHandler)
				alert("No links allowed in the form");
				else
				messageHandler("No links allowed in the form",1);
				return false;
			}

			
	return true;	
}

function pd_validate2(f, nou){
//note: this is checked on the server side as well
var isValid=true;
var isUrl=false;
	for (var i=0; i<f.elements.length; i++){
		if (f.elements[i].name.indexOf("qrm_")>-1||f.elements[i].name.indexOf("qr_")>-1||f.elements[i].name.indexOf("form_image")>-1||f.elements[i].name.indexOf("tac")>-1){	
			document.getElementById("e_"+f.elements[i].name).style.visibility="hidden";
			if (!isChecked(f.elements[i],f)){
				document.getElementById("e_"+f.elements[i].name).style.backgroundColor = "#FF0000";
				document.getElementById("e_"+f.elements[i].name).style.color = "#FFFFFF";
				document.getElementById("e_"+f.elements[i].name).style.visibility = "visible";
				isValid=false;
			}
			else if (f.elements[i].name.indexOf("form_image")>-1){
				if (document.getElementById("pd_f_i_v")&&document.getElementById("pd_f_i_v").value!=f.elements[i].value.toLowerCase()){
				document.getElementById("e_"+f.elements[i].name).style.backgroundColor = "#FF0000";
				document.getElementById("e_"+f.elements[i].name).style.color = "#FFFFFF";
				document.getElementById("e_"+f.elements[i].name).style.visibility = "visible";
				isValid=false;
				}
			}
		}
		if (nou && f.elements[i].value.indexOf("href=")>-1){
			isUrl=true;
		}

	}
	
	
	if (!isValid){
				alert("You haven't filled in all the required fields");
				return false;
			}
			
				else if (isUrl){
				alert("No links allowed in the form");
				return false;
			}

			
	return true;	
}


function isChecked(c,f){
	var checked=false;
	if (c.type.indexOf("select")>-1){
		checked = (c.selectedIndex>0);
	}
	else if (c.type.indexOf("radio")>-1 || c.type.indexOf("check")>-1){
	
	for (var j=0; j<f.elements.length; j++){
			if (f.elements[j].name==c.name && f.elements[j].checked){
				checked=true;			
				break;
			}
	}
	}
	else{
		checked =  (c.value.length>0);
	}
	return checked;

}


function pd_submitnou(f){
//note: this is checked on the server side as well
	for (var i=0; i<f.elements.length; i++){
		//if (f.elements[i].type == 'textarea' && f.elements[i].name.indexOf("_nou")>-1){
			if (f.elements[i].value.indexOf("href=")>-1){
				alert("No links allowed in the form");
				return false;
			}
		//}
	}
	return true;	
}

function pd_templateFwd(){
	var args=pd_getArgs();
	if (args.pd_test1234qzz){
		alert("this is a test");
		return;
	}
	
	var fwd = host+"/blank.html";
	if (args.fwd) fwd=args.fwd;	
		document.write("<iframe name='pd_templateFrame' src='"+fwd+"' width='99%' height='3500' frameborder='0' marginheight='0' marginwidth='0' align='center'></iframe>");
	
	doFixIt("write");	
}


function pd_logoffPP(hid) {
var exp= new Date();
        	var t=exp.getTime()-100;
           	exp.setTime(t);

	document.cookie="pp_"+hid+"=; path=/; expires="+exp.toGMTString();
	document.location.reload();
}

function pd_isOnPP(hid) {
var cookieVal = pd_getCookie("pp_"+hid);
				if (cookieVal==null||cookieVal=="")
					return false;
				else 
				return true;
}
					




function pd_isPP(hid,pid) {
	if (document.location.search.indexOf("pd_1wdrftg")>-1){
			var args=pd_getArgs();	
			if (args.pd_wedfrtg){
				var pairs= args.pd_wedfrtg.split("|");
				var cookieVal = pd_getCookie("pp_"+hid);
				if (cookieVal==null)
					cookieVal="";
    			for (var i=0; i<pairs.length; i++){
    				if (cookieVal.indexOf("|"+pairs[i]+"|")<0){
						cookieVal = cookieVal+pairs[i]+"|";
						}
    			}
			}
			
			
			
	         document.cookie="pp_"+hid+"="+cookieVal+"; path=/;";
	         var next = document.location.href.substring(0,document.location.href.indexOf("pd_1wdrftg")-1);
	         
	        try{
			var iframes = top.document.getElementsByTagName("iframe");
			for (var i=0; i< iframes.length; i++){
				if (iframes.item(i).id.indexOf("CPH_Content_HtmlControl")>-1){
					top.location.replace(nexth);	
					return;
				}
			}
			}
			catch(e){}
	         
	         document.location.replace(next);
	}
	
	var theCookie = pd_getCookie("pp_"+hid);
	if (theCookie!=null && theCookie.indexOf("|"+pid+"|")>-1){
		return true;
	}else{	
		var next = host+"/pp/login.do?tiid="+hid+"&noc=t&nof=t";
		
		var iframes = top.document.getElementsByTagName("iframe");
		for (var i=0; i< iframes.length; i++){
			if (iframes.item(i).id.indexOf("CPH_Content_HtmlControl")>-1){
				top.location.replace(next);	
				return false;
			}
		}
		
		if (parent.frames && parent.frames.pd_pp_outer){
			parent.location.replace(next);			
		}else{
			document.location.replace(next);
		}
			
		return false;
	}
		
}


function pd_showLoggedOn(hid,msg){
	var usern = pd_getUsername(hid);
	if (usern!=null)
		document.write(msg+usern);
		
	doFixIt("write");
	
}

function pd_showUpgrade(hid,msg){
	if (pd_isLoggedOn(hid)&&!pd_isFullMember(hid))
		document.write("<a href="+host+"/reg/payment/member.do?action=upgrade&hid="+hid+">"+msg+"</a>");
	
	doFixIt("write");
	
}


function pd_isFullMember (hid) {
	if (document.cookie.indexOf(hid+"_FULL")>-1)
		return true;
	else
		return false;
}



function pd_getUsername(hid){
	if (pd_isLoggedOn(hid))
		return pd_getCookie(hid+"_USERN");	
}


/*
function pd_showPhoto(hid){
	if (pd_isLoggedOn(hid)){
		//var photo = pd_getCookie(hid+"_PHOTO");		
		//if (photo!=null){
			//document.write("<img src=\""+photo+"\" border=\"0\"/>");
		//}
			var uid = pd_getCookie(hid+"_USER");		
		if (uid!=null){
			document.write("<img src=\""+host+"/images/imagestore/hsts/"+hid+"/visitors/"+uid+"_profPhoto_th.jpg\" width=\"16\" border=\"0\"/>");
		}
	}

	doFixIt("write");
}
*/

function pd_showPhoto(hid,size){
	if (pd_isLoggedOn(hid)){
		if (size){
		var photo = pd_getCookie(hid+"_PHOTO");		
		if (photo!=null){
			document.write('<img src="'+photo+'" border="0" width="'+size+'"/>');
		}
		}
		else{
		var photo = pd_getCookie(hid+"_PHOTO");		
		if (photo!=null){
			document.write('<img src="'+photo+'" border="0" />');
		}
		/*
			var uid = pd_getCookie(hid+"_USER");		
		if (uid!=null){
			document.write("<img src=\""+host+"/images/imagestore/hsts/"+hid+"/visitors/"+uid+"_profPhoto_th.jpg\"  width=\"50\" height=\"50\" border=\"0\"/>");
		}*/
		}
		
	}

	doFixIt("write");
}


function pd_isLoggedOn (hid) {
	if (document.cookie.indexOf("poppydogl_"+hid)>-1)
		return true;
	else
		return false;
}

function pd_isSubscribed (hid,cid) {
var theCookie = pd_getCookie(hid+"_CID");
if (theCookie!=null && theCookie.indexOf(":"+cid+":")>-1)
		return true;
	else
		return false;
}

function pd_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 pd_getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }

function pd_getCookieVal (offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
 endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}




function getObject(id){
var obj1;

if (document.all) 
	obj1= document.all[id];
else if (document.getElementById)
	obj1 =document.getElementById(id);
else
	obj1=eval("document."+id);

if (document.layers)
	return obj1;
else
	return obj1.style;
	

}


function pd_hide(id){
	var theObj =getObject(id);
	theObj.visibility="hidden";
}


function pd_show(id){
	var theObj =getObject(id);
	theObj.visibility="visible";
}


function pd_getArgs(){
	var args = new Object();
    	var query= document.location.search.substring(1);
    	var pairs= query.split("&");
    	for (var i=0; i<pairs.length; i++){
		var pos =pairs[i].indexOf("=");
		if (pos<0) continue;
		var argname = pairs[i].substring(0,pos);
		var value= pairs[i].substring(pos+1);
		args[argname]=unescape(value);		
	}
	return args;
}

function doPlatformCheck(){
	platformCheck=true;
try{
			var iframes = top.document.getElementsByTagName("iframe");
			for (var i=0; i< iframes.length; i++){
				if (iframes.item(i).id.indexOf("CPH_Content_HtmlControl")>-1){
					isOfficeLive = true;
				}
			}
			}
			catch(e){isOfficeLive=false;}

}

function doFixIt(action){
	if (!platformCheck)
		doPlatformCheck();
	if (isOfficeLive){
			top.resizeBy(10,10);
			top.resizeBy(-10,-10);
	}
}

function getDefaultTarget(){
	if (!platformCheck)
		doPlatformCheck();
	if (isOfficeLive)
		return "target='_top'";
	return "";
}

function getDefaultLocationObj(){
	if (!platformCheck)
		doPlatformCheck();
	if (isOfficeLive)
		return top.location;		
	return window.location;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/*addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  
});*/


function push(a,val){
a[a.length]=val;
}


function addParamsToUrl(url,p){
var res;
	var hsh = "";
	var i = url.indexOf("#");
	if (i>-1){
		hsh=url.substring(i,url.length);
		url=url.substring(0,i);
	}
	if (url.indexOf("?")>0)
		res = url+"&"+p;
	else
		res = url+"?"+p;
	return res+hsh;
	/*if (url.indexOf("?")>-1)
		return url+"&"+p;
	else
		return url+"?"+p;*/
}



function doPassLand(hid){
var args = pd_getArgs();
if (args.pd_uabqraction && args.pd_uabqraction.indexOf("logoff")>-1){
		var exp= new Date();
           	exp.setTime(exp.getTime()-100);

            	document.cookie=hid+"_CID=; path=/; expires="+exp.toGMTString();
            	document.cookie="pd_ms_s_"+hid+"=; path=/; expires="+exp.toGMTString();
            	document.cookie="poppydogl_"+hid+"=; path=/; expires="+exp.toGMTString();
		document.cookie=hid+"_USER=;path=/; expires="+exp.toGMTString();
		document.cookie=hid+"_USERN=;path=/; expires="+exp.toGMTString();
		document.cookie=hid+"_FULL=;path=/; expires="+exp.toGMTString();
		document.cookie=hid+"_PHOTO=;path=/; expires="+exp.toGMTString();
	}
	else if (args.pd_uabqraction && args.pd_uabqraction.indexOf("logon")>-1){
		document.cookie="poppydogl_"+hid+"=t; path=/";

		if (args.uabqr){
			document.cookie=hid+"_USER="+args.uabqr+"; path=/";
		}
		if (args.uabqrn){
			document.cookie=hid+"_USERN="+args.uabqrn+"; path=/";
		}
		if (args.uabqph){
			document.cookie=hid+"_PHOTO="+args.uabqph+"; path=/";
		}
		if (args.uabqrf){
			document.cookie=hid+"_FULL=t; path=/";
		}
		if (args.uabqrcid){
			document.cookie=hid+"_CID="+args.uabqrcid+"; path=/";
		}

	}
	
	}
	
	
	function doForwardPassLand(){
var args = pd_getArgs();
if (args.uabqrmma){
	top.location.replace(args.uabqrmma);
	return true;
	}
	return false;
}



function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}


function debugOnce(val){
if (debug_once++==0){alert(val);}
}



var month_names_l = new Array ( );
month_names_l[month_names_l.length] = "January";
month_names_l[month_names_l.length] = "February";
month_names_l[month_names_l.length] = "March";
month_names_l[month_names_l.length] = "April";
month_names_l[month_names_l.length] = "May";
month_names_l[month_names_l.length] = "June";
month_names_l[month_names_l.length] = "July";
month_names_l[month_names_l.length] = "August";
month_names_l[month_names_l.length] = "September";
month_names_l[month_names_l.length] = "October";
month_names_l[month_names_l.length] = "November";
month_names_l[month_names_l.length] = "December";

var day_names_l = new Array ( );
day_names_l[day_names_l.length] = "Sunday";
day_names_l[day_names_l.length] = "Monday";
day_names_l[day_names_l.length] = "Tuesday";
day_names_l[day_names_l.length] = "Wednesday";
day_names_l[day_names_l.length] = "Thursday";
day_names_l[day_names_l.length] = "Friday";
day_names_l[day_names_l.length] = "Saturday";


var month_names_s = new Array ( );
month_names_s[month_names_s.length] = "Jan";
month_names_s[month_names_s.length] = "Feb";
month_names_s[month_names_s.length] = "Mar";
month_names_s[month_names_s.length] = "Apr";
month_names_s[month_names_s.length] = "May";
month_names_s[month_names_s.length] = "Jun";
month_names_s[month_names_s.length] = "Jul";
month_names_s[month_names_s.length] = "Aug";
month_names_s[month_names_s.length] = "Sep";
month_names_s[month_names_s.length] = "Oct";
month_names_s[month_names_s.length] = "Nov";
month_names_s[month_names_s.length] = "Dec";

var day_names_s = new Array ( );
day_names_s[day_names_s.length] = "Sun";
day_names_s[day_names_s.length] = "Mon";
day_names_s[day_names_s.length] = "Tue";
day_names_s[day_names_s.length] = "Wed";
day_names_s[day_names_s.length] = "Thu";
day_names_s[day_names_s.length] = "Fri";
day_names_s[day_names_s.length] = "Sat";


function pd_filter(txt){
var res=txt.replace(/</g,"&lt;");
 res=res.replace(/>/g,"&gt;");
res=res.replace(/\[b\]/g,"<b>");
 res=res.replace(/\[\/b\]/g,"</b>");
 res=res.replace(/\[i\]/g,"<i>");
 res=res.replace(/\[\/i\]/g,"</i>");
 res=res.replace(/\[u\]/g,"<u>");
 res=res.replace(/\[\/u\]/g,"</u>");

 res=res.replace(/\n/g,"<br>");
// res=res.replace(/\r/g,"<br>");
return res;
}


function pd_getMins(date){
var res = date.getMinutes()+"";
if (res.length==1) res="0"+res;
return res;
}


function getHref(){
//return document.location.pathname;

var res =  document.location.href;

var i = res.indexOf("pd_"); 
if (i>-1)
	res = res.substring(0,i-1);
i = res.indexOf("#"); 
if (i>-1)
	res = res.substring(0,i);
	
	i = res.indexOf("jsessionid"); 
if (i>-1)
	res = res.substring(0,i-1);
	
return res;

}




function pd_reglinks_doForgot(hid) {window.open(host+'/comreg/login/forgot.do?action=w_lo4&tiid='+hid+'_reg','forgotw','width=580,height=450,scrollbars=yes,resizable=yes');}
function pd_reglinks_doRegister(hid) {window.open(host+'/comreg.do?action=w_re1&tiid='+hid+'_reg','pdreg','width=600,height=500,scrollbars=yes,resizable=yes');}
function pd_reglinks_doLogout(hid) {document.location=host+"/comreg/login.do?tiid="+hid+"_reg&action=w_lo3&returnPath="+escape(addParamsToUrl(getHref(),"pd_passland=t#pd_rw_a"));return false;}
function pd_reglinks_doInbox(hid) {document.location=host+"/message2.do?action=view&hid="+hid;return false;}
function pd_reglinks_doProfile(hid) {document.location=host+"/comreg/profile.do?tiid="+hid+"_reg&action=w_pr5&returnPath="+escape(getHref());return false;}
function pd_reglinks_doMemDir(hid) {document.location=host+"/member.do?tiid="+hid+"_reg";return false;}
function pd_reglinks_doLogin(hid) {document.location=host+"/comreg/login.do?tiid=1_reg&action=w_lo1&returnPath="+escape(addParamsToUrl(getHref(),"pd_passland=t#pd_rw_a"));return false;}

function pd_reglinks_writeLoginLogout(loginLabel,logoutLabel,hid){
doPassLand(hid);
	if (!doForwardPassLand()){
	
if (!pd_isLoggedOn(hid)){
document.write('<a href="#" onClick="return pd_reglinks_doLogin('+hid+')">'+loginLabel+'</a>');
}else{
document.write('<a href="#" onClick="return pd_reglinks_doLogout('+hid+')">'+logoutLabel+'</a>');
}
doFixIt("write");
}

}





