var xmlHttp

function CheckVerificationNumbers(url) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
 		alert ("Browser does not support HTTP Request")
 		return
 	}
	var Verification = document.getElementById("Verification").value;
	if (Verification.length == 0) {
 		document.getElementById("VerificationMessage").innerHTML="<span class='notification'>Please Enter the Verification Numbers</span>"; 
		return false;
	}
	url=url+"?numbers="+Verification;
	xmlHttp.onreadystatechange=stateChangedVerification; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function CheckUsername(url) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
 		alert ("Browser does not support HTTP Request")
 		return
 	}
	var Username = document.getElementById("requiredUsername").value;
	url=url+"?name="+Username;
	xmlHttp.onreadystatechange=stateChangedUsername; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function CheckEmailTaken(url) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
 		alert ("Browser does not support HTTP Request")
 		return
 	}
	var Email = document.getElementById("requiredEmail").value;
	url=url+"?email="+Email;
	xmlHttp.onreadystatechange=stateChangedEmail; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function stateChangedEmail() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
 		document.getElementById("CheckEmail").innerHTML=xmlHttp.responseText; 
	}
}
function stateChangedUsername() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
 		document.getElementById("CheckUsername").innerHTML=xmlHttp.responseText; 
	}
}

function stateChangedVerification() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
 		document.getElementById("VerificationMessage").innerHTML=xmlHttp.responseText; 
	}
}


function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e) {
 		//Internet Explorer
 		try {
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e) {
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

function CheckExistingUsersDescription() {
	if (document.myform.Description.value.length > 1000) {
		document.getElementById('SubmitError').innerHTML = 'Your description is too long.';
		return false;
	}
	return true;
}

function CheckUniqueFields() {
	result = CheckExistingUsersDescription();
	if (!result) {
		return false;
	}
	if (document.getElementById('Verification').value.length == 0) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	if (document.getElementById('requiredEmail').value.length == 0) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter an Email Address.';
		return false;
	}
	if (document.getElementById('requiredUsername').value.length == 0) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter a Username.';
		return false;
	}
	Verification = document.getElementById('VerificationOK').value;
	if (Verification == '1') {
	} else {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	Email = document.getElementById('EmailOK').value;
	if (Email == '1') {
		Username = document.getElementById('UsernameOK').value;
		if (Username == '1') {
			return true;
		} else {
			document.getElementById('SubmitError').innerHTML = 'The Username that you entered is already used.';
			return false;
		}
	} else {
		document.getElementById('SubmitError').innerHTML = 'The Email address that you entered is already used.';
		return false;
	}
}

function UpdateDescriptionCount(value) {
	if (value==null) {
		maxvalue=1000;
	} else {
		maxvalue=eval(value);
	}
	len = getLengthMCE('Description');
	remaining = maxvalue - len;
	if (remaining >= 0) {
		document.getElementById('DescriptionCount').innerHTML = remaining+' characters remaining';
	} else {
		document.getElementById('DescriptionCount').innerHTML = '<span class="notification">' + len + ' Characters! <br/>Too many characters.</span>';
	}
}

function CheckRFPRFQScreen(ID,which) {
	if (CheckRequiredFields(which)) {
		if (ID == '') {
			if (document.getElementById('Verification').value.length < 4) {
				document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
				return false;
			}
		}
		DescLength = getLengthMCE('Description');
		if (DescLength > 2000) {
			document.getElementById('SubmitError').innerHTML = 'Your description is '+ DescLength + ' characters long. It can only be 2000 characters long.';
			return false;
		}
		Verification = document.myform.VerificationOK.value;
		if (Verification < 1) {
			document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
			return false;
		}
		return true;
	}
	return false;
}

function getLengthMCE(editorId) {

    // Get the editor instance that we want to interact with.
    var oEditor = tinyMCE.get(editorId) ;

    // Get the Editor Area DOM (Document object).
    var oDOM = oEditor.getDoc() ;

    var iLength ;
    // The are two diffent ways to get the text (without HTML markups).
    // It is browser specific.
    if (document.all) {
        // If Internet Explorer.
        iLength = oDOM.body.innerText.length;
    } else {
        // If Gecko.
        var r = oDOM.createRange() ;
        r.selectNodeContents(oDOM.body);
        iLength = r.toString().length;
    }

    return iLength;
}

function CheckUser1(which) {
	//alert("1");
	//return false;
	//They are a user, and they are a provider
	DescLength = getLengthMCE('Description');
	if (DescLength > 1000) {
		document.getElementById('SubmitError').innerHTML = 'Your description is '+ DescLength + ' characters long. It can only be 1000 characters long.';
		return false;
	}

	if (CheckRequiredFields(which)) {
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				if (CheckFieldsMatch('requiredPassword','requiredConfirm_Password')) {
					return true;
				}
			}
		}
	}
	return false;
}

