// javascripts

function checkNumbers(formField) {
	
	var pattern = /^[(0-9|+)]+$/;
	
	if (!formField.value.match(pattern)) {
	  	alert('Contact can only contain number');
		formField.focus();
	} else {
		if (formField.value.length < 8) {
		  alert('Contact number must be at least 9 digits');
		  formField.focus();
		}
	}
	
}

	function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}




function validateForm(f) {
	var valid = true;
	
	var form = f;
	var errors = '';
	var regErrors = '';
	//alert(form);
	pattern = /^\b/;
	
	// trim all variables
	
	
	
	if (trim(f.name.value) == '') {
		
		errors += '\t- Name is required.\r\n';
		// check if there is characters in 
		f.name.focus();
		valid = false;
	}
	if (trim(f.surname.value) == '') {
		errors += '\t- Surname is required.\r\n';
		f.surname.focus();
		valid = false;
	}
	
	if (f.type.selectedIndex == 0) {
		errors += '\t- Please select a type\r\n';
		valid = false;
	}

	if (f.type.selectedIndex == 1) {
	if (f.serial) {
		if (f.serial.value == '') {
			errors += '\t- Serial number is required.\r\n';
			f.serial.focus();
			valid = false;
		}
	}
	}
	if (trim(f.email.value) == '') {
		errors += '\t- Email address is required.\r\n';
		f.email.focus();
		valid = false;
	}
	if (!f.terms.checked) {
		errors += '\t- You must agree to the terms and conditions.\r\n';
		valid = false;
	}
	if (!f.security_code.value) {
	   errors += '\t- Verification code is required.\r\n';
	   valid = false;
	}
	
	/*if(f.validator.value == '') {
	   errors += '\t- Please enter the captcha code.\r\n';
	   f.validator.focus();
	   valid = false;
	}
	if (f.validator.value) {
	if (f.validator.value != f.captcha_code.value) {
	   errors += '\t- Your code value did not match';
	   valid = false;
	}
	}*/
	
	
	if (trim(f.password.value) == "") {
	   errors += '\t- Password is required\r\n';
	   f.password.focus();
	   valid = false;
	}
	if (trim(f.password.value) != "" && trim(f.password2.value) == "") {
	   errors += '\t- Confirmation password is required\r\n';
	   f.password2.focus();
	   valid = false;
	}
	
	if (f.password.value != "" && f.password2.value != "") {
	   	if (f.password.value != f.password2.value) {
		  	 errors += '\t- Your passwords did not match\r\n';
		      f.password2.focus();
	  		 valid = false;
		}
	}
	if (f.contact.value != "") {
	var pattern = /^[0-9]+$/;
	
		if (!f.contact.value.match(pattern)) {
			errors +='\t- Contact details can only contain number';
			valid = false;
		} else {
		   if (f.contact.length < 9) {
			 errors += '\t- Contact number must contain at least 9 digits';
			 valid = false;
		   }
		}
	}
	
	if (f.type.selectedIndex == 2) {
		
		total = f.haveWii.length;
	  
		var selected = false;
		for (var i = 0; i < total; i++) {
		   
		   if (f.haveWii[i].checked) {
			   selected = true;
		   }
		}
		if (!selected) {
			 errors += '\t- Please select if you own a wii\r\n';
		      f.password2.focus();
	  		 valid = false;
		}
		
	}
	
	if (errors) {
	   finalErrors = 'The following errors have occured:\r\n';
	   finalErrors += errors;
	   alert(finalErrors);
	}
	
	
	
	return valid;
}

  if(window.attachEvent)
    window.attachEvent("onload",setListeners);

  function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "#ffffff";
    }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "#ffffff")
      event.srcElement.style.backgroundColor = "#ffffff";
  }//-->



function loginForm(form) {
	
	valid = true;
	var errors = '';
   
     if (form.usernameLogin.value == "") {
		valid = false;
		form.usernameLogin.focus();
		errors += '- Username is required.\r\n';
	 }
	 if (form.passwordLogin.value == "") {
		valid = false;
		form.passwordLogin.focus();
		errors += '- Password is required.\r\n';
	 }
	 
	 if (errors) {
		alert(errors); 
	 }
   
   
   return valid;
}


function eventValidate(form) {
   
   var valid = true;
   var errors='';
   
   if (!form.title.value) {
	   errors += '- Title is required\r\n';
	   form.title.focus();
	   valid = false;
   }
   
   if (form.typeEvent.selectedIndex == 0) {
	  errors += '- Please select an event type\r\n'; 
	  valid = false;
   }
   
    if (!form.location.value) {
	   errors += '- Location is required\r\n';
	   form.location.focus();
	   valid = false;
   }
    if (!form.street.value) {
	   errors += '- Street address is required\r\n';
	   form.street.focus();
	   valid = false;
   }
   if (!form.city.value) {
	   errors += '- City is required\r\n';
	   form.city.focus();
	   valid = false;
   }
    if (!form.dataArea0.value) {
	   errors += '- Date for event is required\r\n';
	   form.city.focus();
	   valid = false;
   }
   
   if (errors) {
	 alert(errors);   
   }
   return valid;
   
}
function eventValidateEdit(form) {
   
   var valid = true;
   var errors='';
   
   if (!form.title.value) {
	   errors += '- Title is required\r\n';
	   form.title.focus();
	   valid = false;
   }
   
   if (form.typeEvent.selectedIndex == 0) {
	  errors += '- Please select an event type\r\n'; 
	  valid = false;
   }
   
    if (!form.location.value) {
	   errors += '- Location is required\r\n';
	   form.location.focus();
	   valid = false;
   }
    if (!form.street.value) {
	   errors += '- Street address is required\r\n';
	   form.street.focus();
	   valid = false;
   }
   if (!form.city.value) {
	   errors += '- City is required\r\n';
	   form.city.focus();
	   valid = false;
   }
   
   
   if (errors) {
	 alert(errors);   
   }
   return valid;
   
}


