//checkForgot
function checkForgot() {
	var secret = document.forgot_form.secret;
	var email = document.forgot_form.email;
	var sec_number = document.forgot_form.sec_number;
	var retype = document.forgot_form.retype;
	
	if (email.value == "") {
		alert("Please enter Your E-Mail Address");
		email.focus();
		return false;
	}
	if (retype.value == "") {
		alert("Please Re-Type Security Number");
		retype.focus();
		return false;
	}
	if (retype.value != secret.value) {
		alert("The Security Number you entered was incorrect. Please verify");
		retype.value = "";
		retype.focus();
		return false;
	}
	document.forgot_form.action = "forgot.php";
	return true;
}

//addTemp()
function addTemp(temp) {
	document.getElementById("message").value = temp + document.getElementById("message").value;
}

//checkSignup
function checkSignup() {
	var firstname = document.signup_form.firstname;
	var lastname = document.signup_form.lastname;
	var company_name = document.signup_form.company_name;
	var cellphone = document.signup_form.cellphone;
	var email = document.signup_form.email;
	
	if (firstname.value == "") {
		alert("Please enter First Name");
		firstname.focus();
		return false;
	}
	if (lastname.value == "") {
		alert("Please enter Last Name");
		lastname.focus();
		return false;
	}
	if (company_name.value == "") {
		alert("Please enter Company Name");
		company_name.focus();
		return false;
	}
	if (cellphone.value == "") {
		alert("Please enter Cellphone");
		cellphone.focus();
		return false;
	}
	if (email.value == "") {
		alert("Please enter E-Mail Address");
		email.focus();
		return false;
	}
	if (email.value.indexOf('@') == -1) {
		alert("Please enter a valid E-Mail Address. This will be where the user's details will be sent.");
		email.focus();
		return false;
	}
	if (email.value.indexOf('.') == -1) {
		alert("Please enter a valid E-Mail Address. This will be where the user's details will be sent.");
		email.focus();
		return false;
	}
	var cell = document.signup_form.cellphone.value;
	if (cell.substring(0,1) == "0") {
		if (cell.length == 10) {
		} else {
			alert("Cellphone is invalid. Please verify");
			cellphone.focus();
			return false;
		}
	} else {
		alert("Cellphone is invalid. Please verify");
		cellphone.focus();
		return false;
	}
	document.signup_form.action = "index-signup.php";
	return true;
}

//checkSend
function checkSend() {
	var sel = document.myform.send;
	if (sel.selectedIndex == 0) {
		hide("sched_table");
	} else {
		show("sched_table");
	}
}

//checkRegister
function checkRegister() {
	var company_name = document.myform5.company_name;
	var display_name = document.myform5.display_name;
	
	if (company_name.value == "") {
		alert("Please enter Company Name");
		company_name.focus();
		return false;
	}
	if (display_name.value == "") {
		alert("Please enter Display Name");
		display_name.focus();
		return false;
	}
	document.myform5.action = "sms-users-company.php";
	return true;
}

//checkPassword
function checkPassword() {
	var old_pass = document.myform4.old_password;
	var hid_pass = document.myform4.hid_password;
	var new_pass = document.myform4.new_password;
	var con_pass = document.myform4.retype_password;
	
	if (old_pass.value == "") {
		alert("Please enter Old Password");
		old_pass.focus();
		return false;
	}
	if (new_pass.value == "") {
		alert("Please enter New Password");
		new_pass.focus();
		return false;
	}
	if (con_pass.value == "") {
		alert("Please Confirm New Password");
		con_pass.focus();
		return false;
	}
	if (hid_pass.value != old_pass.value) {
		alert("Old Password is incorrect. Please verify");
		old_pass.value = "";
		old_pass.focus();
		return false;
	}
	if (new_pass.value != con_pass.value) {
		alert("Passwords don't match. Please verify");
		new_pass.value = "";
		con_pass.value = "";
		new_pass.focus();
		return false;
	}
	document.myform4.action = "sms-users-password.php";
	return true;
}

//checkDeleteUser
function checkDeleteSched(id) {
	var answer = confirm("Are you sure you want to delete this SMS from the schedule?");
	if (answer) {
		window.location = "sms-schedule-rem.php?sms_id=" + id;
	} else {}
}

