var reNum = /^[0-9]*$/i; // Regex number only

function isLegal(str, pattern) {
	// Returns TRUE if str match the given pattern
	var re = pattern;
	return re.test(str);
}

function isEmail(str)
{
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function checkinput(form)
{
	if(trim(form.title.value).length==0) {
		hboxAlert(form.title,'Please select your title.');
		return false ;
	}
	if(trim(form.firstname.value).length==0) {
		hboxAlert(form.firstname);
		return false ;
	}
	if(trim(form.lastname.value).length==0) {
		hboxAlert(form.lastname);
		return false ;
	}
	if(trim(form.email.value).length==0) {
		hboxAlert(form.email);
		return false ;
	}
	else
	{ 
		if (!isEmail(form.email.value)){
			hboxAlert(form.email,'Please input the right format.');
			return false ;
		}
	}
	if(trim(form.emailconfirm.value).length==0) {
		hboxAlert(form.emailconfirm);
		return false ;
	}
	else
	{ 
		if (!isEmail(form.emailconfirm.value)){
			hboxAlert(form.emailconfirm,'Please input the right format.');
			return false ;
		}
	}
	if (form.email.value != form.emailconfirm.value){
		hboxAlert(form.emailconfirm,'Confirm email should be same as the email address above.');
		return false ;		
	}
	
	if(trim(form.mobile.value).length != 10) {
		hboxAlert(form.mobile,'Please input the right format.');
		return false ;
	}
	else
	{ 
		if (!isLegal(form.mobile.value,reNum)){
			hboxAlert(form.mobile,'Please input the number only.');
			return false ;
		}
	}
	if(trim(form.subarusite.value).length==0) {
		hboxAlert(form.subarusite,'Please select a Subaru location.');
		return false ;
	}
	confirmation(form);
	//form.submit();
}

function resetForm(obj) {
	if (confirm("All data will be lost, are you sure?"))
		if (obj)
			obj.reset();
}


function confirmation(form)
{
 var title = form.title.value;
 var firstname = form.firstname.value;
 var lastname = form.lastname.value;
 var email = form.email.value;
 var mobile = form.mobile.value;
 var subarusite = form.subarusite.value;
 
 var where_to= confirm("Please review these details before you click OK\n\nTitle: "+title+"\nFirst Name: "+firstname+"\nLast Name: "+lastname+"\nEmail: "+email+"\nMobile: "+mobile+"\nSelected location: "+subarusite);
 if (where_to== true)
 {
   form.submit();
 }
}