
// CHECK WE ARE FROM AN ALLOWED SITE

homeland = location.href; //document.referrer;
proceed = homeland.indexOf("www.pwc.co.uk");
if(proceed == -1){ proceed = homeland.indexOf("flatearth.co.uk");}
if(proceed == -1){ proceed = homeland.indexOf("pwclegal.co.uk");}
if(proceed == -1){exit();}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

var cookieid = (Get_Cookie( "pwccommentsid" ) );

getCommentPanel = new function() {

	var BASE_URL = "http://clients.flatearth.co.uk/pwcpolls/";

	function isIE(){
		return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	}
	
	if (isIE()){
        var STYLESHEET = BASE_URL + "comments_panel_ie.css"
    }else{
        var STYLESHEET = BASE_URL + "comments_panel.css"
	}
	
	var SWF_URL = BASE_URL ; // "http://www.pwc.co.uk/ts_tests/";
	var CONTENT_URL = BASE_URL + "comments-display-response.php?id=" + cookieid ;
	var ROOT = "commentsbox";
	var qtxt = "";
	var txt = "";	

	function requestStylesheet(stylesheet_url) {
		stylesheet = document.createElement("link");
		stylesheet.rel = "stylesheet";
		stylesheet.type = "text/css";
		stylesheet.href = stylesheet_url;
		stylesheet.media = "all";
		document.lastChild.firstChild.appendChild(stylesheet);
	}
	
	function requestContent() {
		var script = document.createElement("script");
		script.src = CONTENT_URL;
		// IE7 doesnt like this: document.body.appendChild(script);
		// Instead use:
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	
	this.serverResponse = function( data ) {
		if (!data) return;
		var qdiv = document.getElementById("commentsreplyhead");
		var cmmt_div = document.getElementById(ROOT);
		qtxt = data[0];
		txt = data[1];
		cmmt_div.innerHTML = "<p><strong>Here are some of the comments so far:</strong></p>" + txt ;  // assign new HTML into #ROOT
		qdiv.innerHTML= "<p><strong>" + qtxt + "</strong></p>" ; 
		cmmt_div.style.display = "block"; // make element visible
		qdiv.style.display = "block"; // make element visible
	}
	
	// FUNCTIONS LOADED, START THE CODE
	
	// ADD STYLE SHEET
	requestStylesheet(STYLESHEET);

	dynamicstr = '' ;

	// DIV FOR LEADING PARA AND FLASH REPLY
	dynamicstr = dynamicstr + '<div id="commentsreply" class="cmmts_reply_div_gry">' ;
	// PREPARE OUT JS REPLY FUNCTION FOR SWF TO TRIGGER 
	dynamicstr = dynamicstr + '<script type="text/javascript">function reply(){document.getElementById("commentsflashreply").innerHTML="<p>Your comment has been recieved.</p>";}</script>';
	// DIVS FOR COMMENT BOX
	dynamicstr = dynamicstr + '<div id="commentsreplyhead"></div>' ;
	dynamicstr = dynamicstr + '<div id="commentsflashreply"><p>You need Flash installed</p></div>' ;
	dynamicstr = dynamicstr + '</div>' ;
	// DIV FOR COMMENTS
	dynamicstr = dynamicstr + "<div id='" + ROOT + "' class='cmmts_reply_div_gry' style='display: block'></div>" ;
	// WRITE OUT DIVS
	document.write(dynamicstr);

	// REPLACE FLASH DIV WITH SWF
	var so = new SWFObject(BASE_URL + "comments_name_100219a.swf", "replybox", "354", "80", "8", "#ffffff");
	so.addVariable("id",cookieid);
	so.write("commentsflashreply");

	// GET JSON CONTENT AND REWRITE DIVS
	requestContent();

}

