var printerFriendly;
var netsuiteReg = false;
var netsuiteParams = new Array();
var unsubscribeParamFound = false;
var qs = new Querystring()
var qsPrint = qs.get("print");
var bStartFP = false;

setPrintCss();
setNetsuiteParams();

window.onload = windowonload;

/* OLD FUNCTIONS */
/*function windowonload() {
	appendPrinterFriendly();

	if (netsuiteReg == true){
		document.getElementById('content').innerHTML += 
			'<DIV id="popup">'
			+ '<DIV class="message-pane-bottom">'
			+ '<P>Thank you for your interest in Infoflow.</P>'
			+ '<P>We will contact you shortly to discuss your requirements in more detail.</P>'
			+ '<BR/>'
			+ '<P>Yours sincerely,</P>'
			+ '<P>Infoflow Business Team</P>'
			+ '<DIV class="cClose2"><BUTTON onClick="closePopup();"></BUTTON></DIV>'
			+ '</DIV>'
			+ '</DIV>';
	}
}

function closePopup(){
	window.location = 'http://www.infoflow.co.uk';
}*/

function windowonload() {
	appendPrinterFriendly();

	if (netsuiteReg == true){
		document.getElementById('pane-top-content').innerHTML += 
			'<DIV id="popup">'
			+ '<P>Thank you for your interest in Infoflow.</P>'
			+ '<P>We will contact you shortly to discuss your requirements in more detail.</P>'
			+ '<BR/>'
			+ '<P>Yours sincerely,</P>'
			+ '<P>Infoflow Business Team</P>'
			+ '<DIV class="cClose"><BUTTON onClick="closePopup();"></BUTTON></DIV>'
			+ '</DIV>';
	} else {
		//startFP();
	}
}

function closePopup(){
	document.getElementById('pane-top-content').removeChild(document.getElementById('popup'));
	startFP();
}

function setPrintCss(){
	if (qsPrint == 'true'){
		if( document.styleSheets ) {
			var css;
			for (var i=0; i<document.styleSheets.length; i++ ){
				if (document.all)
					css = document.styleSheets[i].media;
				else
					css = document.styleSheets[i].media.mediaText;

				if (css.search(/screen/i)!=-1)
					document.styleSheets[i].disabled = true;
				else if (css.search(/print/i)!=-1 && (document.styleSheets.length-1) != i)
					appendCssMedium(i, 'all');
			}

			printerFriendly = true;
		}
	}
}

function appendPrinterFriendly(){
	if (printerFriendly) {
		var imgStyle = 'position:absolute;right:10px;top:10px;border:none;';

		document.body.innerHTML = 
			'<A href="javascript:window.print();">'
			+ '<IMG src="images/icons/print.jpg" alt="Click here to print" title="Click here to print" style="' + imgStyle + '" class="cNoPrint-2"/>' 
			+ '</A>'
			+ document.body.innerHTML
			+ '<DIV class="cClose"><BUTTON onClick="self.close();" class="cNoPrint-2"></BUTTON></DIV>';
	} else {
		if(document.getElementById('printer-friendly')){
			var sPage = document.location;

			var content = 
				'<A href="javascript:openPopupWindow(\'' + sPage + '?print=true\')" title="Click here to open printer friendly page">'
				+ '<IMG src="images/icons/print-friendly.jpg" alt="Click here to open printer friendly page" title="Click here to open printer friendly page"/>'
				+ '</A>';

			document.getElementById('printer-friendly').innerHTML = content;
		}
	}
}

function openPopupWindow(url,target,width,height)
{
	var width = 600;
	var height = 500;
	var target = '_blank';

	var winl = (screen.width-width)/2;
	var wint = (screen.height-height)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	
	window.open(url, target, "dependent=yes,menubar=no,location=no,resizable=yes,status=no,scrollbars=yes,height="+height+",width="+width+",left="+winl+",top="+wint+"");
}


