//----check login informatin...
function loginValidation() {
	var errorstr = "";
	errorstr += checkitem(document.formlogin.userid,"User ID");
	errorstr += checkitem(document.formlogin.password,"Password");
	if (errorstr != "") { alert(errorstr); }
	else {document.formlogin.action="login.php";}
}

//---check check non-members feedback form...
function feedbackValidation(status) {
	var errorstr = "";
	errorstr += checkitem(document.feedbackform.email,"Your email");
	errorstr += checkemail(document.feedbackform.email);
	errorstr += checkitem(document.feedbackform.subject,"Subject");
	errorstr += checkitem(document.feedbackform.comment,"Comment/Feedback");
	if (errorstr == "") {
		if (status="nonmembers") {
			document.feedbackform.action="contact_email.php";
		} else {
			document.feedbackform.action="member_contact_email.php";
		}
		document.feedbackform.submit();
	} else { alert(errorstr); }
}

//---check inquiry form entry...
function inquiryValidation() {
	var errorstr = "";
	errorstr += checkitem(document.inquiryform.name,"Your name");
	errorstr += checkitem(document.inquiryform.email,"Your email");
	errorstr += checkemail(document.inquiryform.email);
	errorstr += checkitem(document.inquiryform.message,"Message:");
	if (errorstr == "") {
		document.inquiryform.action="property_inquiry_submit.php";
		document.inquiryform.submit();
	} else { alert(errorstr); }
}

//---check garage sale form entries...
function garageSaleValidation() {
	var errorstr = "";
	errorstr += checkitem(document.garagesaleform.name,"Your name");
	errorstr += checkitem(document.garagesaleform.email,"Your email");
	errorstr += checkemail(document.garagesaleform.email);
	errorstr += checkitem(document.garagesaleform.message,"Message:");
	if (errorstr == "") {
		document.garagesaleform.action="garage_sale_submit.php";
		document.garagesaleform.submit();
	} else { alert(errorstr); }
}

//---check inquiry form entry...
function discountInquiryValidation() {
	var errorstr = "";
	errorstr += checkitem(document.discountform.name,"Your name");
	errorstr += checkitem(document.discountform.email,"Your email");
	errorstr += checkemail(document.discountform.email);
	errorstr += checkitem(document.discountform.message,"Message:");
	if (errorstr == "") {
		document.discountform.action="discounts_submit.php";
		document.discountform.submit();
	} else { alert(errorstr); }
}

