function get_random()
{
    var ranNum= Math.floor(Math.random()*3);
    return ranNum;
}

function openWindow(theURL, Name, popW, popH, scroll, resize) {
    var winleft = (screen.width - popW) / 2;
    var winUp = (screen.height - popH) / 2;
    winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable='+resize+'';
    Win = window.open(theURL, Name, winProp);
    Win.window.focus();
}

function changeUrl() {
	var redirect;
	redirect = document.getElementById('jumpUrl').value;
	if (redirect != '') document.location.href = redirect;
}

function toggleDateDisplay() {
	var mdiv=document.getElementById('complaint-date');
	var ndiv=document.getElementById('complaint-time');
	var cdiv=document.getElementById('complaint-type');
	if(cdiv.options[cdiv.selectedIndex].value=='complaint')
	{
		mdiv.style.display='block';
		ndiv.style.display='block';
		document.complaint.c_hour.value = ""
		document.complaint.c_minute.value = ""
	}
	else
	{
		document.complaint.c_hour.value = "--"
		document.complaint.c_minute.value = "--"
		mdiv.style.display='none';
		ndiv.style.display='none';
	} 
}

//Login Form Validation
function validateLogin()
{
	form = document.login	

	username = form.username.value
	if(username.length <= 0)
	{
		alert("Please Enter Username!")
		return false
	}
	else
	{
		password = form.password.value
		if(password.length <= 0)
		{
			alert("Please Enter Password!")
			return false
		}
	}
}

//Add News Validation
function validateAddNews()
{
	form = document.news

	header = form.newsheader.value
	if(header.length <= 0)
	{
		alert("Please Enter News Title!")
		return false
	}
	else
	{
		contents = form.news_content.value
		if(contents.length <= 0 || contents == "Enter News Content")
		{
			alert("Please Enter News Content/Description!")
			return false
		}
	}
}

//Add Service Validation
function validateAddService()
{
	form = document.service

	name_en = form.service_name_en.value
	name_cn = form.service_name_cn.value
	
	if(name_en.length <= 0 && name_cn.length <= 0)
	{
		alert("Please Enter At Least an English or Chinese Name!")
		return false
	}
}

//Add FAQ Validation
function validateAddFAQ()
{
	form = document.faq

	question = form.faq_question.value
	answer = form.faq_answer.value
	
	if(question.length <= 0 || answer.length <= 0 || question == "Question" || answer == "Answer")
	{
		alert("Please Enter both the Question and Answer!")
		return false
	}
}

//Add vacancy Validation
function validateAddVacancy()
{
	form = document.vac

	title = form.vac_title.value
	desc = form.vac_desc.value
	
	if(title.length <= 0 || desc.length <= 0 || desc == "Job Description")
	{
		alert("Please Enter both the Vacancy Title and Job Description!")
		return false
	}
}

function validateComplaint()
{
	form = document.complaint

	name = form.complaint_name.value
	contact_email = form.complaint_email.value
	contact_hp = form.complaint_hp.value
	contents = form.complaint_content.value
	day = form.complaint_date_dd.value
	month = form.complaint_date_mm.value
	year = form.complaint_date_yyyy.value
	hour_c = form.c_hour.value
	minute_c = form.c_minute.value
	no1 = form.n1.value
	no2 = form.n2.value
	ttl = form.t.value
	a = form.ans.value
	
	if(name.length <= 0)
	{
		alert("Please Enter Your Name!")
		return false
	}
	else
	{
		if(contact_email.length <= 0 && contact_hp.length <= 0)
		{
			alert("Please Enter Either an Email or a Contact Number")
			return false;
		}
		else
		{
			if(contents <= 0 || contents == "Your Comments Here")
			{
				alert("Please Enter Your Comment/Complaints")
				return false;
			}
			else
			{
				var d = new Date()
				today_year = d.getFullYear();
				if(year > today_year)
				{
					alert("Please Check the Year");
					return false;
				}
				else
				{
					if(month == "Feb" || month == "Apr" || month == "Jun" || month == "Sept" || month == "Nov")
					{
						if(day == "31")
						{
							alert("Please Enter Correct Date")
							return false;
						}
					}
					else
					{
						if(hour_c != "--" && minute_c != "--")
						{
							if(hour_c.length <= 0 || minute_c.length <= 0)
							{
								alert("Please Enter the Complaint Incident Time")
								return false;
							}
							else
							{
								if(hour_c > "23" || hour_c < "00" || minute_c > "60" || minute_c < "00")
								{
									alert("Please Enter the Corrent Incident Time")
									return false
								}
							}
						}
					}
				}
			}
		}
	}
		
	myBranch = -1;
	for (i=form.branch.length-1; i > -1; i--) 
	{
		if (form.branch[i].checked) 
		{
			myBranch = i; i = -1;
		}
	}
	if (myBranch == -1) 
	{
		alert("Please Select a Branch");
		return false;
	}
	
	if(a.length <= 0)
	{
		alert("Please Enter the Check Sum")
		return false
	}
	else {
		if(a != ttl) {
			alert("Please Enter a Correct Check Sum")
			return false
		}
	}
}

