function ChkAgentLogin(frm){
	if (frm.agent_code.value == "" || frm.agent_code.value.length < 6) {
		alert("Please enter your agent code!")
		frm.agent_code.focus()
		return false
	}
	if (frm.agent_pw.value == "" || frm.agent_pw.value.length < 6) {
		alert("Please enter your agent password!")
		frm.agent_pw.focus()
		return false
	}
}

function ChkAddListing(frm) {
	if (frm.landlord.value == "" || frm.landlord.value.length < 2) {
		alert("Please enter the landlord's name in full!")
		frm.landlord.focus()
		return false
	}
	if (frm.tel_home.value == "" || frm.tel_home.value.length < 7 || !IsNumeric(frm.tel_home.value)) {
		alert("Please enter the landlord's home telephone number!")
		frm.tel_home.focus()
		return false
	}
	if (frm.tel_cell.value == "" || frm.tel_cell.value.length < 10 || !IsNumeric(frm.tel_cell.value)) {
		alert("Please enter the landlord's cell phone number!")
		frm.tel_cell.focus()
		return false
	}
	if (frm.email.value == "") {
		alert("Please enter the landlord's e-mail address!")
		frm.email.focus()
		return false
	}
	if (frm.email.value != "") {
		if (!validEmail(frm.email.value)) {
			alert("The Email address you entered is invalid. Please enter a valid Email address!")
			frm.email.focus()
			return false
		}
	}
	if (frm.address.value == "" || frm.address.value.length < 5) {
		alert("Please enter the address in full!")
		frm.address.focus()
		return false
	}
	if (frm.suburb.value == "" || frm.suburb.value.length < 2) {
		alert("Please select a suburb!")
		frm.suburb.focus()
		return false
	}
	if (!computeJD(frm.year.value,frm.month.value,frm.day.value)) {
		alert("Available date can only be in the future!")
		frm.day.focus()
		return false
	}
	if (frm.amount.value == "" || frm.amount.value.length < 3 || !IsNumeric(frm.amount.value)) {
		alert("Please enter an amount in Rands!")
		frm.amount.focus()
		return false
	}
	if (frm.category.value == "-- select --") {
		alert("Please select the property category!")
		frm.category.focus()
		return false
	}
	if (frm.type.value == "-- select --") {
		alert("Please select the property type!")
		frm.type.focus()
		return false
	}
	return true
}

function ChkContact(frm) {
	now = new Date
	thisYear = now.getYear()

	if (frm.name.value == "") {
		alert("Please enter your name!")
		frm.name.focus()
		return false
	}
	if (frm.email.value == "") {
		alert("Please enter the a e-mail address!")
		frm.email.focus()
		return false
	}
	if (frm.email.value != "") {
		if (!validEmail(frm.email.value)) {
			alert("The Email address you entered is invalid. Please enter a valid Email address!")
			frm.email.focus()
			return false
		}
	}
	if (frm.cell.value == "") {
		alert("Please enter your cell number!")
		frm.cell.focus()
		return false
	}
	if (frm.cell.value.length < 10){
      alert("Please enter your cell number in the correct format 0825671090");
      frm.cell.focus();
      return (false);
    }else{
      if (!IsNumeric(frm.cell.value)) {
	    alert("Please enter your valid cell number!")
	    frm.cell.focus()
        return false
	  }
	}	
	if (frm.comment.value == "") {
		alert("Please enter a comment!")
		frm.comment.focus()
		return false
	}
	return true
}


function validEmail(email) {
	invalidChars = " /:,;"
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@", 1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@", atPos + 1) > -1) {
		return false
	}
	periodPos = email.indexOf(".", atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length) {
		return false
	}
	return true
}
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789/?.>,<';:]}[{\|=+-_)(*&^%$#@!~` ";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function computeJD(theY,theM,theD) {
    MM=eval(theM)
    DD=eval(theD)
    YY=eval(theY)
    with (Math) {  
      GGG = 1;
      if (YY <= 1585) GGG = 0;
      JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
      S = 1;
      if ((MM - 9)<0) S=-1;
      A = abs(MM - 9);
      J1 = floor(YY + S * floor(A / 7));
      J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
      JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
      JD = JD + 1721027 + 2 * GGG + 367 * YY;
    }
	var jdenter = JD
	var jdtoday = todayJD();
	
    if (jdenter < jdtoday) {
      return false
    } else {
	  return true
    }
}
function todayJD() {
  var today = new Date()
  var month = today.getMonth()
  var year = today.getYear()
  var day = today.getDate()
  if(day<10) day = "0" + day
  if(month<10) month= "0" + month 
  if(year<1000) year+=1900

    MM=eval(month)
    DD=eval(day)
    YY=eval(year)
    with (Math) {  
      GGG = 1;
      if (YY <= 1585) GGG = 0;
      JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
      S = 1;
      if ((MM - 9)<0) S=-1;
      A = abs(MM - 9);
      J1 = floor(YY + S * floor(A / 7));
      J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
      JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
      JD = JD + 1721027 + 2 * GGG + 367 * YY;
    }
 
  return JD
}