//checkLogin
function checkLogin() {
	var username = document.myform.username;
	var password = document.myform.password;
	
	if (username.value == "") {
		alert("Please enter Username");
		username.focus();
		return false;
	}
	if (password.value == "") {
		alert("Please enter Password");
		password.focus();
		return false;
	}
	document.myform.action = "sms-validate.php";
	return true;
}

//checkEditContact
function checkEditContact(id) {
	var birthday_day = document.getElementById("birthday_day_"+id).selectedIndex;
	var birthday_month = document.getElementById("birthday_month_"+id).selectedIndex;
	
	if (birthday_day == 0) {
		if (birthday_month != 0) {
			alert("Please select Birthday Day");
		} else {}
	} else {
		if (birthday_month == 0) {
			alert("Please select Birthday Month");
		} else {}
	}
	
	document.forms['myform_'+id].action = "sms-contacts-edit.php";
	return true;
}

//checkEditUser
function checkEditUser(id) {
	var birthday_day = document.getElementById("birthday_day_"+id).value;
	var birthday_month = document.getElementById("birthday_month_"+id).value;
	
	if (birthday_day != "") {
		var intval = parseInt(birthday_day);
		var tostring = intval.toString();
		if (intval > 31) {
			alert("Invalid birthday. Please check birthday 'day' value");
			return false;
		} else if (intval == 0) {
			alert("Invalid birthday. Please check birthday 'day' value");
			return false;
		} else {
			if (tostring == "NaN") {
				alert("Invalid birthday. Please check birthday 'day' value");
				return false;
			} else {}
		}
		if (birthday_month != "") {
			var intval = parseInt(birthday_month);
			var tostring = intval.toString();
			if (intval > 12) {
				alert("Invalid birthday. Please check birthday 'month' value");
				return false;
			} else if (intval == 0) {
				alert("Invalid birthday. Please check birthday 'month' value");
				return false;
			} else {
				if (tostring == "NaN") {
					alert("Invalid birthday. Please check birthday 'month' value");
					return false;
				} else {}
			}
			
		}
	}
	document.forms['myform_'+id].action = "sms-users-edit.php";
	return true;
}

//initSMS()
function initSMS() {
	var signature = document.myform.signature;
	if (signature.value != "") {
		document.myform.message.value = signature.value;
	}
	document.myform.to.selectedIndex = 0;
	document.myform.group_id.selectedIndex = 0;
	document.myform.con_id.selectedIndex = 0;
	document.myform.send.selectedIndex = 0;
	document.myform.timepicker.value = "10:00 am";
	document.myform.datepicker.value = "";
	document.getElementById("template_check").checked = false;
	document.getElementById("template_hid").value = "";
	
	checkToSel();
	checkCharactersApp();
	hide("sched_table");
}

//checkDeleteUser
function checkDeleteUser(id, user) {
	user = user.replace("'", "");
	var answer = confirm("Are you sure you want to delete the following user?\n\n" + user);
	if (answer) {
		window.location = "sms-users-rem.php?user_id=" + id;
	} else {}
}

//checkNewUser
function checkNewUser() {
	var firstname = document.myform.firstname;
	var lastname = document.myform.lastname;
	var cellphone = document.myform.cellphone;
	var email = document.myform.email;
	
	if (firstname.value == "") {
		alert("Please enter First Name");
		firstname.focus();
		return false;
	}
	if (lastname.value == "") {
		alert("Please enter Last Name");
		lastname.focus();
		return false;
	}
	if (cellphone.value == "") {
		alert("Please enter Cellphone");
		cellphone.focus();
		return false;
	}
	if (email.value == "") {
		alert("Please enter E-Mail Address");
		email.focus();
		return false;
	}
	if (email.value.indexOf('@') == -1) {
		alert("Please enter a valid E-Mail Address. This will be where the user's details will be sent.");
		email.focus();
		return false;
	}
	if (email.value.indexOf('.') == -1) {
		alert("Please enter a valid E-Mail Address. This will be where the user's details will be sent.");
		email.focus();
		return false;
	}
	var cell = document.myform.cellphone.value;
	if (cell.substring(0,1) == "0") {
		if (cell.length == 10) {
		} else {
			alert("Cellphone is invalid. Please verify");
			cellphone.focus();
			return false;
		}
	} else {
		alert("Cellphone is invalid. Please verify");
		cellphone.focus();
		return false;
	}
	document.myform.action = "sms-users-new.php";
	return true;
}