function CheckUser2(which) {
	//They are a user, and they are not provider
	if (CheckRequiredFields(which)) {
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				if (CheckFieldsMatch('requiredPassword','requiredConfirm_Password')) {
					return true;
				}
			}
		}
	}
	return false;
}


function CheckUser3(which) {
	//They are a not user, and they are a provider
	if (CheckRequiredFields(which)) {
	Username = document.getElementById('UsernameOK').value;
	if (Username != 1) {
		document.getElementById('SubmitError').innerHTML = 'Please pick a new Username, this one is already taken.';
		return false;
	}
	Email = document.getElementById('EmailOK').value;
	if (Email != 1) {
		document.getElementById('SubmitError').innerHTML = 'The Email address that you entered is already used.';
		return false;
	}
	DescLength = getLengthMCE('Description');
	if (DescLength > 1000) {
		document.getElementById('SubmitError').innerHTML = 'Your description is '+ DescLength + ' characters long. It can only be 1000 characters long.';
		return false;
	}
	//Check that they said OK to the Privacy
	Privacy = document.myform.Privacy[1];
	if (Privacy.checked) {
		document.getElementById('SubmitError').innerHTML = 'You must agree with the Privacy Statement to continue.';
		return false;
	}
	Terms = document.myform.Terms[1];
	if (Terms.checked) {
		document.getElementById('SubmitError').innerHTML = 'You must agree with the Terms of Use to continue.';
		return false;
	}
	if (document.getElementById('Verification').value.length < 4) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Validation Numbers.';
		return false;
	}
	Verification = document.myform.VerificationOK.value;
	if (Verification < 1) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				if (CheckFieldsMatch('requiredPassword','requiredConfirm_Password')) {
					return true;
				}
			}
		}
	}
	return false;
}


function CheckUser4(which) {
	//They are a not user, and they are not provider
	//Check that they said OK to the Privacy
	if (CheckRequiredFields(which)) {
	Username = document.getElementById('UsernameOK').value;
	if (Username != 1) {
		document.getElementById('SubmitError').innerHTML = 'Please pick a new Username, this one is already taken.';
		return false;
	}
	Email = document.getElementById('EmailOK').value;
	if (Email != 1) {
		document.getElementById('SubmitError').innerHTML = 'The Email address that you entered is already used.';
		return false;
	}
	Privacy = document.myform.Privacy[1];
	if (Privacy.checked) {
		document.getElementById('SubmitError').innerHTML = 'You must agree with the Privacy Statement to continue.';
		return false;
	}
	Terms = document.myform.Terms[1];
	if (Terms.checked) {
		document.getElementById('SubmitError').innerHTML = 'You must agree with the Terms of Use to continue.';
		return false;
	}
	if (document.getElementById('Verification').value.length < 4) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	Verification = document.myform.VerificationOK.value;
	if (Verification < 1) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				if (CheckFieldsMatch('requiredPassword','requiredConfirm_Password')) {
					return true;
				}
			}
		}
	}
	return false;
}

function CheckEmail(which) {
	//They are sending an email at the Contact Us Screen
	if (document.getElementById('Verification').value.length < 4) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	Verification = document.myform.VerificationOK.value;
	if (Verification < 1) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	if (CheckRequiredFields(which)) {
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				return true;
			}
		}
	}
	return false;
}

function CheckJob(ID, which) {
	if (ID == '') {
		if (document.getElementById('Verification').value.length < 4) {
			document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
			return false;
		}
	}
	Verification = document.myform.VerificationOK.value;
	if (Verification < 1) {
		document.getElementById('SubmitError').innerHTML = 'Please Enter the Correct Validation Numbers.';
		return false;
	}
	if (document.getElementById('RequiredExperience').value.length > 500) {
		thislength = document.getElementById('RequiredExperience').value.length;
		document.getElementById('SubmitError').innerHTML = 'Your Experience Required Description is '+ thislength + ' characters long. It can only be 500 characters long.';
		return false;
	}
	DescLength = getLengthMCE('Description');
	if (DescLength > 2000) {
		document.getElementById('SubmitError').innerHTML = 'Your description is '+ DescLength + ' characters long. It can only be 2000 characters long.';
		return false;
	}
	if (DescLength < 1) {
		document.getElementById('SubmitError').innerHTML = 'You need to enter a Description';
		return false;
	}
	if (CheckRequiredFields(which)) {
		if (CheckEmailField('requiredEmail')) {
			if (CheckFieldsMatch('requiredEmail','requiredConfirm_Email')) {
				return true;
			}
		}
	}
	return false;
}

