//Standard site functions
function fnChangeImage(Obj, Img){
	Obj.src = '../images/'+Img;
}

function fnSubmitForm(Obj1, IPValue){
		Obj1.mode.value = IPValue;
		Obj1.submit();
}

function fnSubmitItem(obj){
	var i
	for (i=0; i<document.all.item(obj).options.length; i++){
			document.all.item(obj).options[i].selected = true
	}
	
	return true;
}


function fnProfileSubmitForm(IPValue){
		document.frmProfile.mode.value = IPValue;
		document.frmProfile.submit();
}

function fnCheckKeyPress(Obj, Obj1, IPValue){
	if (Obj == 13){
		Obj1.mode.value = IPValue;
		Obj1.submit();
	}
}

function fnStatusChange(Obj){
	window.status = Obj
}

function fbreakout(obj){
	if (window.top!=window.self) 
	{
		window.top.location=obj
	}
}

function addCatItem(obj, obj1, obj2, obj3){
	var i
	
	for (i=0; i<document.all.item(obj1).options.length; i++){
		if (document.all.item(obj1).options[i].selected==true){
			addNewItem(obj, obj3, obj2, document.all.item(obj1).options[i].value, document.all.item(obj1).options[i].text)
		}
	}	
}

function addNewItem(obj, obj1, obj2, id, name){
	var i
	addOption = true
	for (i=0; i<document.all.item(obj2).options.length; i++){
		if(document.all.item(obj2).options[i].value == id){
			addOption = false
		}
	}
	
	if (addOption == true){
		document.all.item(obj2).options[document.all.item(obj2).options.length] = new Option(name, id)

		if (obj == 'event' || obj == 'categories'){
			if (document.all.item(obj2).options.length == 1){
				document.all.item(obj1).value = id
			}else{
				document.all.item(obj1).value = document.all.item(obj1).value +','+id
			}
		}
	}
}

function removeCatItem(obj, obj1, obj2){
	var i
	var oElements
	var CatID
	var SelCat
	for (i=0; i<document.all.item(obj2).options.length; i++){
		if (document.all.item(obj2).options[i].selected==true){
			CatID = document.all.item(obj2).options[i].value
			document.all.item(obj2).options.remove(i)
		}
	}
	
	if (obj == 'event' || obj == 'categories'){
		for (i=0; i<document.all.item(obj2).options.length; i++){
			document.all.item(obj2).options[i].selected = true
		}
		
		oElements = document.all.item(obj2).value.split(",");
		document.all.item(obj1).value = "";
		for (i=0;i<oElements.length;i++){
			if (oElements.length == 1){
				document.all.item(obj1).value = oElements[i]
			}else{
				document.all.item(obj1).value = document.all.item(obj1).value +','+oElements[i]
			}
		}
	}
	
	if (obj == 'request'){
		document.location.replace("default.asp?cid=7&rid="+CatID)
	}
}

