
/*################### DIV POSITIONING AND MASKS #####################*/

// function to centre a div of a given width and height in the browser space
function deadCenterDiv(Xwidth,Yheight,divid) { 
	// First, determine how much the visitor has scrolled 

	var scrolledX, scrolledY; 
	if( self.pageYOffset ) { 
		scrolledX = self.pageXOffset; 
		scrolledY = self.pageYOffset; 
	} else if( document.documentElement && document.documentElement.scrollTop ) { 
		scrolledX = document.documentElement.scrollLeft; 
		scrolledY = document.documentElement.scrollTop; 
	} else if( document.body ) { 
		scrolledX = document.body.scrollLeft; 
		scrolledY = document.body.scrollTop; 
	} 

	// Next, determine the coordinates of the center of browser's window 

	var centerX, centerY; 
	if( self.innerHeight ) { 
		centerX = self.innerWidth; 
		centerY = self.innerHeight; 
	} else if( document.documentElement && document.documentElement.clientHeight ) { 
		centerX = document.documentElement.clientWidth; 
		centerY = document.documentElement.clientHeight; 
	} else if( document.body ) { 
		centerX = document.body.clientWidth; 
		centerY = document.body.clientHeight; 
	} 

	// Xwidth is the width of the div, Yheight is the height of the 
	// div passed as arguments to the function: 
	var leftOffset = scrolledX + (centerX - Xwidth) / 2; 
	var topOffset = scrolledY + (centerY - Yheight) / 2; 
	// The initial width and height of the div can be set in the 
	// style sheet with display:none; divid is passed as an argument to // the function 
	var o=document.getElementById(divid); 
	var r=o.style; 
	r.position='absolute'; 
	r.top = topOffset + 'px'; 
	r.left = leftOffset + 'px'; 
	r.display = "block"; 
} 

// function to add a mask div of a given opacity and z-index
function showMaskDiv(opacity, zvalue) {
	if (document.getElementById('maskDiv'))
		var myMask = document.getElementById('maskDiv');
	else
		{
		var myMask = document.createElement('div');
		myMask.setAttribute("id","maskDiv");
		document.body.appendChild(myMask);
		}

	// get the page height
	if (window.innerHeight && window.scrollMaxY) {	
		myHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		myHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myHeight = document.body.offsetHeight;
	}

	myMask.style.position = 'absolute';
	myMask.style.top = '0px';
	myMask.style.left = '0px';
	myMask.style.width = '100%';
	myMask.style.height = myHeight+'px';
	myMask.style.visibility = 'visible';
	myMask.style.background = '#000000';
	myMask.style.opacity = opacity;
	myMask.style.filter = 'alpha(opacity='+(opacity*100)+')';
	myMask.style.zIndex = zvalue;
}

// hide the mask div
function hideMaskDiv() {
	document.getElementById('maskDiv').style.visibility = 'hidden';
}

// hide help div
function hideHelpDiv() {
	document.getElementById('helpBox').style.display = 'none';
}

// ajax to get help box
function openHelp(filename, width, height, title) {
	showMaskDiv(0.75, 1);
	var mydiv = $('helpBox');
	var url = "/functions/ajax.php?act=openHelp&filename="+filename+"&myWidth="+width+"&myHeight="+height+"&title="+escape(title);
	new Ajax.Updater(mydiv, url);
	document.getElementById('helpBox').style.display='';
	deadCenterDiv(width, height, 'helpBox');
}

// ajax to show comments
function showComments(itemID, page, loggedIn) {
	var mydiv = $('commentsBox');
	var url = "/functions/ajax.php?act=showComments&itemID="+itemID+"&page="+page+"&loggedIn="+loggedIn;
	new Ajax.Updater(mydiv, url);
}

// ajax to show comment form
function commentForm(itemID, structure, sector, subscriberID) {
	var mydiv = $('commentAjax');
	var url = "/functions/ajax.php?act=commentForm&itemID="+itemID+"&structure="+structure+"&sector="+sector+"&subscriberID="+subscriberID;
	new Ajax.Updater(mydiv, url);
}

// ajax to save a comment
function saveComment() {
	if ((document.commentForm.subject.value != "") && (document.commentForm.message.value != ""))
		{
		document.getElementById('sendComment').src = "/images/forms/send_grey.gif";
		var mydiv = $('commentAjax');
		var itemID = document.commentForm.itemID.value;
		var structure = document.commentForm.structure.value;
		var sector = document.commentForm.sector.value;
		var subscriberID = document.commentForm.subscriberID.value;
		var subject = document.commentForm.subject.value;
		var message = document.commentForm.message.value;
		var url = "/functions/ajax.php?act=saveComment&itemID="+itemID+"&structure="+structure+"&sector="+sector+"&subscriberID="+subscriberID+"&subject="+escape(subject)+"&message="+escape(message);
		new Ajax.Updater(mydiv, url);
		}
	else
		alert("Please complete both the subject and comment fields before submitting");
}

// ajax to show advice sections
function adviceSection(type) {
	if (type == "question")
		helplineVideo();
	var mydiv = $('adviceBox');
	var url = "/functions/ajax.php?act=adviceSection&type="+type;
	new Ajax.Updater(mydiv, url);
}

// ajax to send advice emails
function adviceSend(form) {
	var mydiv = $('adviceBox');
	var subscriberID = document.getElementById('hiddenSubscriberID').value;
	if (document.myForm.message.value != "")
	{
		var myElements = form.elements;
		var dataUrl = "";
		for (var i=0; i < myElements.length; i++ ) {
			if ((myElements[i].type) && (myElements[i].type.length > 0 )) {
				dataUrl += "&"+myElements[i].name+"="+myElements[i].value;
			}
		}
		var url = "/functions/ajax.php?act=adviceSend&subscriberID="+subscriberID+dataUrl;
	}
	else
		var url = "/functions/ajax.php?act=adviceSection&error=1&type="+document.myForm.type.value+"&subscriberID="+subscriberID+dataUrl;
	new Ajax.Updater(mydiv, url);
}

// ajax to show helpline video
function helplineVideo() {
	var mydiv = $('film');
	var url = "/functions/ajax.php?act=helplineVideo";
	new Ajax.Updater(mydiv, url);
}

// ajax 
function interestOption(myField, myValue) {
	document.getElementById(myField).value = myValue;
}

// ajax 
function signOption(myField, myValue) {
	document.getElementById(myField).value = myValue;
	if (myField == "isign")
		signAjax(myValue);
}

// ajax 
function signAjax(isign) {
	document.getElementById('isign').value = isign;
	if (isign == 1)
		document.getElementById('signAjax').style.display = '';
	else
		document.getElementById('signAjax').style.display = 'none';
}

// ajax 
function accountOption(myField, myValue) {
	if (myField == "defaultvideo")
		myField = "defaultVideo";
	document.getElementById(myField).value = myValue;
	if (myField == "mobile")
		mobileAjax(myValue);
}

// ajax 
function mobileAjax(mobile) {
	if (mobile == 1)
		document.getElementById('mobileAjax').style.display = '';
	else
		document.getElementById('mobileAjax').style.display = 'none';
}


