// Various validation scripts


function trim (str) {
    str = str.replace(/^\s+/, '');
    return str.replace(/\s+$/, '');
}

function validateLoginInput()
{
	var valid = true;
	var emailMsg = document.getElementById("emailMessage");
	emailMsg.innerHTML = "";

	var pswdMsg = document.getElementById("passwordMessage");
	pswdMsg.innerHTML = "";
	
	var email = document.getElementById("email");
	var emailVal = email.value;
	var check = emailVal.search(/^.+@.+\..+$/);
	if (check != 0) {
		emailMsg.innerHTML = "Please enter a valid email.";
	    email.focus();
	    valid = false;
	}
	if (check != 0 && emailVal.length > 45) {
	  	emailMsg.innerHTML = "Emails longer than 45 characters are not accepted.";
	    email.focus();
	    valid = false;
	}
	
	var pswd = document.getElementById("password");
	if (pswd.value.length < 5) {
		pswdMsg.innerHTML = "Please enter a valid password.";
		if (valid === true) pswd.focus();
		valid = false;
	}
	
	return valid;	
}


function validateJoinInput()
{
	var valid = true;
	
	var nameMsg = document.getElementById("nameMessage");
	nameMsg.innerHTML = "";
	
	var emailMsg = document.getElementById("emailMessage");
	emailMsg.innerHTML = "";

	var pswdMsg = document.getElementById("passwordMessage");
	pswdMsg.innerHTML = "";
	
	var name = document.getElementById("name");
	if (name.value.length == 0) {
		nameMsg.innerHTML = "Please enter a valid name.";
		valid = false;
		name.focus();
	}

	var email = document.getElementById("email");
	var emailVal = email.value;
	var check = emailVal.search(/^.+@.+\..+$/);
	if (check != 0) {
		emailMsg.innerHTML = "Please enter a valid email.";
	    if (valid === true) email.focus();
	    valid = false;
	}
	if (check != 0 && emailVal.length > 45) {
	  	emailMsg.innerHTML = "Emails longer than 45 characters are not accepted.";
	    if (valid === true) email.focus();
	    valid = false;
	}
	
	var pswd = document.getElementById("password_r");
	var pswd2 = document.getElementById("password_r2");
	if (pswd.value.length < 5) {
		pswdMsg.innerHTML = "Please enter a valid password.";
		if (valid === true) pswd.focus();
		valid = false;
	}
	if (pswd.value.length > 5 && pswd.value != pswd2.value)	{
		pswdMsg.innerHTML = "Passwords not identical.";
		pswd.value = "";
		pswd2.value = "";
		if (valid === true) pswd.focus();
		valid = false;
	}
	
	var iagree = document.getElementById("iagree");
	var iagreemsg = document.getElementById("iagreeMessage");
	if (iagree.checked == false) {
		iagreemsg.innerHTML = "You must agree.";
		valid = false;
	}
	else {
		iagreemsg.innerHTML = "";
	}
	
	return valid;	
}

function validateUploadItem()
{
	var content = "";
	var valid = true;
	var itemfile = document.getElementById("itemfile");
	content = trim(itemfile.value);
	
	if (content.length <= 0) {
		valid = false;
		var imagemsg = document.getElementById("uploaditem_immsg");
		if (imagemsg != null)
			imagemsg.innerHTML = "Invalid file";
	}
	
	var itemprice = document.getElementById("itemprice");
	var pricemsg = document.getElementById("pricemsg");
	
	$numeric = false;
	if (itemprice.value == parseInt(itemprice.value))
		$numeric = true;
	if (itemprice.value != '' && $numeric == false) {
		if (pricemsg != null)
			pricemsg.innerHTML = "Invalid price";
		valid = false;
	}
	else {
		if (pricemsg != null)
			pricemsg.innerHTML = "";
	}
	
	
/*
	var itemtitle = document.getElementById("itemtitle");
	content = trim(itemtitle.value);
	if (content.length <= 0) {
		valid = false;
	}
	
	var itemdescription = document.getElementById("itemdescription");
	content = trim(itemdescription.value);
	if (content.length <= 0) {
		valid = false;
	}
	*/
	return valid;
	
}

function validateItem()
{
	var valid = true;
	
	var itemprice = document.getElementById("itemprice");
	var pricemsg = document.getElementById("pricemsg");
	
	$numeric = false;
	if (itemprice.value == parseInt(itemprice.value))
		$numeric = true;
	if (itemprice.value != '' && $numeric == false) {
		if (pricemsg != null)
			pricemsg.innerHTML = "Invalid price";
		valid = false;
	}
	else {
		if (pricemsg != null)
			pricemsg.innerHTML = "";
	}
	
	return valid;
}

function deleteItemConfirmation()
{
	var res = confirm("Are you sure you want to delete this item?");
	return res;
}

function validatePassword()
{
	var valid = true;
	var pswdMsg = document.getElementById("pswdMessage");
	var pswd = document.getElementById("password");
	var pswd2 = document.getElementById("password_r");
	if (pswd.value.length < 5) {
		pswdMsg.innerHTML = "Please enter a valid password (min 5 characters)";
		pswd.focus();
		pswd.value = "";
		pswd2.value = "";
		valid = false;
	}
	if (pswd.value.length > 5 && pswd.value != pswd2.value)	{
		pswdMsg.innerHTML = "Passwords not identical.";
		pswd.value = "";
		pswd2.value = "";
		pswd.focus();
		valid = false;
	}
	return valid; 
}

function validateMessage() {
	var valid = true;
	var msg = document.getElementById("message");
	var msgvalue = msg.value; 
	
	msgvalue = msgvalue.replace(/^\s*/, "");
	msgvalue = msgvalue.replace(/\s*$/, "");
	
	if (msgvalue == "") {
		msg.focus();
		valid = false;
	}
	return valid; 
}

function validateDonationData() {
	var valid = true;
	
	var amount = document.getElementById("os0");
	var amountmsg = document.getElementById("amountMessage");
	
	$numeric = false;
	if (amount.value == parseInt(amount.value) && parseInt(amount.value) > 0)
		$numeric = true;
	
	if ($numeric == false) {
		amountmsg.innerHTML = "Invalid amount";
		valid = false;
		amount.focus();
	}
	else {
		var amt = document.getElementById("amount");
		amt.value = amount.value; 
	}
	
	return valid;
}