//checkDeleteGroup
function checkDeleteGroup(id, group) {
	var answer = confirm("Are you sure you want to delete the following group?\n\n" + group);
	if (answer) {
		window.location = "sms-groups-rem.php?group_id=" + id;
	} else {}
}

//checkNewGroup
function checkNewGroup() {
	var name = document.myform.name;
	if (name.value == "") {
		alert("Please enter Group Name");
		name.focus();
		return false;
	}
	document.myform.action = "sms-groups-new.php";
	return true;
}

//checkFormat
function checkFormat() {
	var cnt = document.myform.cellphone1.selectedIndex;
	var cell = document.myform.cellphone.value;
	if (cnt == 0) {
		if (cell.substring(0,1) == "0") {
			if (cell.length == 10) {
				document.getElementById("formatter").innerHTML = "<img src='sms-pix/mark-tick.gif' width='16' height='16'/>";
			} else {
				document.getElementById("formatter").innerHTML = "* In LOCAL format - eg. (SA number) 082123456";
			}
		} else {
			document.getElementById("formatter").innerHTML = "* In LOCAL format - eg. (SA number) 082123456";
		}
	} else if (cnt == 1) {
		if (cell.length == 8) {
			document.getElementById("formatter").innerHTML = "<img src='sms-pix/mark-tick.gif' width='16' height='16'/>";
		} else {
			document.getElementById("formatter").innerHTML = "* In INTERNATIONAL format";
		}
	} else if (cnt == 2) {
		if (cell.length == 10) {
			document.getElementById("formatter").innerHTML = "<img src='sms-pix/mark-tick.gif' width='16' height='16'/>";
		} else {
			document.getElementById("formatter").innerHTML = "* In INTERNATIONAL format";
		}
	} else {}
}

//checkFormat
function checkEmailFormat() {
	var email = document.myform.email.value;
	if (email.indexOf('@') != -1) {
		if (email.indexOf('.') != -1) {
			var compare = email.indexOf('.')+2;
			if (email.length > compare) {
				document.getElementById("formatter_email").innerHTML = "<img src='sms-pix/mark-tick.gif' width='16' height='16'/>";
			} else {
				document.getElementById("formatter_email").innerHTML = "* In the format yourname@yourdomain.com";
			}
		} else {
			document.getElementById("formatter_email").innerHTML = "* In the format yourname@yourdomain.com";
		}
	} else {
		document.getElementById("formatter_email").innerHTML = "* In the format yourname@yourdomain.com";
	}
}

//checkAllocateGroup
function checkAllocateGroup(id) {
	var array = id.split("_");
	var group_id = array[0];
	var contact_id = array[1];
	var total = parseInt(document.getElementById("total").value);
	
	var allocated = document.getElementById("allocated").value;
	
	if (total >= 150) {
		alert("You have assigned 150 contacts to this group. This is the maximum allowed per group.\nIf you wish to add more contacts, please create another group");
	} else {
		if (document.getElementById("check_"+id).checked == true) {
			document.getElementById("hid_"+id).value = "Yes";
			allocated = allocated + ","+contact_id+",";
			total = total + 1;
		} else {
			document.getElementById("hid_"+id).value = "";
			allocated = allocated.replace(","+contact_id+",", "");
			total = total - 1;
		}
		document.getElementById("allocated").value = allocated;
	}
}

//checkNewContact
function checkNewContact() {
	var firstname = document.myform.firstname;
	var lastname = document.myform.lastname;
	var c1 = document.myform.cellphone1;
	var cellphone = document.myform.cellphone;
	var comp = document.myform.comp.value;
	
	if (firstname.value == "") {
		alert("Please enter First Name");
		firstname.focus();
		return false;
	}
	if (lastname.value == "") {
		alert("Please enter Last Name");
		lastname.focus();
		return false;
	}
	if (comp == "13") {
	} else if (comp == "26") {
	} else {	
		if (cellphone.value == "") {
			alert("Please enter Cellphone");
			cellphone.focus();
			return false;
		}
		var cell = document.myform.cellphone.value;
		
		if (c1.selectedIndex == 0) {
			//south africa
			if (cell.substring(0,1) == "0") {
				if (cell.length == 10) {
				} else {
					alert("Cellphone is invalid. Please verify");
					cellphone.focus();
					return false;
				}
			} else {
				alert("Cellphone is invalid. Please verify");
				cellphone.focus();
				return false;
			}
		} else if (c1.selectedIndex == 1) {
			//lesotho
			if (cell.length == 8) {
			} else {
				alert("Cellphone is invalid. Please verify");
				cellphone.focus();
				return false;
			}
		} else if (c1.selectedIndex == 2) {
			//uk
			if (cell.length == 10) {
			} else {
				alert("Cellphone is invalid. Please verify");
				cellphone.focus();
				return false;
			}
		} else {}
	}
	document.myform.action = "sms-contacts-new.php";
	return true;
}