function fnValidateRequest(obj, lan){
	if (document.getElementById('cboTitle').value == ""){
		if (lan == "A"){
			alert("Gee jou titel asseblief");
		}else{
			alert("Please provide a title");
		}
		document.getElementById('cboTitle').focus();
		return false;
	}
	
	if (document.getElementById('txtFirstName').value == ""){
		if (lan == "A"){
			alert("Gee jou voorletters asseblief");
		}else{
			alert("Please provide a first name");
		}
		document.getElementById('txtFirstName').focus();
		return false;
	}
	
	if (document.getElementById('txtLastName').value == ""){
		if (lan == "A"){
			alert("Gee jou van asseblief");
		}else{
			alert("Please provide a last name");
		}
		document.getElementById('txtLastName').focus();
		return false;
	}
	
	if (document.getElementById('txtProvince').value == ""){
		if (lan == "A"){
			alert("Gee jou provinsie asseblief");
		}else{
			alert("Please select your province");
		}
		document.getElementById('txtProvince').focus();
		return false;
	}
	
	if (document.getElementById('txtMemberNo').value == ""){
		if (lan == "A"){
			alert("Gee jou lidmaatskapnommer asseblief");
		}else{
			alert("Please provide your membership number");
		}
		document.getElementById('txtMemberNo').focus();
		return false;
	}
	
	if (document.getElementById('txtPwd').value == ""){
		if (lan == "A"){
			alert("Gee jou wagwoord asseblief");
		}else{
			alert("Please provide a password for your account");
		}
		document.getElementById('txtPwd').focus();
		return false;
	}
	
	if (document.getElementById('txtPrincipal').value == ""){
		if (lan == "A"){
			alert("Is jy a principal of nie");
		}else{
			alert("Are you a principle");
		}
		document.getElementById('txtPrincipal').focus();
		return false;
	}
	
	if (document.getElementById('txtContactEmail').value == ""){
		if (lan == "A"){
			alert("Gee jou e-pos asseblief");
		}else{
			alert("Please provide a contact email address");
		}
		document.getElementById('txtContactEmail').focus();
		return false;
	}
	
	var addr = document.getElementById('txtContactEmail').value
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  	if (lan == "A"){
				alert("Jou e-pos het verkeet characters");
			}else{
				alert('The email address contains invalid characters');
			}
		  	return false;
	   }
	}
	
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
			if (lan == "A"){
				alert("Jou titel het non ascii characters");
			}else{
				alert("The email address contains non ascii characters.");
			}
		  	
		 	return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (lan == "A"){
			alert("Jou e-pos moet 'n @ he");
		}else{
			alert('The email address must contain an @');
		}
 		return false;
	}
	if (atPos == 0) {
		if (lan == "A"){
			alert("Jou e-pos moet nie met 'n @ begin nie");
		}else{
			alert('The email address must not start with @');
		}
	   	return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   	if (lan == "A"){
			alert("Jou e-pos moet net een @ he");
		}else{
			alert('The email address must contain only one @');
		}
	   	return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (lan == "A"){
			alert("Jou e-pos moet 'n . he");
		}else{
			alert('The email address must contain a period in the domain name');
		}
	    return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (lan == "A"){
			alert("Jou e-pos is verkeet");
		}else{
			alert('The period must not immediately follow @ in email address');
		}
	   	return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (lan == "A"){
			alert("Jou e-pos is verkeet");
		}else{
			alert('The period must not immediately precede @ in email address');
		}
	   	return false;
	}
	if (addr.indexOf('..',0) != -1) {
	  	if (lan == "A"){
			alert("Jou e-pos is verkeet");
		}else{
			alert('The two periods must not be adjacent in email address');
		}
	  	return false;
	}
	
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (lan == "A"){
			alert("Jou e-pos is verkeet");
		}else{
			alert('The primary domain in email address is invalid');
		}
	   	
	   	return false;
	}	
	
	if (document.getElementById('txtContactNo').value == ""){
		if (lan == "A"){
			alert("Gee jou kontaknommer asseblief");
		}else{
			alert("Please provide a preferred contact number");
		}
		document.getElementById('txtContactNo').focus();
		return false;
	}
	
	if (document.getElementById('txtContactOther').value == ""){
		if (lan == "A"){
			alert("Gee jou ander kontaknommer asseblief");
		}else{
			alert("Please provide another contact number");
		}
		document.getElementById('txtContactOther').focus();
		return false;
	}
}

function fnValidatePost(lang){
	var oArray = document.getElementById("sid").value.split("|");
	var bSubjectSelected
	
	if (lang == "A"){
		bSubjectSelected = false;
		for (i=0;i<oArray.length;i++){
			if (document.getElementById("chk"+oArray[i]).checked == true){
				bSubjectSelected = true;
				break;
			}
		}
		
		if (!bSubjectSelected){
			alert("Kies asseblief 'n vak")
			return false;
		}
		
		if (document.getElementById("txtStartDate").value == ""){
			alert("Gee vanaf wanneer jy beskikbaar is");
			document.getElementById("txtStartDate").focus();
			return false;
		}
		
		if (document.getElementById("txtPosType").value == ""){
			alert("Gee die tipe pos");
			document.getElementById("txtPosType").focus();
			return false;
		}
		
		if (document.getElementById("txtLocation").value == ""){
			alert("Gee waar die pos is");
			document.getElementById("txtLocation").focus();
			return false;
		}
		
		bSubjectSelected = false;
		for (i=0;i<document.all.item("txtType").length;i++){
			if (document.all.item("txtType")[i].checked == true){
				bSubjectSelected = true;
				break;
			}
		}
		
		if (!bSubjectSelected){
			if (document.getElementById("txtTypeOther").value == ""){
				alert("Kies asseblief die tipe skool")
				return false;
			}
		}
		
		if (document.getElementById("txtContact").value == ""){
			alert("Gee 'n kontak naam");
			document.getElementById("txtContact").focus();
			return false;
		}
		
		if (document.getElementById("txtContactTel").value == ""){
			alert("Gee 'n kontak nomer");
			document.getElementById("txtContactTel").focus();
			return false;
		}
		
		if (document.getElementById("txtContactEmail").value == ""){
			alert("Gee 'n kontak epos");
			document.getElementById("txtContactEmail").focus();
			return false;
		}
		
		return true;
	}else{
		bSubjectSelected = false;
		for (i=0;i<oArray.length;i++){
			if (document.getElementById("chk"+oArray[i]).checked != true){
				bSubjectSelected = true
				break;
			}
		}
		
		if (!bSubjectSelected){
			alert("Kies 'n vak")
			return false;
		}
		
		if (document.getElementById("txtStartDate").value == ""){
			alert("Please provide a start date");
			document.getElementById("txtStartDate").focus();
			return false;
		}
		
		if (document.getElementById("txtPosType").value == ""){
			alert("Please provide the position type");
			document.getElementById("txtPosType").focus();
			return false;
		}
		
		if (document.getElementById("txtLocation").value == ""){
			alert("Please provide a location");
			document.getElementById("txtLocation").focus();
			return false;
		}
		
		bSubjectSelected = false;
		for (i=0;i<document.all.item("txtType").length;i++){
			if (document.all.item("txtType")[i].checked){
				bSubjectSelected = true;
				break;
			}
		}
		
		if (!bSubjectSelected){
			if (document.getElementById("txtTypeOther").value == ""){
				alert("Please select what type of school it is")
				return false;
			}
		}
		
		if (document.getElementById("txtContact").value == ""){
			alert("Please provide a contact name");
			document.getElementById("txtContact").focus();
			return false;
		}
		
		if (document.getElementById("txtContactTel").value == ""){
			alert("Please provide your contact telephone number");
			document.getElementById("txtContactTel").focus();
			return false;
		}
		
		if (document.getElementById("txtContactEmail").value == ""){
			alert("Please provide a contact email");
			document.getElementById("txtContactEmail").focus();
			return false;
		}
		
		return true;
	}
}

