// JavaScript Document
function checkLength(ctl) {
	//debugger;
	var sText = new String(ctl.value);
	//alert(sText.length);
	if(sText.length >= ctl.maxLength) {
		var iTotalControls = ctl.form.length;
		var i = 0;
		while(i<iTotalControls) {
			if(ctl.form[i] == ctl)
				break;
			i++;
		}
		while(++i < iTotalControls) {
			if(ctl.form[i].tagName == 'INPUT' | ctl.form[i].tagName == 'input') {
				try {
					ctl.form[i].focus();
				}
				catch(err){}
				break;
			}
		}
	}
}

function validatePassword(){
	// Make quick references to our fields
	var cpassword = document.getElementById('cpassword');
	var npassword = document.getElementById('npassword');
	var cnpassword = document.getElementById('cnpassword');
	var opassword = document.getElementById('opassword');

	// Check each input in the order that it appears in the form!
	if(isEmpty(cpassword, "Please enter your current password.")){
		if(isMatch(cpassword, opassword, "Please enter current password to update.")){
			if(isEmpty(npassword, "Please enter your new password.")){
				if(isEmpty(cnpassword, "Please re-enter your new password.")){
					if(isMatch(npassword, cnpassword, "New passwords do not match.")){
						return true;
					}
				}
			}
		}
	}

	return false;

}

function validateInfo(){
	// Make quick references to our fields
	var fname = document.getElementById('fname');
	var lname = document.getElementById('lname');
	var country_id = document.getElementById('country_id');
	var phone = document.getElementById('phone');

	// Check each input in the order that it appears in the form!
	if(isEmpty(fname, "Please enter your first name.")){
		if(isEmpty(lname, "Please enter your last name.")){
			if(madeSelection(country_id, "Please choose your country.")){
				if(isEmpty(phone, "Please enter your phone number.")){
					return true;
				}
			}
		}
	}

	return false;

}

function validateHProperty(){
	// Make quick references to our fields
	var type_id = document.getElementById('type_id');
	var avail_total = document.getElementById('avail_total');
	var minx = document.getElementById('min');
	var maxx = document.getElementById('max');

	// Check each input in the order that it appears in the form!
	if(madeSelection(type_id, "Please choose type.")){
		if(isEmpty(avail_total, "Please enter your total avail.")){
			if(isEmpty(minx, "Please enter your minimum.")){
				if(isEmpty(maxx, "Please enter your maximum.")){
					return true;
				}
			}
		}
	}

	return false;

}

function validateNProperty(){
	// Make quick references to our fields
	var type_id = document.getElementById('type_id');
	var minx = document.getElementById('min');
	var maxx = document.getElementById('max');

	// Check each input in the order that it appears in the form!
	if(madeSelection(type_id, "Please choose type.")){
		if(isEmpty(minx, "Please enter your minimum.")){
			if(isEmpty(maxx, "Please enter your maximum.")){
				return true;
			}
		}
	}

	return false;

}


function isMatch(elem, elem2, helperMsg){
	if(elem.value != elem2.value){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == ""){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}