//checkDeleteContact
function checkDeleteContact(id, contact) {
	contact = contact.replace("'", "");
	var answer = confirm("Are you sure you want to delete the following contact?\n\n" + contact);
	if (answer) {
		window.location = "sms-contacts-rem.php?conid=" + id;
	} else {}
}

//checkDeleteTemplate
function checkDeleteTemplate(id, msg) {
	var answer = confirm("Are you sure you want to delete the following template?\n\n" + msg);
	if (answer) {
		window.location = "sms-template-rem.php?id=" + id;
	} else {}
}

//show
function show(object) {
	document.getElementById(object).style.visibility = "visible";
	document.getElementById(object).style.display = "block";
}

//hide
function hide(object) {
	document.getElementById(object).style.visibility = "hidden";
	document.getElementById(object).style.display = "none";
}

//checkCharactersApp()
function checkCharactersApp() {
	var message = document.myform.message;
	var total = document.myform.char;
	var char = message.value.length;
	var left = 160 - char;
	
	if (left < 0) {
		document.myform.message.value = message.value.substr(0,160);
		document.myform.char.value = 0;
	} else {
		document.myform.char.value = left;
	}
}

//checkToSel()
function checkToSel() {
	if (document.myform.to.selectedIndex == 1) {
		show("group_table");
		hide("indiv_table");
	} else {
		if (document.myform.to.selectedIndex == 2) {
			show("indiv_table");
			hide("group_table");
		} else {
			hide("group_table");
			hide("indiv_table");
		}
	}
}

//checkSaveTemplate()
function checkSaveTemplate() {
	if (document.getElementById("template_check").checked == true) {
		document.getElementById("template_hid").value = "Yes";
	} else {
		document.getElementById("template_hid").value = "";
	}
}

//checkSendSMS
function checkSendSMS() {
	var to = document.myform.to;
	var message = document.myform.message;
	if (to.selectedIndex == 0) {
		alert("Please select To");
		to.focus();
		return false;
	} else {
		if (to.selectedIndex == 1) {
			if (document.myform.group_id.selectedIndex == 0) {
				alert("Please select Group");
				document.myform.group_id.focus();
				return false;
			} else {}
		} else {
			if (document.myform.con_id.selectedIndex == 0) {
				alert("Please select Contact");
				document.myform.con_id.focus();
				return false;
			} else {}
		}
	}
	if (message.value.indexOf('"') != -1) {
		alert("Please remove the quotation mark/s from Message");
		message.focus();
		return false;
	}
	if (message.value == "") {
		alert("Please enter Message");
		message.focus();
		return false;
	}
	document.myform.action = "sms-send-action.php";
	return true;
}

function checkSchedSMS() {
	var to = document.myform.to;
	var message = document.myform.message;
	if (to.selectedIndex == 0) {
		alert("Please select To");
		$('#dialog').dialog("close");
		to.focus();
		return false;
	} else {
		if (to.selectedIndex == 1) {
			if (document.myform.group_id.selectedIndex == 0) {
				alert("Please select Group");
				$('#dialog').dialog("close");
				document.myform.group_id.focus();
				return false;
			} else {}
		} else {
			if (document.myform.con_id.selectedIndex == 0) {
				alert("Please select Contact");
				$('#dialog').dialog("close");
				document.myform.con_id.focus();
				return false;
			} else {}
		}
	}
	if (message.value.indexOf('"') != -1) {
		alert("Please remove the quotation mark/s from Message");
		$('#dialog').dialog("close");
		message.focus();
		return false;
	}
	if (message.value == "") {
		alert("Please enter Message");
		$('#dialog').dialog("close");
		message.focus();
		return false;
	}
	
	var answer = confirm("Are you sure you want to schedule the sending of this message for the specified date?");
	if (answer) {
		document.myform.action = "sms-send-schedule.php";
		return true;
	} else {
		return false;
	}
}
