var xmlHttp = createXmlHttpRequestObject();
var serverAddress = "validate.php";
var showErrors = true;
var cache = new Array();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	try 
	{	
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
	}
	
}

function limitText(limitField, limitCount, limitNum) {
		if (limitField.value.length > limitNum) {
			limitField.value = limitField.value.substring(0, limitNum);
		} else {
			limitCount.value = limitNum - limitField.value.length;
		}
	}
	
function isEmpty(elem)
{
		var str = elem;
		if(str.length == 0)
			{
				return true;
			} 
			else 
			{
			return false;
			}
}

function checkSubmission()
{
	var url		= document.getElementById("cp_url").value;
	var email		= document.getElementById("cp_email").value;
	var title	= document.getElementById("cp_name").value;
	var description = document.getElementById("cp_message").value;
	var isvalid = document.getElementById("isvalid");
	
	if(isEmpty(url)) { isvalid.innerHTML = "Please enter a valid url"; return false;}
	var urlfilter  = /^(http:|https:)\/\/(www\.)?([a-zA-Z0-9_\-])+\.([a-zA-Z0-9_\-\.\/])+$/;
	if(!urlfilter.test(url))
	{
		isvalid.innerHTML = "A valid example url is http://www.url.com";
		return false;
	}
	
	if(isEmpty(email)) { isvalid.innerHTML = "Please enter a valid email"; return false;}
	var emailfilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
	if(!emailfilter.test(email))
	{
		isvalid.innerHTML = "This is not a valid email address";
		return false;
	}
	
	if(isEmpty(title)) { isvalid.innerHTML = "Please enter a valid title";  return false;}
	if(isEmpty(description)) { isvalid.innerHTML = "Please enter a valid description";  return false;}
	
	return true;
}


function checkEmailFields()
{
	var name	= document.getElementById("cp_name").value;
	var email   = document.getElementById("cp_email").value;
	var subject	= document.getElementById("cp_subject").value;
	var message = document.getElementById("cp_message").value;
	var antispam = document.getElementById("cp_ans").value;
	var isvalid = document.getElementById("isvalid");
	
	if(isEmpty(name)) { isvalid.innerHTML = "Please enter your name";  return false;}
	if(isEmpty(email)) { isvalid.innerHTML = "Please enter a valid email"; return false;}
	var emailfilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
	if(!emailfilter.test(email))
	{
		isvalid.innerHTML = "This is not a valid email address";
		return false;
	}
	if(isEmpty(subject)) { isvalid.innerHTML = "Please enter a subject";  return false;}
	if(antispam!="12") { isvalid.innerHTML = "Please answer the anti spam question";  return false;}
	if(isEmpty(message)) { isvalid.innerHTML = "Please enter your message";  return false;}
	return true;
}