function checkInvite(form) {
	
   var valid = true;
   var errors = "";
   
   if (form.nameInvite.value == "") {
	  errors += '- Name is required\r\n';
	  form.nameInvite.focus();
	  valid = false;
   }
   if (form.surnameInvite.value == "") {
	    form.surnameInvite.focus();
	  errors += '- Surname is required\r\n'; 
	  valid = false;
   }
   if (form.emailInvite.value == "") {
	   form.emailInvite.focus();
	  errors += '- Email address is required\r\n';
	  valid = false;
   }
   if (errors) {
	  alert(errors);
   }
   return valid;
}



function checkRSVP() {
	
	var valid = true;
	var answer = '';
	
	total = document.rsvp.rsvpCheck.length;
	
	for (var i = 0; i < total; i++) {
	   
	   if (document.rsvp.rsvpCheck[i].checked) {
		   answer = document.rsvp.rsvpCheck[i].value;
	   }
	}
	
	
	if (!answer) {
		alert("You must select a RSVP option");
		valid = false;
	} else {
		valid = true;
		//send_form('submitRsvp.php', 'rsvpDiv', 'rsvp', 'POST', '', 'getting response' 0, 'rsvpButton');
	 // send_form = function(page, div, form, send_method, do_add, message, proxy, button) {
	}
	alert(valid);
	return valid;
}


function checkFile() {
 
  
   var valid2 = true;
   var errors = '';
   
   if (!document.galleryForm.certify.checked) {
	   errors +='- You must select that you own the right to this image\r\n';
	   valid2 = false;
   }
   if (!document.galleryForm.galleryAdd.value) {
	   errors += '- You must select an image to upload.';
	   valid2 = false;
   }
   if (errors) {
	   alert(errors);
   }
 
   return valid2;
   
}

function checkType(formVal) {
	
  
	if (document.registrationForm.type.selectedIndex == 2) {
	document.getElementById('divOwn').style.display='block';
	document.getElementById('serialWii').style.display='none';
	} else if (document.registrationForm.type.selectedIndex == 1) {
	  document.getElementById('divOwn').style.display='none';	
	  document.getElementById('serialWii').style.display='block';
	}
  
}


function checkVote() {
   
   var valid = true;
   var radio = document.voteForm.vote;
   var answer = '';
   
   for (var i = 0; i < radio.length; i++) {
	  
	  if (document.voteForm.vote[i].checked) {
		answer = document.voteForm.vote[i].value;
	  } 
	  
   }
   
   if (!answer) { 
     alert(' Please select an image to vote.');
	 valid = false;
   }
   
   return valid;
  // if (document.  
}

function openPopup(url,name,width,height) {
  
  var myLeft = (screen.width - width) /2;
  var myTop = (screen.height - height) / 2;
  
  var myWindow = window.open(url,name,'width='+width+',height='+height+',left='+myLeft+',top='+myTop+',scrollbars=no');
  myWindow.focus();
  
}
function openPopup2(url,name,width,height) {
  
  var myLeft = (screen.width - width) /2;
  var myTop = (screen.height - height) / 2;
  
  var myWindow = window.open(url,name,'width='+width+',height='+height+',left='+myLeft+',top='+myTop+',scrollbars=yes');
  myWindow.focus();
  
}


 function validateCo() {
	 
	var valid = true;
	var errMsg = '';
	
	formName = document.inviteCo;
	
	
	if (!formName.coName.value) {
		errMsg += '- Please select a company name.\r\n';
		valid = false;
		formName.coName.focus();
	}
	
	if (!formName.coDesc.value) {
	   errMsg += '- Please supply a company description.\r\n';	
	   formName.coDesc.focus();
	   valid = false;
	}
	if (!formName.number.value) {
	   errMsg += '- Number of people attending is required.\r\n';	
	   formName.number.focus();
	   valid = false;
	}
	if (!formName.contact.value) {
	   errMsg += '- Contact name is required.\r\n';	
	   formName.contact.focus();
	   valid = false;
	}
	if (!formName.contactNum.value) {
	   errMsg += '- Contact number is required.\r\n';	
	   formName.contactNum.focus();
	   valid = false;
	}
	if (!formName.security_code.value) {
	   errMsg += '- Verification code is required.\r\n';	
	   formName.security_code.focus();
	   valid = false;
	}
	 
	if (errMsg) {
	  finalErr  = 'The following erros have occured:\r\n';	
	  finalErr += errMsg;
	  alert(finalErr);	
	}
	
	return valid;
 }


function checkComment() {
  	
	var valid = true;
	var errMsg = '';
	
	f = document.formComment;

	if (!f.title.value) {
		errMsg += '\t- Comment title is required\r\n';
		f.title.focus();
		valid = false;
	}
	if (!f.comment.value) {
		errMsg += '\t- Comment is required\r\n';
		f.comment.focus();
		valid = false;
	}
	if (!f.security_code.value) {
		errMsg += '\t- Verification code is required\r\n';
		f.security_code.focus();
		valid = false;
	}
	
	if (errMsg) {
		errFinal = 'The following errors occured:\r\n' + errMsg;
		alert(errFinal);
	}
	return valid;
}