function validateUploadPhoto()
{
	form = document.photo

	title = form.caption.value
	
	if(title.length <= 0)
	{
		alert("Please Enter the Photo Caption")
		return false
	}
}

function validateVacancies()
{
	form = document.vacancy_app
	
	name = form.name.value
	age = form.age.value
	ic_pp = form.ic_passport.value
	add = form.add1.value
	postcode = form.postcode.value
	city = form.city.value
	email = form.email.value
	hp = form.hp.value
	descrip = form.description.value
	no1 = form.n1.value
	no2 = form.n2.value
	ttl = form.t.value
	a = form.ans.value
	
	if(name.length <= 0)
	{
		alert("Please Enter Your Name!")
		return false
	}
	else
	{
		if(age.length <= 0)
		{
			alert("Please Enter Your Age")
			return false
		}
		else
		{
			if(ic_pp.length <= 0)
			{
				alert("Please Enter Your IC/Passport No")
				return false
			}
			else
			{
				if(add.length <= 0)
				{
					alert("Please Enter Your Address")
					return false
				}
				else
				{
					if(postcode.length <= 0)
					{
						alert("Please Enter Your Address' Postcode")
						return false
					}
					else
					{
						if(city.length <= 0)
						{
							alert("Please Enter The City That You Live at")
							return false
						}
						else
						{
							if(email.length <= 0 && hp.length <= 0)
							{
								alert("Please Enter at least a Contact")
								return false
							}
							else
							{
								if(descrip.length <= 0)
								{
									alert("Please Brief Us About Yourself")
									return false
								}
							}
						}
					}
				}
			}
		}
	}
	
	myBranch = -1;
	for (i=form.branch.length-1; i > -1; i--) 
	{
		if (form.branch[i].checked) 
		{
			myBranch = i; i = -1;
		}
	}
	if (myBranch == -1) 
	{
		alert("Please Select a Branch");
		return false;
	}
	
	if(a.length <= 0)
	{
		alert("Please Enter the Check Sum")
		return false
	}
	else {
		if(a != ttl) {
			alert("Please Enter a Correct Check Sum")
			return false
		}
	}
}


// Validate Request
function validateRequest()
{
	form = document.request

	name = form.request_name.value
	contact_email = form.request_email.value
	contact_hp = form.request_hp.value
	contents = form.request_game.value
	no1 = form.n1.value
	no2 = form.n2.value
	ttl = form.t.value
	a = form.ans.value
	
	if(name.length <= 0)
	{
		alert("Please Enter Your Name!")
		return false
	}
	else
	{
		if(contact_email.length <= 0 && contact_hp.length <= 0)
		{
			alert("Please Enter Either an Email or a Contact Number")
			return false;
		}
		else
		{
			if(contents <= 0)
			{
				alert("Please Enter The Game Name")
				return false;
			}
		}
	}
		
	myBranch = -1;
	for (i=form.branch.length-1; i > -1; i--) 
	{
		if (form.branch[i].checked) 
		{
			myBranch = i; i = -1;
		}
	}
	if (myBranch == -1) 
	{
		alert("Please Select a Branch");
		return false;
	}
	
	if(a.length <= 0)
	{
		alert("Please Enter the Check Sum")
		return false
	}
	else {
		if(a != ttl) {
			alert("Please Enter a Correct Check Sum")
			return false
		}
	}
}

////////////////////////// Trial and Error //////////////////////////////////
function changeBox(cbox) {
	cbox.checked=true
}

