function getXhttp ()
{
    var ajax_request;
   
    if (  window.ActiveXObject  )  {
        var mSoftVersions = [
            'MSXML2.DOMDocument.5.0',
            'MSXML2.DOMDocument.4.0',
            'MSXML2.DOMDocument.3.0',
            'MSXML2.DOMDocument.2.0',
            'MSXML2.DOMDocument',
            'Microsoft.XmlDom',
            'Msxml2.XMLHTTP',
            'Microsoft.XMLHTTP'
        ];
       
        for (  i=0; i<mSoftVersions.length; i++  )  {
            try {
                ajax_request = new ActiveXObject (  mSoftVersions[i]  );
            }  catch (  e  )  {    }
        }
    }  else if (  !ajax_request && typeof XMLHttpRequest != 'undefined'  )  {
        try {
            ajax_request = new XMLHttpRequest;
        }  catch (  e  )  {    }
    }  else if (  !ajax_request && window.createRequest  )  {
        try {
            ajax_request = window.createRequest;
        }  catch (  e  )  {    }
    }  else  {
        ajax_request = false;
    }
   
    return ajax_request;
}
  
function checkRegisterPassword()
{
	var form = document.logon;
	var returnValue = false;
	if (form.txtOldPassword.value == "")
	{
		alert("Please enter your old password");
		form.txtOldPassword.focus();
	}
	else if (form.txtNewPassword.value == "")
	{
		alert("Please enter your new password");
		form.txtNewPassword.focus();
	}
	else if (form.txtConfirmPassword.value == "")
	{
		alert("Please confirm your new password");
		form.txtConfirmPassword.focus();
	}
	else if (form.txtNewPassword.value != form.txtConfirmPassword.value)
	{
		alert("The passwords are not the same");
		form.txtNewPassword.focus();
		form.txtNewPassword.select();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function checkFormRegister() 
{
	var returnValue = false;
	if(document.getElementById('txtUserName').value == "")
	{
		alert("Please enter a username");
		document.getElementById('txtUserName').focus();
	}
	else if (document.getElementById('txtEmail1').value == "")
	{
		alert("Please enter your email address");
		document.getElementById('txtEmail1').focus();
	}
	else if (document.getElementById('txtEmail2').value == "")
	{
		alert("Please confirm your email address");
		document.getElementById('txtEmail2').focus();
	}
	else if (document.getElementById('txtEmail1').value != document.getElementById('txtEmail2').value)
	{
		alert("Your emails are not the same");
		document.getElementById('txtEmail1').focus();
	}
	else if (document.getElementById('txtPostcode').value == "")
	{
		alert("Please enter your Postcode");
		document.getElementById('txtPostcode').focus();
	}
	else if (document.getElementById('txtFirstName').value == "")
	{
		alert("Please enter your first name");
		document.getElementById('txtFirstName').focus();
	}
	else if (document.getElementById('txtLastName').value == "")
	{
		alert("Please enter your last name");
		document.getElementById('txtLastName').focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function checkFormLogon()
{
	var form = document.logon;
	var returnValue = false;
	if (form.txtEmail.value == "")
	{
		alert("Please enter your email address");
		form.txtEmail.focus();
	}
	else if (form.txtPassword.value == "")
	{
		alert("Please enter your password");
		form.txtPassword.focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function checkFormPassword()
{
	var form = document.forgot;
	var returnValue = false;
	if (form.txtEmail.value == "")
	{
		alert("Please enter your email address");
		form.txtEmail.focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function checkFormContact()
{
	var form = document.message;
	var returnValue = false;
	if (form.txtEmail.value == "")
	{
	alert("Please enter an email address")
	form.txtEmail.focus();
	}
	else if (form.txtMessage.value == "")
	{
	alert("Please enter a message")
	form.txtMessage.focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function goBack()
{
history.go(-1);
}

function goCancelAddQuestionnaire() {
	window.location.href="questionnaire.php";
	}

function checkAddQuestionnaire() {
	var form = document.questionnaire;
	var returnValue = false;
	if (form.txtTitle.value == "")
	{
	alert("Please enter a title for the questionnaire")
	form.txtTitle.focus();
	}
	else if (form.txtDescription.value == "")
	{
	alert("Please enter a description for the questionnaire")
	form.txtDescription.focus();
	}
	else if (form.txtNoSections.value == "")
	{
	alert("Please enter the number of sections")
	form.txtNoSections.focus();
	}
	else if (form.txtCloseDate.value == "")
	{
	alert("Please enter the closing date for questionnaire")
	form.txtCloseDate.focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function swapImage(obj)
{
	document.map.src=obj;
}

function changeColor(pl, col)	
{
	document.getElementById(pl).style.color = col;
	document.getElementById(pl).style.color = col;
}

function addCommentInput(divname, username, pressid, commentlevel, pagename)
{
	var commentlevel = commentlevel+1;
	var submitcomment = '<form name="addcomment" method="POST" action="admin/sendCommentDetails.php" onsubmit="return checkSendComment(' + divname + ')"><div id="sendcomment"><textarea name="txtnewComment" rows="5" class="commenttextarea" id="txtnewComment' + divname + '"></textarea><input type="hidden" name="divname" id="divname" value="' + divname + '"><input type="hidden" name="username" id="username" value="' + username + '"><input type="hidden" name="pressid" id="pressid" value="' + pressid + '"><input type="hidden" name="commentlevel" id="commentlevel" value="' + commentlevel + '"><input type="hidden" name="pagename" id="pagename" value="' + pagename + '"><br /><br /><input type="submit" value="Post" name="submitpost"></form>'
	var newdiv = document.createElement('div');
    newdiv.innerHTML = submitcomment;
    document.getElementById(divname).appendChild(newdiv);
	document.getElementById('reply' + divname).style.display='none';
	document.getElementById('txtnewComment' + divname).focus();
}

function checkSendComment(commentid)
{
	var commentfieldid = document.getElementById('txtnewComment' + commentid).value;
	var returnValue = false;
	if (commentfieldid == "")
	{
		alert("Please enter a comment");
		document.getElementById('txtnewComment' + commentid).focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function checkSendCommentTopLevel()
{
	var commentfieldid = document.getElementById('txtnewComment').value;
	var returnValue = false;
	if (commentfieldid == "")
	{
		alert("Please enter a comment");
		document.getElementById('txtnewComment').focus();
	}
	else
	{
		returnValue = true;
	}
	return returnValue;
}

function hideLayer(layerName)
{
   document.getElementById(layerName).style.display='none';
   document.getElementById('scrolldiv').style.height="750px";
}

function commentrecommend(commentid, commentreco)
{	
	var xml = getXhttp( );
    if ( !xml )
	{ 
	   return false; //Usually you alert something but I don't :d
	}
	
    xml.open('POST', 'admin/commentRecommend.php');
    xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
	
    //CHECKS DONE HERE.	

    xml.send('ID=' + commentid);
 
    //ANYTHING EXTRA IS ADDED HERE.
	
	xml.onreadystatechange = function()
	{

		if(xml.readyState == 4 && xml.status == 200)
		{	
			if(xml.responseText != "Could not add recommend")
			{
				if(commentreco == 0)
				{
					document.getElementById('commentrecommend' + commentid).innerHTML = "<p><span id='showrec" + commentid + "'>" + xml.responseText + " person recommends this posting</p>";
					document.getElementById('recommend' + commentid).innerHTML = "Recommended<br />";
				}
				else
				{
					document.getElementById('showrec' + commentid).innerHTML = xml.responseText + " people recommend";
					document.getElementById('recommend' + commentid).innerHTML = "Recommended<br />";
				}				
			}
			else
			{
				document.getElementById('recommend' + commentid).innerHTML = xml.responseText;
			}
		}
	}
}

function enter_pressed(e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return false;
	return (keycode == 13);
}

// Clear input on keyup
function cleartext(field)
{
	var fieldval = document.getElementById(field).value;
	if(fieldval == "All products" || fieldval == "All business names" || fieldval == "Postcode" || fieldval == "By Product" || fieldval == "By Name")
	{
		document.getElementById(field).value = "";
		document.getElementById(field).style.color = "#000";
	}
}

// Global function to send any form
function  sendform(formname, target)
{
	var urlcontent;
	var presign;
	for(i=0; i<document.formname.elements.length; i++)
	{
		 if(i == 0)
		 {
			presign = "?";
		 }
		 else
		 {
			presign = "&";
		 }
		 urlcontent = urlcontent + presign + document.formname.elements[i].name + "=" + document.formname.elements[i].value;
	}
	window.location.href= + target + urlcontent;
}


function checkPayPalAmount()
{
	var checkpaypal = true;
	
	if(document.getElementById('amount').value == "" || isNaN(document.getElementById('amount').value))
	{
		document.getElementById('amount').style.color = "#ff0000";
		document.getElementById('amount').value = "Enter amount";
		checkpaypal = false;
	}
	
	return checkpaypal;
	
}




