// JavaScript Document
function isInvalidEmail(emailStr){
	if (emailStr.value!="") {
		i=emailStr.value.indexOf("@");
		j=emailStr.value.indexOf(".",i);
		k=emailStr.value.indexOf(",");
		kk=emailStr.value.indexOf(" ");
		jj=emailStr.value.lastIndexOf(".")+1;
		len=emailStr.value.length;
 		if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
 			return true
		}
 		else {
			alert("Invalid email address.\n" +
			emailStr.value + " is invalid!.");
			emailStr.focus();
			emailStr.select();
			return false;
		}
	}
}
function CheckEmail(emailStr) {
	var emailPat=/^[^@]+@[^@]+.[a-z]{2,}$/i;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("Email yang dimasukkan tidak sah!(semak tanda '@' and '.')");
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
    	alert("ID Pengguna tidak sah!.")
    	return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	  	for (var i=1;i<=4;i++) {
	    	if (IPArray[i]>255) {
	        	alert("Alamat IP tidak sah!!")
				return false
	    		}
    		}
    		return true
		}

		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			alert("Nama Domain tidak sah.")
    		return false
		}

		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 ||  domArr[domArr.length-1].length>3) {
   			alert("Email mesti diakhiri dengan 3 huruf untuk nama domain atau 2 huruf untuk negara.")
   			return false
		}
		if (len<2) {
   			var errStr="Nama host pada alamat tidak ditemui!"
   			alert(errStr)
   			return false
		}
		return true;
}

//declare functions
function isNumber(elemen) {
    /*var inputValue = elemen;
    var re = "/^[-]?\d*\d*$/";
    inputValue = inputValue.toString();
    if (!inputValue.match(re)) {
        alert("Please Fill Number Only.");
		value.focus();
		value.select();
        return true;
    } else {
		return false;
	}*/
	inputValue = elemen.value.match(/^\d+$/);
	if (inputValue==null){
		alert("Please Fill Number Only!!");
		elemen.focus();
		elemen.select();
		return true;
	}else
		return false;
}

function isInvalidChar(value) {
    var inputValue = value;
	re = /\$|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$/g;
	inputValue = inputValue.toString();
    if (!inputValue.match(re)) {
        return false;
    }
    return true;
}

function isValidKeluasan(value) {
    var inputValue = value;
    var re = /^[-]?\d*\.?\d*$/;
    inputValue = inputValue.toString();
    if (!inputValue.match(re)) {
        alert("Sila masukkan nombor sahaja.");
        return false;
    }
    return true;
}
function isNumberCost(value, whichCost) {
    var inputValue = value;
    var re = /^[-]?\d*\.?\d*$/;
    inputValue = inputValue.toString();
    if (!inputValue.match(re)) {
        alert("Nilai " + whichCost + " tidak sah.\nSila masukkan hanya angka dan nilai sen jika ada!");
        return false;
    }
    return true;
}
function isNotEmpty(value) {
    var re = /.+/;
    if(!value.match(re)) {
        return false;
    } else {
        return true;
    }
}
function isEmpty(element, elementName) {
	if(element.value == null || element.value.length == 0) {
		alert("Please Fill " + elementName + "!");
		element.focus();
		return true;
	} else {
		return false;
	}
}
function checkYear() {
	//check flow : check if empty --> check if numeric --> check if full year entered --> validate conditions
	//check if empty
	if(!isNotEmpty(this.built)) {
		alert("Sila isi tahun bina premis!");
		this.element.TahunBina.focus();
		this.element.TahunBina.select();
		result = false;
	} else if(!isNumber(this.built)) {
		this.element.TahunBina.focus();
		this.element.TahunBina.select();
		result = false;
	} else if(this.tahunbinaLength < 4 || this.tahunbinaLength > 4) {
		alert("Sila masukkan 4 angka untuk tahun bina premis, contoh: 2006");
		this.element.TahunBina.focus();
		this.element.TahunBina.select();
		result = false;
	} else if(this.built < 1950) {
		alert("Nilai minima bagi tahun bina premis ialah tahun 1950!");
		this.element.TahunBina.focus();
		this.element.TahunBina.select();
		result = false;
	} else if(this.tahunsiapLength != 0 && this.built > this.finished) {
		alert("Sila betulkan tahun bina premis!\nTahun bina tidak boleh melebihi tahun siap.");
		this.element.TahunBina.focus();
		this.element.TahunBina.select();
		result = false;
	} else if(!isNotEmpty(this.finished)) {
		alert("Sila isi tahun siap premis!");
		this.element.TahunSiap.focus();
		this.element.TahunSiap.select();
		result = false;
	} else if(!isNumber(this.finished)) {
		this.element.TahunSiap.focus();
		this.element.TahunSiap.select();
		result = false;
	} else if(this.tahunsiapLength < 4 || this.tahunsiapLength > 4) {
		alert("Sila masukkan 4 angka untuk tahun bina premis, contoh: 2006");
		this.element.TahunSiap.focus();
		this.element.TahunSiap.select();
		result = false;
	} else {
		result = true;
	}
	return result;
}
function checkSelection() {
	if(this.comboBox.selectedIndex == 0) {
		alert("No " + this.comboName + " selected!");
		this.comboBox.focus();
		return false;
	} else {
		return true;
	}
}
function isCheck(elemen) {
	if(elemen.checked == true) {
		return true;
	} else {
		return false;
	}
}
function costCheck() {
	if(!isNotEmpty(this.kosKeseluruhan)) {
		alert("Sila isi kos kesuluruhan!");
		this.form.KosKeseluruhan.focus();
		return false;
	} else if(!isNumberCost(this.kosKeseluruhan, "kos keseluruhan")) {
		this.form.KosKeseluruhan.focus();
		this.form.KosKeseluruhan.select();
		return false;
	} else if(this.kosElektrik.length != 0 && parseFloat(this.kosKeseluruhan) < parseFloat(this.kosElektrik)) {
		alert("Sila semak semula jumlah kos keseluruhan!\nKos elektrik melebihi kos keseluruhan.");
		this.form.KosKeseluruhan.focus();
		this.form.KosKeseluruhan.select();
		result = false;
	} else if(!isNotEmpty(this.kosElektrik)) {
		alert("Sila isi kos elektrik!");
		this.form.KosElektrik.focus();
		return false;
	} else if(!isNumberCost(this.kosElektrik, "kos elektrik")) {
		this.form.KosElektrik.focus();
		this.form.KosElektrik.select();
	} else {
		return true;
	}
}
function ValidateEmail() {
	//Check if empty
	if(isNotEmpty(this.email.value)) {
		if(!CheckEmail(this.email.value)) {
			this.email.focus();
			this.email.select();
			return false;
		}
	} else {
		return true;
	}
	return true;
}