//---check sign-up form before submission...
function signupValidation() {
	var errorstr = "";
	errorstr += checkitem(document.signupform.firstname,"First Name");
	errorstr += checkitem(document.signupform.lastname,"Last Name");
	errorstr += checkitem(document.signupform.nickname,"Nick Name");
	errorstr += checkitem(document.signupform.gender,"Gender");
	errorstr += checkitem(document.signupform.email,"Email");
	errorstr += checkemail(document.signupform.email);
	errorstr += checkitem(document.signupform.username,"Username");
	if (document.signupform.username.value.length < 7) {
		errorstr += "'Username' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.signupform.password,"Password");
	if (document.signupform.password.value.length < 7) {
		errorstr += "'Password' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.signupform.password_confirm,"Password Confirmation Field");
	if (document.signupform.password_confirm.value.length < 7) {
		errorstr += "'Password confirmation field' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.signupform.profession,"Profession");
	errorstr += checkitem(document.signupform.country,"Country");
	errorstr += checkitem(document.signupform.state,"State");
	errorstr += checkitem(document.signupform.city,"City");
	errorstr += checkitem(document.signupform.address,"Address");
	//---perform another check if password and confirmed password is the same.
	if (document.signupform.password.value != document.signupform.password_confirm.value) {
		errorstr += "Password and confirmed password must be the same.\n";
	}
	//---also, checked if TERMS has been understood...
	if (!document.signupform.terms_understood.checked) {
		errorstr += "Please check if you've understood the TERMS and CONDITIONS contained in this website.";
	}
	if (errorstr == "") {
		document.signupform.action="signup_check_duplicate.php";
		document.signupform.submit();
	} else { alert(errorstr); }
}

//---check basic profile before saving...
function membershipProfileValidation() {
	var errorstr = "";
	errorstr += checkitem(document.basicProfileForm.firstname,"First Name");
	errorstr += checkitem(document.basicProfileForm.lastname,"Last Name");
	errorstr += checkitem(document.basicProfileForm.nickname,"Nick Name");
	errorstr += checkitem(document.basicProfileForm.gender,"Gender");
	errorstr += checkitem(document.basicProfileForm.password,"Password");
	if (document.basicProfileForm.password.value.length < 7) {
		errorstr += "'Password' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.basicProfileForm.password_confirm,"Password Confirmation Field");
	if (document.basicProfileForm.password_confirm.value.length < 7) {
		errorstr += "'Password confirmation field' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.basicProfileForm.profession,"Profession");
	errorstr += checkitem(document.basicProfileForm.email,"Email");
	errorstr += checkitem(document.basicProfileForm.country,"Country");
	errorstr += checkitem(document.basicProfileForm.state,"State");
	errorstr += checkitem(document.basicProfileForm.city,"City");
	errorstr += checkitem(document.basicProfileForm.address,"Address");
	//---perform another check if password and confirmed password is the same.
	if (document.basicProfileForm.password.value != document.basicProfileForm.password_confirm.value) {
		errorstr += "Password and confirmed password must be the same.\n";
	}
	if (errorstr == "") {
		document.basicProfileForm.action="merchants_edit_update.php";
		document.basicProfileForm.submit();
	} else { alert(errorstr); }
}

//---check basic profile before saving...
function profileUpdateValidation() {
	var errorstr = "";
	errorstr += checkitem(document.basicProfileForm.firstname,"First Name");
	errorstr += checkitem(document.basicProfileForm.lastname,"Last Name");
	errorstr += checkitem(document.basicProfileForm.nickname,"Nick Name");
	errorstr += checkitem(document.basicProfileForm.gender,"Gender");
	errorstr += checkitem(document.basicProfileForm.password,"Password");
	if (document.basicProfileForm.password.value.length < 7) {
		errorstr += "'Password' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.basicProfileForm.password_confirm,"Password Confirmation Field");
	if (document.basicProfileForm.password_confirm.value.length < 7) {
		errorstr += "'Password confirmation field' can't be less than 7 characters.\n";
	}
	errorstr += checkitem(document.basicProfileForm.profession,"Profession");
	errorstr += checkitem(document.basicProfileForm.email,"Email");
	errorstr += checkitem(document.basicProfileForm.country,"Country");
	errorstr += checkitem(document.basicProfileForm.state,"State");
	errorstr += checkitem(document.basicProfileForm.city,"City");
	errorstr += checkitem(document.basicProfileForm.address,"Address");
	//---perform another check if password and confirmed password is the same.
	if (document.basicProfileForm.password.value != document.basicProfileForm.password_confirm.value) {
		errorstr += "Password and confirmed password must be the same.\n";
	}
	if (errorstr == "") {
		document.basicProfileForm.action="../merchant/profile_save.php";
		document.basicProfileForm.submit();
	} else { alert(errorstr); }
}

//---check before saving property...
function propertyValidation() {
	var errorstr = "";
	errorstr += checkitem(document.propertyform.property_name,"Property name");
	errorstr += checkitem(document.propertyform.location,"Location");
	errorstr += checkitem(document.propertyform.client_ctr,"Merchant");
	errorstr += checkitem(document.propertyform.developer,"Developer");
	errorstr += checkitem(document.propertyform.district,"District");
	errorstr += checkitem(document.propertyform.no_units,"No. of units");
	errorstr += IsNumericField(document.propertyform.no_units,"No. of units");
	errorstr += checkitem(document.propertyform.property_description,"Property description");
	if (errorstr == "") {
		document.propertyform.action="properties_add_save.php";
		document.propertyform.submit();
	} else { alert(errorstr); }
}

//---check before saving property...
function propertyEditValidation() {
	var errorstr = "";
	errorstr += checkitem(document.propertyform.property_name,"Property name");
	errorstr += checkitem(document.propertyform.location,"Location");
	errorstr += checkitem(document.propertyform.client_ctr,"Merchant");
	errorstr += checkitem(document.propertyform.developer,"Developer");
	errorstr += checkitem(document.propertyform.district,"District");
	errorstr += checkitem(document.propertyform.no_units,"No. of units");
	errorstr += IsNumericField(document.propertyform.no_units,"No. of units");
	errorstr += checkitem(document.propertyform.property_description,"Property description");
	if (errorstr == "") {
		document.propertyform.action="properties_edit_save.php";
		document.propertyform.submit();
	} else { alert(errorstr); }
}

//---check before saving variations...
function variationValidation() {
	var errorstr = "";
	errorstr += checkitem(document.variationform.bedroom,"Bedroom");
	errorstr += IsNumericField(document.variationform.bedroom,"Bedroom");
	errorstr += checkitem(document.variationform.bath,"Bathroom");
	errorstr += IsNumericField(document.variationform.bath,"Bathroom");
	errorstr += checkitem(document.variationform.sqm,"Area (sqm)");
	errorstr += IsNumericField(document.variationform.sqm,"Area (sqm)");
	errorstr += checkitem(document.variationform.recent_price,"Recent Price");
	errorstr += IsNumericField(document.variationform.recent_price,"Recent Price");
	errorstr += checkitem(document.variationform.current_price,"Current Price");
	errorstr += IsNumericField(document.variationform.current_price,"Current Price");
	if (errorstr == "") {
		document.variationform.action="properties_variations_add_save.php";
		document.variationform.submit();
	} else { alert(errorstr); }
}

//---check before saving variations...
function variationEditValidation() {
	var errorstr = "";
	errorstr += checkitem(document.variationform.bedroom,"Bedroom");
	errorstr += IsNumericField(document.variationform.bedroom,"Bedroom");
	errorstr += checkitem(document.variationform.bath,"Bathroom");
	errorstr += IsNumericField(document.variationform.bath,"Bathroom");
	errorstr += checkitem(document.variationform.sqm,"Area (sqm)");
	errorstr += IsNumericField(document.variationform.sqm,"Area (sqm)");
	errorstr += checkitem(document.variationform.recent_price,"Recent Price");
	errorstr += IsNumericField(document.variationform.recent_price,"Recent Price");
	errorstr += checkitem(document.variationform.current_price,"Current Price");
	errorstr += IsNumericField(document.variationform.current_price,"Current Price");
	if (errorstr == "") {
		document.variationform.action="properties_variations_edit_save.php";
		document.variationform.submit();
	} else { alert(errorstr); }
}

//---check before saving transactions...
function transactionValidation() {
	var errorstr = "";
	errorstr += checkitem(document.transactionform.level,"Level");
	errorstr += IsNumericField(document.transactionform.level,"Level");
	errorstr += checkitem(document.transactionform.sqm,"Area (sqm)");
	errorstr += IsNumericField(document.transactionform.sqm,"Area (sqm)");
	errorstr += checkitem(document.transactionform.price,"Transaction Price");
	errorstr += IsNumericField(document.transactionform.price,"Transaction Price");
	if (errorstr == "") {
		document.transactionform.action="properties_transactions_add_save.php";
		document.transactionform.submit();
	} else { alert(errorstr); }
}

//---check before saving transactions...
function transactionEditValidation() {
	var errorstr = "";
	errorstr += checkitem(document.transactionform.level,"Level");
	errorstr += IsNumericField(document.transactionform.level,"Level");
	errorstr += checkitem(document.transactionform.sqm,"Area (sqm)");
	errorstr += IsNumericField(document.transactionform.sqm,"Area (sqm)");
	errorstr += checkitem(document.transactionform.price,"Transaction Price");
	errorstr += IsNumericField(document.transactionform.price,"Transaction Price");
	if (errorstr == "") {
		document.transactionform.action="properties_transactions_edit_save.php";
		document.transactionform.submit();
	} else { alert(errorstr); }
}

//---check user data before saving...
function userValidation() {
	var errorstr = "";
	errorstr += checkitem(document.userform.username,"User name");
	errorstr += checkitem(document.userform.password,"Password");
	errorstr += checkitem(document.userform.firstname,"First name");
	errorstr += checkitem(document.userform.lastname,"Last name");
	errorstr += checkitem(document.userform.nickname,"Nick name");
	if (errorstr == "") {
		document.userform.action="users_add_check_dupe.php";
		document.userform.submit();
	} else { alert(errorstr); }
}

//---check user data before saving...
function userEditValidation() {
	var errorstr = "";
	errorstr += checkitem(document.userform.username,"User name");
	errorstr += checkitem(document.userform.password,"Password");
	errorstr += checkitem(document.userform.firstname,"First name");
	errorstr += checkitem(document.userform.lastname,"Last name");
	errorstr += checkitem(document.userform.nickname,"Nick name");
	if (errorstr == "") {
		document.userform.action="users_edit_save.php";
		document.userform.submit();
	} else { alert(errorstr); }
}

//---check before saving garage item...
function garageValidation(pSource) {
	var errorstr = "";
	errorstr += checkitem(document.garageform.title,"Product Name");
	errorstr += checkitem(document.garageform.description,"Description");
	errorstr += checkitem(document.garageform.price,"Selling Price");
	errorstr += IsNumericField(document.garageform.price,"Selling_price");
	if (errorstr == "") {
		if (pSource=="add") {
			document.garageform.action="garage_add_save.php";
		} else {
			document.garageform.action="garage_edit_save.php";
		}
		document.garageform.submit();
	} else { alert(errorstr); }
}

//---check before saving classified item...
function classifiedValidation(pSource) {
	var errorstr = "";
	errorstr += checkitem(document.classifiedform.title,"Classified Name");
	errorstr += checkitem(document.classifiedform.description,"Description");
	if (errorstr == "") {
		if (pSource=="add") {
			document.classifiedform.action="classifieds_add_save.php";
		} else {
			document.classifiedform.action="classifieds_edit_save.php";
		}
		document.classifiedform.submit();
	} else { alert(errorstr); }
}

//---check before saving discount item...
function discountValidation(pSource) {
	var errorstr = "";
	errorstr += checkitem(document.discountform.title,"Discount Item");
	errorstr += checkitem(document.discountform.description,"Description");
	if (errorstr == "") {
		if (pSource=="add") {
			document.discountform.action="discounts_add_save.php";
		} else {
			document.discountform.action="discounts_edit_save.php";
		}
		document.discountform.submit();
	} else { alert(errorstr); }
}

function DaysDifference(d1, d2) { // Date Objects, with similar times
	var one_day=1000*60*60*24;
	var diff = parseInt((d1.getTime()- d2.getTime()) / (one_day));
	return diff;
}

function convertHeight() {
	var height_cm;
	var height_ft = document.extendedProfileForm.height_english.value;
	height_cm = InchToCM(height_ft);
	document.extendedProfileForm.height.value = height_cm;
}

function convertWeight() {
	var weight_kg;
	var weight_lbs = document.extendedProfileForm.weight_english.value;
	weight_kg = LbToKg(weight_lbs);
	document.extendedProfileForm.weight.value = weight_kg;
}

function LbToKg(pounds) 
{ 
	var kg = pounds / 2.2; 
	return kg; 
} 
  
function KgToLb(kg) 
{ 
	var pounds = kg * 2.2; 
	return pounds; 
} 

function CMToInch(CM) 
{ 
    var Inch = CM / 2.54; 
    return Inch; 
} 
  
function InchToCM(Inch) 
{ 
	var CM = Inch * 2.54; 
	return CM;
} 

function updateHeight() {
	document.extendedProfileForm.height.value = document.extendedProfileForm.height_english.value;
}

function updateWeight() {
	document.extendedProfileForm.weight.value = document.extendedProfileForm.weight_english.value;
}

function updateTotalCost() {
	if (IsNumeric(document.formCart.quantity.value) == true) {
		if (document.formCart.quantity.value > 0) {
			var totalAmount = document.formCart.cost.value * document.formCart.quantity.value
			document.formCart.amount.value = totalAmount.toFixed(2);
		} else {
			alert("Quantity should be more than zero. System had set the quantity back to 1.");
			document.formCart.quantity.value = 1;
			document.formCart.amount.value = document.formCart.cost.value
		}
		
	} else {
		if (isNaN(parseFloat(document.formCart.quantity.value))) {
			alert("Quantity should be of numeric value. System had set the quantity back to 1.");
			document.formCart.quantity.value = 1;
			document.formCart.amount.value = document.formCart.cost.value;
		} else {
			if (document.formCart.quantity.value > 0) {
				alert("Quantity should be of whole number value. System had set the quantity to a whole number.");
				document.formCart.quantity.value = parseInt(document.formCart.quantity.value);
				document.formCart.amount.value = document.formCart.cost.value * document.formCart.quantity.value;
			} else {
				alert("Quantity should be more than zero. System had set the quantity back to 1.");
				document.formCart.quantity.value = 1;
				document.formCart.amount.value = document.formCart.cost.value; 
			}
		}
	}
}

function submitCart() {
	validateCart();
}

function submitCartEdit() {
	validateCartEdit();
}

function validateCart() {
	var errorstr = "";
	errorstr += checkitem(document.formCart.recipient,"Gift recipient");
	updateTotalCost();
	if (errorstr == "") {
		document.formCart.action = "member_cart_save.php";
		document.formCart.submit();
	} else { alert(errorstr); }
}

function validateCartEdit() {
	var errorstr = "";
	errorstr += checkitem(document.formCart.recipient,"Gift recipient");
	updateTotalCost();
	if (errorstr == "") {
		document.formCart.action = "member_cart_update.php";
		document.formCart.submit();
	} else { alert(errorstr); }
}

