/*
**     JavaScript Source Code
**     Created by Kalin Ganev
**     <KalinGanev [AT] Gmail (DOT) com>
**     Date Created:  2008-03-28
**     Last Modified: 2011-12-29
*/





function /*boolean*/ validateForm (obj_form, bool_isModeEdit, bool_isBuyer) {
	if (document.getElementById) {
		// Checking username:
		if (!bool_isModeEdit && document.getElementById('username')) {
			// OK: A "username" input field exists (add mode).
			if ('' == document.getElementById('username').value) {
				// Error: Add mode and username is blank.
				$.msgbox(gstr_msgErrorUsernameBlank, {type: 'error'}, function(buttonPressed) {document.getElementById('username').focus();});
				return false;
			}
		}

		// Checking passwords:
		if (document.getElementById('password') && document.getElementById('password2')) {
			// OK: A "password" and a "password2" password fields exist.
			if (document.getElementById('password').value == document.getElementById('password2').value) {
				// OK: Both passwords match.
				if (!bool_isModeEdit && '' == document.getElementById('password').value) {
					// Error: Add mode and password is blank.
					$.msgbox(gstr_msgErrorPasswordBlank, {type: 'error'}, function(buttonPressed) {document.getElementById('password').focus();});
					return false;
				}
			} else {
				// Error: Both passwords mismatch.
				$.msgbox(gstr_msgErrorPasswordsMismatch, {type: 'error'}, function(buttonPressed) {document.getElementById('password2').focus();});
				return false;
			}
		}

		var  obj_regExpEmail = new RegExp('^[a-z0-9._%-]+@[a-z0-9._%-]+\\.([a-z]{2,4}|museum|travel)$', 'i');
		if (bool_isBuyer) {
			// User to add/edit is of buyer type.

			// Checking e-mail address:
			if (document.getElementById('email')) {
				// OK: An "email" input field exists.
				if (!document.getElementById('email').value.match(obj_regExpEmail)) {
					// Error: E-mail address is incorrect.
					$.msgbox(gstr_msgErrorEmailIncorrect, {type: 'error'}, function(buttonPressed) {document.getElementById('email').focus();});
					return false;
				}
			}

			// Checking CAPTCHA code:
			if (document.getElementById('code_captcha')) {
				// OK: A "code_captcha" input field exists.
				if ('' == document.getElementById('code_captcha').value) {
					// Error: CAPTCHA code is blank.
					$.msgbox(gstr_msgErrorCodeCaptchaBlank, {type: 'error'}
							, function(buttonPressed) {document.getElementById('code_captcha').focus();});
					return false;
				} else {
					// OK: CAPTCHA code field is filled.
					var  obj_regExpCodeCaptcha = new RegExp('^[a-z0-9]{4}$', 'i');
					if (!document.getElementById('code_captcha').value.match(obj_regExpCodeCaptcha)) {
						// Error: CAPTCHA code is incorrect for sure.
						$.msgbox(gstr_msgErrorCodeCaptchaIncorrect, {type: 'error'}
								, function(buttonPressed) {document.getElementById('code_captcha').focus();});
						return false;
					}
				}
			}

			// Checking first name:
			if (document.getElementById('name_first')) {
				// OK: A "name_first" input field exists.
				if ('' == document.getElementById('name_first').value) {
					// Error: First name is blank.
					$.msgbox(gstr_msgErrorNameFirstBlank, {type: 'error'}, function(buttonPressed) {document.getElementById('name_first').focus();});
					return false;
				}
			}

			// Checking telephone1:
			if (document.getElementById('telephone1')) {
				// OK: A "telephone1" input field exists.
				if ('' == document.getElementById('telephone1').value) {
					// Error: Telephone1 is blank.
					$.msgbox(gstr_msgErrorTelephone1Blank, {type: 'error'}, function(buttonPressed) {document.getElementById('telephone1').focus();});
					return false;
				} else {
					// OK: "telephone1" field is filled.
					var  obj_regExpTel1 = new RegExp('^09[1|2|5|7|8|9][0-9]{6,7}$'/*'^08[4|5|6|7|8|9][0-9]{7}$'*/, 'g');
					if (!document.getElementById('telephone1').value.match(obj_regExpTel1)) {
						// Error: Telephone1 is incorrect.
						$.msgbox(gstr_msgErrorTelephone1Incorrect, {type: 'error'}
								, function(buttonPressed) {document.getElementById('telephone1').focus();});
						return false;
					}
				}
			}

			// Checking city:
			if (document.getElementById('city2_id')) {
				// OK: A "city2_id" pull-down menu exists.
				if (parseInt(document.getElementById('city2_id').value) <= 0) {
					// Error: A city option is NOT selected.
					$.msgbox(gstr_msgErrorCity, {type: 'error'}, function(buttonPressed) {document.getElementById('city2_id').focus();});
					return false;
				}
			}

			// Checking area:
			if (document.getElementById('area2_id')) {
				// OK: A "area2_id" pull-down menu exists.
				if (parseInt(document.getElementById('area2_id').value) <= 0) {
					// Error: An area option is NOT selected.
					$.msgbox(gstr_msgErrorArea, {type: 'error'}, function(buttonPressed) {document.getElementById('area2_id').focus();});
					return false;
				}
			}

			// Checking address:
			if (document.getElementById('address')) {
				// OK: An "address" input textarea exists.
				if ('' == document.getElementById('address').value) {
					// Error: Address is blank.
					$.msgbox(gstr_msgErrorAddressBlank, {type: 'error'}, function(buttonPressed) {document.getElementById('address').focus();});
					return false;
				}
			}
		} else {
			// User to add/edit is of administrator type.
			// Checking e-mail address if entered (not a compulsory field for users of administrator type):
			if (document.getElementById('email')) {
				// OK: An "email" input field exists.
				if ('' != document.getElementById('email').value && !document.getElementById('email').value.match(obj_regExpEmail)) {
					// Error: An e-mail address is entered and it's incorrect.
					$.msgbox(gstr_msgErrorEmailIncorrectAdmin, {type: 'error'}, function(buttonPressed) {document.getElementById('email').focus();});
					return false;
				}
			}
		}


		// All input data are OK.
		// Disabling "Submit" button (if exists):
		if (document.getElementById('btn_submit')) {
			window.disableButtonSubmit();
		}
	}
	return true;
} // validateForm() function




function /*void*/ focusUsername () {
	document.getElementById('username').focus();
}
function /*void*/ focusPassword () {
	document.getElementById('password').focus();
}
function /*void*/ focus1stField () {
	if (document.getElementById) {
		if (document.getElementById('username')) {
			// A "username" input field exists.
			 document.getElementById('username').value ? focusPassword() : focusUsername();
		} else {
			// A "username" input field does NOT exist.
			window.focusPassword();
		}
	}
}
if (document.getElementById) {
	window.onload = focus1stField;
}




function /*void*/ clearEmailRecommenderDefault () {
	if (gstr_emailRecommenderNoteDefault == document.getElementById('email_recommender').value) {
		// Default note is entered (by system).
		document.getElementById('email_recommender').value     = '';
		document.getElementById('email_recommender').className = 'register-field-long';
	}
}
function /*void*/ restoreEmailRecommenderDefault () {
	if ('' == document.getElementById('email_recommender').value) {
		// Note is NOT entered at all.
		document.getElementById('email_recommender').value     = gstr_emailRecommenderNoteDefault;
		document.getElementById('email_recommender').className = 'register-field-long comments_box_default';
	}
}