function fnFPwdValidate(obj){
	if (document.getElementById('txtContactEmail').value == ""){
		alert("Please provide a contact email address");
		document.getElementById('txtContactEmail').focus();
		return false;
	}
	
	var addr = document.getElementById('txtContactEmail').value
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  alert('The email address contains invalid characters');
		  return false;
	   }
	}
	
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  alert("The email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   alert('The email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   alert('The email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   alert('The email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   alert('The email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   alert('The period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   alert('The period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	  alert('The two periods must not be adjacent in email address');
	   return false;
	}
	
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   alert('The primary domain in email address is invalid');
	   return false;
	}
}

function fnRegValidate(obj){
	if (document.getElementById('txtCompanyName').value == ""){
		alert("Please provide your company name");
		document.getElementById('txtCompanyName').focus();
		return false;
	}
	
	if (document.getElementById('txtContactPerson').value == ""){
		alert("Please provide a contact persons name");
		document.getElementById('txtContactPerson').focus();
		return false;
	}
	
	if (document.getElementById('txtContactNo').value == ""){
		alert("Please provide a contact persons number");
		document.getElementById('txtContactNo').focus();
		return false;
	}
	
	if (document.getElementById('txtContactEmail').value == ""){
		alert("Please provide a contact email address");
		document.getElementById('txtContactEmail').focus();
		return false;
	}
	
	var addr = document.getElementById('txtContactEmail').value
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  alert('The email address contains invalid characters');
		  return false;
	   }
	}
	
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  alert("The email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   alert('The email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   alert('The email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   alert('The email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   alert('The email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   alert('The period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   alert('The period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	  alert('The two periods must not be adjacent in email address');
	   return false;
	}
	
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   alert('The primary domain in email address is invalid');
	   return false;
	}	
}

function fnPreload(){ 
  var args = fnPreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

function fnValidateSAID()
{
	var SAIDRegEx = /^(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7}))/;
	if (SAIDRegEx.test(document.getElementById("txtIDNo").value)){ 
		return true; 
	}else{
		alert("Please provide a valid SA ID number to proceed.")
		document.getElementById("txtIDNo").value = "";
		document.getElementById("txtIDNo").focus();
		return false; 
	}
}

function fnSchool(prov){
	prov = document.getElementById('province').value
	processRequest("/process.asp?mode=school&provid="+prov)
}

function fnValidateBooking(){
	if (document.getElementById('Employees').value == ""){
		alert("Voorsien getal personeel / Provide number of personnel")
		document.getElementById('Employees').focus();
		return false;
	}
	if (document.getElementById('Members').value == ""){
		alert("Voorsien getal lede / Provide number of members")
		document.getElementById('Members').focus();
		return false;
	}
	if (document.getElementById('Voters').value == ""){
		alert("Voorsien getal stemmers / Provide number of voters")
		document.getElementById('Voters').focus();
		return false;
	}
}

function checkIt(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Only numbers / Slegs getalle")
        return false
    }
    status = ""
    return true
}