//---------------------------  constructors ------------------------------------------
//define constructor and methods
function CheckTahun(element, tahunbina, tahunsiap) {
	//object properties
	this.element = element;
	this.built = tahunbina;	
	this.finished = tahunsiap;
	this.tahunbinaLength = tahunbina.length;
	this.tahunsiapLength = tahunsiap.length;
	//define object methods [methods = functions]
	this.isValidTahun = checkYear;
}
function CheckCombo(comboBox, comboName) {
	//alert(comboName);
	//properties
	this.comboBox = comboBox;
	this.comboName = comboName;
	//methods
	this.isSelected = checkSelection;
}
function CheckKos(form, total, elektrik) {
	//properties
	this.form = form;
	this.kosKeseluruhan = total;
	this.kosElektrik = elektrik;
	//methods
	this.isValidKos = costCheck;
}
function CheckGPS(theForm, gpsNumber1, gpsNumber2) {
	//properties
	this.theForm = theForm;
	this.gps1 = gpsNumber1;
	this.gps2 = gpsNumber2;
	this.gps1Length = gpsNumber1.length;
	this.gps2Length = gpsNumber2.length;
	//method
	this.isValidGPS = checkGPSNumber;
}
function CheckKontraktor(Form) {
	//properties
	this.kontraktorForm = Form;
	this.kontraktorDaerah = Form.Daerah;
	this.kontraktorName = Form.NamaKontraktor;
	this.kontraktorAlamat = Form.Alamat1;
	this.kontraktorBandar = Form.Bandar;
	this.kontraktorPoskod = Form.Poskod;
	this.kontraktorTelefonPejabat = Form.TelefonP;
	this.kontraktorHP = Form.TelefonHP;
	this.kontraktorFax = Form.Fax;
	this.kontraktorEmail = Form.Email;
	this.poskodLength = Form.Poskod.value.length;
	this.telefonLength = Form.TelefonP.value.length;
	this.hpLength = Form.TelefonHP.value.length;
	//method
	this.isValidKontraktor = checkTheKontraktor;
}
function EmailChecker(FormName) {
	//properties
	this.EmailForm = FormName;
	this.email = FormName.Email;
	//method
	this.isValidEmail = ValidateEmail;
}
// ------------------------------------------------------------------------------------