function checkCheckBox(cbox) {
  if (cbox.checked == false) {
    alert('Please check the box to continue.');
    return false;
  } else
    return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
function show_row(){ 
    // Make sure the element exists before calling it's properties 
    if ((document.getElementById("complaint-date") != null)&&(document.getElementById("complaint-time") != null)) 
      // Toggle visibility between none and inline 
      if ((document.getElementById("complaint-date").style.display == 'none')&&(document.getElementById("complaint-time").style.display == 'none')) 
      { 
        document.getElementById("complaint-date").style.display = 'block'; 
        document.getElementById("complaint-time").style.display = 'block'; 
          document.getElementById("complaint_type").value == 'complaint' ; 
      } else { 
        document.getElementById("complaint-date").style.display = 'none'; 
        document.getElementById("complaint-time").style.display = 'none'; 
          document.getElementById("complaint_type").value == 'suggestion'; 
      } 
  } 
function check_form() {
	if (document.getElementById('name').value == "") {
		alert("Please enter your name here!")
		document.getElementById('name').focus()
		return false
	}
	
	if (document.getElementById('email').value == "" && document.getElementById('phone').value == "" ) {
		alert("Please enter Email Address or Phone Number!")
		return false
	}
	if (document.getElementById('email').value != "" && !valid_email(document.getElementById('email').value)) {
		document.getElementById('email').focus()
		document.getElementById('email').select()
		return false
	}
	
	
	if (document.getElementById('complaint-type').value == "complaint"){
		
		if (document.getElementById('c_hour').value == "" || document.getElementById('c_hour').value >= 24 ) {
			alert("Please enter valid time in 24hour format!")
			document.getElementById('c_hour').focus()
			return false
		}
		else if (document.getElementById('c_minute').value == "" || document.getElementById('c_minute').value >= 60 ) {
			alert("Please enter valid time in 24hour format")
			document.getElementById('c_minute').focus()
			return false
		}
	
	}
	
	if (document.getElementById('complaint_content').value == "" || document.getElementById('complaint_content').value == "Your Comments Here" ) {
		alert("Please enter your comment here!")
		document.getElementById('complaint_content').focus()
		return false
	}
	
	if (document.getElementById('code').value == "") {
		alert("Please enter verification word in the image here!")
		document.getElementById('code').focus()
		return false
	}
	return true
}

//check numeric number

function check_phone(id) {
	var control = document.getElementById(id);
	var numeric = /[0-9-+]/g;
	var specialString = control.value.replace(numeric, "");
	
	if (specialString.length > 0) {
		control.value = control.value.substring(0, control.value.length - 1);
	}
	return true;
}

function check_time(id) {
	var control = document.getElementById(id);
	var numeric = /[0-9]/g;
	var specialString = control.value.replace(numeric, "");
	
	if (specialString.length > 0) {
		control.value = control.value.substring(0, control.value.length - 1);
	}
	return true;
}

// validate E-mail address
function valid_email(emailStr) {
	/* The JavaScript Source!! http://javascript.internet.com */
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			return false;
		}
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
		// user is not valid
		
		alert("The username doesn't seem to be valid.");
		return false;
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	alert("The domain name does not seem to be valid.");
	return false;
	   }
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}


//////////////////////////////////////////////////// VACANCIES ////////////////////////////////////////
function check_form_vacancies() {
	if (document.getElementById('name').value == "") {
		alert("Please enter your name here!")
		document.getElementById('name').focus()
		return false
	}
	if (document.getElementById('age').value == "") {
		alert("Please enter your age here!")
		document.getElementById('age').focus()
		return false
	}
	if (document.getElementById('ic_passport').value == "") {
		alert("Please enter your IC or passport number here!")
		document.getElementById('ic_passport').focus()
		return false
	}
	if (document.getElementById('add1').value == "") {
		alert("Please enter your address here!")
		document.getElementById('add1').focus()
		return false
	}
	if (document.getElementById('postcode').value == "") {
		alert("Please enter your Postcode here!")
		document.getElementById('postcode').focus()
		return false
	}
	if (document.getElementById('city').value == "") {
		alert("Please enter your city here!")
		document.getElementById('city').focus()
		return false
	}
	
	if (document.getElementById('email').value == "" && document.getElementById('hp').value == "" ) {
		alert("Please enter Email Address or Phone Number!")
		return false
	}
	if (document.getElementById('email').value != "" && !valid_email(document.getElementById('email').value)) {
		document.getElementById('email').focus()
		document.getElementById('email').select()
		return false
	}
	if (document.getElementById('description').value == "") {
		alert("Please enter your description here!")
		document.getElementById('description').focus()
		return false
	}
	
	if (document.getElementById('code').value == "") {
		alert("Please enter verification word in the image here!")
		document.getElementById('code').focus()
		return false
	}
	return true
}

//check numeric number

function check_phone(id) {
	var control = document.getElementById(id);
	var numeric = /[0-9-+]/g;
	var specialString = control.value.replace(numeric, "");
	
	if (specialString.length > 0) {
		control.value = control.value.substring(0, control.value.length - 1);
	}
	return true;
}

function check_age(id) {
	var control = document.getElementById(id);
	var numeric = /[0-9]/g;
	var specialString = control.value.replace(numeric, "");
	
	if (specialString.length > 0) {
		control.value = control.value.substring(0, control.value.length - 1);
	}
	return true;
}
function check_postcode(id) {
	var control = document.getElementById(id);
	var numeric = /[0-9]/g;
	var specialString = control.value.replace(numeric, "");
	
	if (specialString.length > 0) {
		control.value = control.value.substring(0, control.value.length - 1);
	}
	return true;
}


// validate E-mail address
function valid_email(emailStr) {
	/* The JavaScript Source!! http://javascript.internet.com */
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com

	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			return false;
		}
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
		// user is not valid
		
		alert("The username doesn't seem to be valid.");
		return false;
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	alert("The domain name does not seem to be valid.");
	return false;
	   }
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
