// pop-up window
var fullpath = "http://newserver/dating/heavendating2.co.uk/";

if(window.attachEvent) window.attachEvent("onload", setListeners);

function moveDiv(theDiv){

	heightWidth = getScrollOffset();
	scroll_x = heightWidth[0];
	scroll_y = heightWidth[1];

	heightWidth = getInnerDim();
	x = heightWidth[0];
	y = heightWidth[1];
	
	theDiv.style.left = 600 + "px";
	theDiv.style.top = 0 + (scroll_y - 10) + "px";
	
}

function getScrollOffset(){

	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	
	return Array(dsocleft, dsoctop);
}

function getInnerDim(){

	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	return Array(x, y);
}

function setListeners(){
	
	inputList = document.getElementsByTagName("INPUT");    
	for(i=0;i<inputList.length;i++){
		inputList[i].attachEvent("onpropertychange",restoreStyles);
	    inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");

	for(i=0;i<selectList.length;i++){
		selectList[i].attachEvent("onpropertychange", restoreStyles);
		selectList[i].style.backgroundColor = "";
	}  
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "") event.srcElement.style.backgroundColor = "";
}

function addBookmark(title,url) {
  if (window.sidebar) {
    window.sidebar.addPanel(title, url,"");
  } else if( document.all ) {
    window.external.AddFavorite( url, title);
  } else if( window.opera && window.print ) {
    return true;
  }
}

function checkCountry(){
	
	theSelect = document.getElementById('country');
	selectedCountry = theSelect[theSelect.selectedIndex].value;
	thepostcode = document.getElementById('postcode_holder');
	theInfo = document.getElementById('postcode_details');
	
	if(selectedCountry == 1){
		thepostcode.style.display = 'block';
		
		if(theInfo.innerHTML != "") theInfo.style.display = 'block';
	}	
	else{
		thepostcode.style.display = 'none';
		theInfo.style.display = 'none';
	}
}

function changeSelectedPhoto(filename, id){
	thePhoto = document.getElementById(id);
	theInput = document.getElementById('avatar');
	theHolder = document.getElementById('avail_photos');
	
	theInput.value = filename;
	
	//unfade all the images
	for(x=0; x < theHolder.childNodes.length; x++){	
		thisImage = theHolder.childNodes[x];
		if(thisImage.id != undefined) setOpacity(thisImage.id, 100);
	}
	
	setOpacity(id, 5);

}

function switchTitle(thenum){

	theDiv = document.getElementById('icons_title');
	thetext = new String();
	
	switch(thenum){
		case 1: thetext = "Contact member"; break;
		case 2: thetext = "Add to favourites"; break;
		case 3: thetext = "Comment on member"; break;
		case 4: thetext = "Invite user to your private gallery"; break;
		case 5: thetext = "View users comments"; break;
		case 6: thetext = "Back to users profile"; break;
		case 7: thetext = "Back to members gallery"; break;
		case 8: thetext = "Add private notes"; break;
	}
	
	theDiv.innerHTML = thetext;
}

function resetTitle(){
	theDiv = document.getElementById('icons_title');
	theDiv.innerHTML = "&nbsp;";
}
	
function MsgOkCancel(message, url){
	var fRet;
	fRet = confirm(message);
	if (fRet == true) {
	 window.location = url;
	}
}

function addPollOption(textvalue, optId, optionsName){
	pollOptCount++;
	theHolder = document.getElementById('option_holder');
	
	if(optId != undefined) pollOptId = optId
	else pollOptId = '';
	
	if(optionsName == '' || optionsName == undefined) optionsName = 'poll_options';

	var theDiv = document.createElement('div');
	theDiv.id = 'opt' + pollOptCount.toString();
	theDiv.style.marginBottom = '5px';

	var theIpt = document.createElement('input');
	theIpt.className = 'form';
	theIpt.style.width = "60%";
	theIpt.style.marginRight = "2px";
	theIpt.name = optionsName + "[" + pollOptId + "]";
	theIpt.id = 'poll_opt' + pollOptId;
	theDiv.appendChild(theIpt);
	if(textvalue != undefined) theIpt.value = textvalue;
	
	var theBtn = document.createElement('img');
	theBtn.style.verticalAlign = 'middle';
	theBtn.src = 'images/icons/delete.gif';
	theBtn.alt = 'Remove option';
	theBtn.id = "opt_rem" + pollOptCount.toString();

	theBtn.onclick = function (){	
		theHolder.removeChild(theDiv);
	}
	theDiv.appendChild(theBtn);
	
	var theBr = document.createElement('br');
	theDiv.appendChild(theBr);
	
	theHolder.appendChild(theDiv);	
}