function appendCssMedium(cssId, oType) {
	if( !document.styleSheets ) { 
		showMessage(); //showNoSSWarning(); 
		return; 
	}
	if( !document.styleSheets.length ) { 
		showMessage(); //showNoSSFoundWarning(); 
		return; 
	}

	try { 
		if (document.all)
			document.styleSheets[cssId].media = oType;
		else
			document.styleSheets[cssId].media.appendMedium(oType);
	} catch(e) { 
		showMessage(); //showNoDOMSSWarning(); 
	}
}

function showMessage(){ alert('Please use Print Preview in your browser to view how this page will be printed.'); }
function showNoDOMSSWarning() { alert('Your browser does not support DOM 2 Style Sheets'); }
function showNoSSWarning() { alert('Your browser does not support document.styleSheets'); }
function showNoSSFoundWarning() { alert('Your browser does not have any stylesheets in the document.styleSheets collection'); }
function showNoDemoWarning() { alert('Your browser does not allow me to find the demonstration stylesheet'); }
function showNoBothSSWarning() { alert('Your browser does not correctly support DOM 2 Style Sheets or the Internet Explorer stylesheets model'); }

function setNetsuiteParams(){
	for (var i=0; i<netsuiteParams.length; i++)
		setNetsuiteParam(netsuiteParams[i]);
	if (unsubscribeParamFound == false)
		document.cookie = 'unsubscribe=F';
}

function setNetsuiteParam(param){
	if (qs.params[param]) {
		if (param == 'unsubscribe') 
			unsubscribeParamFound = true;
		document.cookie = param + '=' + qs.params[param];
	}
}

function getNetsuiteParams(element){
	var newQs = '';
	if (document.cookie != '') {
		var cookies = document.cookie.split(';');
		var tempCookie;

		for (var i=0; i<cookies.length; i++) {
			tempCookie = cookies[i].split('=');
			if (tempCookie[1] != null && tempCookie[1] != 'print')
				newQs += tempCookie[0].replace(/^\s*|\s*$/g,"") + '=' + tempCookie[1].replace(/^\s*|\s*$/g,"") + '&';
		}

		if (newQs != '')
			document.getElementById(element).href += '&' + newQs.substring(0,newQs.length - 1);
	}
}

// QUERYSTRING PARAMETERS
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object();
	this.get = Querystring_get;
	
	if (qs == null)
		qs = location.search.substring(1,location.search.length);

	if (qs.length == 0) 
		return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);

		if (pair.length == 2)
			value = unescape(pair[1]);
		else
			value = name;
		
		this.params[name] = value;
		if (name != '' && name != 'print' && name != 'netsuitereg')
			netsuiteParams[netsuiteParams.length] = name;
		
		if (name == 'netsuitereg')
			if (value == 'success')
				netsuiteReg = true;
	}
}

function Querystring_get(key, default_) {
	if (default_ == null) default_ = null;
	
	var value=this.params[key];
	if (value==null) value=default_;
	
	return value;
}

function validate(formid) {
	var x;
	var required = document.getElementsByName("requiredfield");
	for (x=0;x<required.length;x++) {
		var name = required[x].id.split("_");
		var title = document.getElementsByName(name[1])[0].title;
		if(	document.getElementsByName(name[1])[0].form.id == formid && 
			document.getElementsByName(name[1])[0].value == "") {
			alert(title + " is a required field.\nPlease make sure to enter " + title + ".");
			document.getElementsByName(name[1])[0].focus();
			return false;
		}
	}

	if(document.getElementById('registration')){
		document.getElementById('registration').action = 'register.php?oid='+document.getElementById('oidParam').value;
	} else if(document.getElementById('slingboxentry')){
		document.getElementById('slingboxentry').action = 'win-slingbox-prize-draw.php';
	} else if(document.getElementById('webinar')){
		document.getElementById('webinar').action = 'contact-webinar.php';
	} else if(document.getElementById('registration-white-paper')){
		document.getElementById('registration-white-paper').action = 'register-white-paper.php';
	}
	return true;
}