function checkEmail(type, xmldata){
	var theSpan = document.getElementById("emailspan");
	theSpan.innerHTML = "";
	
	if(type == 1){
		var theEmail = document.getElementById("email").value;
		if(email = "") theSpan.innerHTML = "<strong>Please enter a valid email address</strong><br /><br />";
		else getData("ajax/checkemail.php?email=" + theEmail, 10);
	}
	else if(type == 2){
		var rootElement = xmldata.documentElement;
		var validEmail = rootElement.attributes[0].nodeValue;
		if(validEmail == 0) theSpan.innerHTML = "<strong>Please enter a valid email address</strong><br /><br />";
	}
}

function setOpacity(id, value)
{
	var testObj = document.getElementById(id);
	testObj.style.opacity = value/10;
	testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function setAvatar(photoid, filename){
	theInput = document.getElementById("avatarid");
	theImage = document.getElementById("avatarholder");
	
	theInput.value = photoid;
	theImage.src = "member-photos/thumbs/" + filename;
}

function hideUnhide(theDiv){
	var theDiv = document.getElementbyId(theDiv);
	
	if(theDiv.style.display == 'block') theDiv.style.display = 'none';
	else theDiv.style.display = 'block';
}

function changeImage(theImage, image1, image2){
	var theImage = document.getElementById(theImage);
	if(theImage.src == fullpath + "images/" + image1) theImage.src = 'images/' + image2;
	else theImage.src = 'images/' + image1;
}

function NewWindow(mypage,myname,w,h,scroll, status, menubar){
	var win = null;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'width='+w+', height='+h+', top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+', status=' + status + ', menubar=' + menubar; 
	win = window.open(mypage, myname, settings);
	win.focus();
}

function unhidemails(){
	elementVar = document.getElementById('hiddenmailfield');
	elementVar.style.display = 'block';
}

function addToTextBox(textbox, text){
	var eleTextarea = document.getElementById(textbox);

	 eleTextarea.focus();
	 if( eleTextarea.createTextRange ) {
	   document.selection.createRange().text += text;
	 }
	 else if(eleTextarea.setSelectionRange){
	   var len = eleTextarea.selectionEnd;
	   eleTextarea.value = eleTextarea.value.substr( 0, len ) + text + eleTextarea.value.substr( len );
	   eleTextarea.setSelectionRange(len+text.length,len+text.length);
	 }
	 else{ 
		eleTextarea.value += text;
	 }
}

function readKey(e){
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	var theTextarea = document.getElementById("ticket");

	if(parseInt(key) == 32){
		//send text off to be searched for relevant articles
		var theText = theTextarea.value;
		theText = encodeURI(theText);
		makePOSTRequest(fullpath + "ajax/getArticles.php", "text=" + theText, 3);
	}
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}


function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function stripHTML(value){
	var re = /<\S[^><]*>/g
	return value.replace(re, "");
}

function addTowns(xmlData){
	var documentNode = xmlData.documentElement;
	var children = documentNode.childNodes;

	for(x = 0; x < children.length; x++){
		
		townid = children[x].attributes[0].nodeValue;
		town = children[x].attributes[1].nodeValue;
		
		//add the option to the box
		addOption("town", town, townid);
	}
}

function getTowns(selectname, postcodename){	
	var theSelect = document.getElementById(selectname);
	var theInput = document.getElementById(postcodename);
	var thepostcode = theInput.value;

	//test if they can do dynamic select boxes
	var test = testAddOption(theSelect);

	if(!test) window.location = "?loadtowns=" + thepostcode;
	else{
		//clear the select box
		theSelect.options.length = 0;
		getData("ajax/getTowns.php?postcode=" + thepostcode, 3);
	}
}

function testAddOption(selectBox){
	var theSelect = selectBox;
	
	optionTest = true;
	lgth = theSelect.options.length = 1;
	theSelect.options[lgth] = null;
	if (theSelect.options[lgth]) optionTest = false;
	
	return optionTest;
}
	

function addOption(selectbox,text,value ){
	var optn = document.createElement("OPTION");
	var selectbox = document.getElementById(selectbox);
	
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

function hideUnhide(theDiv){
	var theDiv = document.getElementById(theDiv);
	
	if(theDiv.style.display == 'block') theDiv.style.display='none';
	else theDiv.style.display='block';
	
	return false;
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}