//this file is a javascript validation collection.
//please feel free to use this script.
//this is my own & my collegue javascript collection.
//can be free to use for doing project under Vintedge.Pte. Ltd.

function Trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}	
	return retValue;
}

function IsEmpty(v){
	varValue = Trim(v);
	if (varValue.length == 0){ return true; }else{ return false; }
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function checkDate(mm,dd,yyyy){
	leapyear =false;
	validdate=true;
	//alert(mm+dd+yyyy);
	
	if( yyyy%4 == 0 ){
		leapyear=true;
		//alert("Leap Year");
	}
	
	if( mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12 ){
		if(dd > 31){ 
			validdate=false; 
		}
	}
	else if(mm==4 || mm==6 || mm==9 || mm==11){
		if(dd > 30){ 
			validdate=false; 
		}
	}
	else if(mm == 2){
		if(leapyear==true){
			if(dd > 29){ 
				validdate=false; 
			}
		}
		else{
			if(dd > 28){ 
				validdate=false; 
			}
		}
	}
	if(validdate == false){
		alert("Date is not valid.");
		return 0;
		
	}
	else{
		return 1;
	}
	
	
}

function openWindow(url) {
	parameter = "width=610,height=440,toolbar=no,scrollbars=no,resizable=no";
	var v = window.open(url,'_blank',parameter);
}

function reset() {
	document.form1orms[0].reset();
}

function check_email(address) {
	//var emailPat = /^((\w|\.|\_)+)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	var emailPat = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var matchArray = address.match(emailPat);
	if (matchArray == null)
		return false;
	else
		return true;
}

function isDigit() {
if ((event.keyCode < 48)||(event.keyCode > 57 )) {
  if (event.keyCode != 46)
	{event.returnValue=false;}
	}
}

function IsDigit(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		//if (code != 8 && code != 46 && code != 9 && code != 45 && code != 40 && code != 41)
		//{
			return false;
		//}
	}
}

function trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	
	return retValue;
}


function validate_makeover() 
{
if (trim(document.getElementById("name").value) == ""){
	alert("Please key in your name.");
	document.getElementById("name").focus();
	return;
}

if (trim(document.getElementById("nric").value) == ""){
	alert("Please key in your NRIC.");
	document.getElementById("name").focus();
	return;
	}

		if (document.getElementById("nric").value.length != 9){ //9
		alert("Your NRIC format does not seem to be correct. E.g.: SXXXXXXXA");
		document.getElementById("nric").focus();
		return;
		}

		if (document.getElementById("nric").value.charAt(0).toUpperCase() != "S"){ //9 first character must be S
			alert("Your NRIC format does not seem to be correct.Must be prefix with 'S'. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		
		if (isNaN(document.getElementById("nric").value.charAt(8).toUpperCase()) == false){ //9 last character must be aplhabeth
			alert("Your NRIC format does not seem to be correct.Must be suffix with Alphabeth. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		status1 = true;
		for(i=1;i<8;i++){
			if (isNaN(document.getElementById("nric").value.charAt(i).toUpperCase()) == true){ 
				status1=false;
			}
		}
		
		if (status1==false){ 
			alert("Your NRIC format does not seem to be correct. Digit 2-8 must be numeric. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
	

if (trim(document.getElementById("gender").value) == ""){
	alert("Please select your gender.");
	document.getElementById("gender").focus();
	return;
	}
if (trim(document.getElementById("gender").value) == "Male"){
	alert("Miss Samsung Innov8 Photogenic Makeover Contest is open to only female Samsung Innov8 owners.");
	document.getElementById("gender").focus();
	return;
	}
if (trim(document.getElementById("dob").value) == ""){
	alert("Please select your date of birth.");
	document.getElementById("dob").focus();
	return;
	}
if (document.getElementById("mobile").value == ""){
	alert("Please key in your phone number.");
	document.getElementById("mobile").focus();
	return;
}
//if (document.getElementById("mobile").value.length > 0)
//{
//	if (( (document.getElementById("mobile").value.indexOf ('9') != 0) && (document.getElementById("mobile").value.indexOf ('8') != 0)) || (document.getElementById("mobile").value.length < 8)){
//		alert("Your phone number format does not seem to be correct.");
//		document.getElementById("mobile").focus();
//		return;
//		}
//}


	if (IsEmpty(document.getElementById("mobile").value))
		{
			alert("Please enter your mobile number.");
			document.getElementById("mobile").focus();
			return;
		}
	else{
			if ((Left(document.getElementById("mobile").value.toLowerCase(),1)!="9") && (Left(document.getElementById("mobile").value.toLowerCase(),1)!="8") || document.getElementById("mobile").value.length < 8)
			{
				alert("please validate your mobile number, first number must be 9 or 8.");
				document.getElementById("mobile").focus();
				return;
			}
	}


if (document.getElementById("email").value == "" || !check_email(document.getElementById("email").value)){
	alert("Please key in a valid email.");
	document.getElementById("email").focus();
	return;
}
if (trim(document.getElementById("imei").value) == ""){
	alert("Please key in your IMEI number.");
	document.getElementById("imei").focus();
	return;
	}

if (trim(document.getElementById("dop").value) == ""){
	alert("Please select your date of purchase.");
	document.getElementById("dop").focus();
	return;
	}

var hobcheck = false
	for (var i = 1; i <= 10 ; i++ )
	{
		if (document.getElementById("hob"+i).checked == true)
		{
			hobcheck = true;
		}
	}

	if (hobcheck == false)
	{
		alert("Please select at least 1 hobby.");
			return;
	}

if (trim(document.getElementById("aboutyourself").value) == ""){
	alert("Please tell us about yourself.");
	document.getElementById("aboutyourself").focus();
	return;
	}

if (document.getElementById("tnc").checked==false) 
{
alert("You must agree with the terms and conditions in order to proceed.");

document.getElementById("tnc").focus();
return;
}

 document.form1.submit();
  
}


//for reset pw
function validate_reset() 
{
	if (document.getElementById("email").value == "" || !check_email(document.getElementById("email").value)){
		alert("Please key in a valid email.");
		document.getElementById("email").focus();
		return;
	}
	 document.form1.submit();
  
}

//tell a friend
function doit1(){
	/*
	if(document.form1.message.value == ""){
		alert("Please insert your message!");
		document.form1.message.focus();
		return 0;
	}
	*/

//	email cannot be the same as sender
	if(document.form1.e1.value != ""){
		if(document.form1.e1.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e2.value != ""){
		if(document.form1.e2.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e3.value != ""){
		if(document.form1.e3.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e4.value != ""){
		if(document.form1.e4.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e5.value != ""){
		if(document.form1.e5.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}


	if (trim(document.form1.yourname.value) == ""){
		alert("Please key in your name.");
		document.form1.yourname.focus();
		return;
	}
	if (document.form1.youremail.value == "" || !check_email(document.form1.youremail.value)){
		alert("Please key in your valid email.");
		document.form1.youremail.focus();
	return;
	}
	
	if (trim(document.form1.f1.value) == ""){
		alert("Please key in your friend #1 name.");
		document.form1.f1.focus();
		return;
	}
	if (document.form1.e1.value == "" || !check_email(document.form1.e1.value)){
		alert("Please key in your friend #1 valid email.");
		document.form1.e1.focus();
	return;
	}

	//check for email validation friend num 2-5
	if(document.form1.e2.value != ""){
		if (!check_email(document.form1.e2.value)){
			alert("Please key in your friend #2 valid email.");
			document.form1.e2.focus();
		return;
		}
	}
	if(document.form1.e3.value != ""){
		if (!check_email(document.form1.e3.value)){
			alert("Please key in your friend #3 valid email.");
			document.form1.e3.focus();
		return;
		}
	}
	if(document.form1.e4.value != ""){
		if (!check_email(document.form1.e4.value)){
			alert("Please key in your friend #4 valid email.");
			document.form1.e4.focus();
		return;
		}
	}

	if(document.form1.e5.value != ""){
		if (!check_email(document.form1.e5.value)){
			alert("Please key in your friend #5 valid email.");
			document.form1.e5.focus();
		return;
		}
	}
	
	//check for name and email must be fill in together 2-5. eg. you must fill the name if you keyin the email. and also otherwise
	if( (document.form1.f2.value == "" && document.form1.e2.value != "") || (document.form1.f2.value != "" && document.form1.e2.value == "") ){ 
		alert("Name and Email friend #2 is not complete"); 
		document.form1.f2.focus();
		return;
	}
	if( (document.form1.f3.value == "" && document.form1.e3.value != "") || (document.form1.f3.value != "" && document.form1.e3.value == "") ){ 
		alert("Name and Email friend #3 is not complete"); 
		document.form1.f3.focus();
		return;
	}
	if( (document.form1.f4.value == "" && document.form1.e4.value != "") || (document.form1.f4.value != "" && document.form1.e4.value == "") ){ 
		alert("Name and Email friend #4 is not complete"); 
		document.form1.f4.focus();
		return;
	}
	if( (document.form1.f5.value == "" && document.form1.e5.value != "") || (document.form1.f5.value != "" && document.form1.e5.value == "") ){ 
		alert("Name and Email friend #5 is not complete"); 
		document.form1.f5.focus();
		return;
	}
	
	//check friend email cannot be the same. eg. if email not blank... email1 != email2 email2 != email3 etc......
	if( (document.form1.e1.value == document.form1.e2.value) || (document.form1.e1.value == document.form1.e3.value) || (document.form1.e1.value == document.form1.e4.value) || (document.form1.e1.value == document.form1.e5.value)){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	
	if(document.form1.e2.value != ""){
		
		if((document.form1.e2.value == document.form1.e3.value) || (document.form1.e2.value == document.form1.e4.value) || (document.form1.e2.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	
	if(document.form1.e3.value != ""){
		if( (document.form1.e3.value == document.form1.e4.value) || (document.form1.e3.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e4.value != ""){
		if( (document.form1.e4.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	
	//check if there is a age restriction.
	
//send the form	
document.form1.submit();
}


function validate_Login()
{

	if (IsEmpty(document.getElementById('nric').value))
	{
		alert("Please enter your user login.");
		document.getElementById('nric').focus();
		return;
	}
	if (IsEmpty(document.getElementById('password').value))
		{
			alert("Please enter your password.");
			document.getElementById("password").focus();
			return;
		}

	document.form1.submit();
}

function validate_nav_login()
{

	if (IsEmpty(document.getElementById('topnavnric').value))
	{
		alert("Please enter your user login.");
		document.getElementById('topnavnric').focus();
		return;
	}
	if (IsEmpty(document.getElementById('toppassword').value))
		{
			alert("Please enter your password.");
			document.getElementById("toppassword").focus();
			return;
		}

	document.formnav.submit();
}


function submit_upload()
{

var status1=true


if (document.getElementById("photo").value == "" )
{
	 alert("Please upload your picture.");
	 return false;
	 
}

if ((document.form1.photo.value) &&  (Right(document.form1.photo.value.toLowerCase(),3)!="jpg") && (Right(document.form1.photo.value.toLowerCase(),4)!="jpeg"))
{
	alert("Only JPG / JPEG files are allowed.");
	document.form1.photo.focus();
	return;
}

if (document.form1.description.value == ""){
	alert("Please key in the description for your submission.");
	document.form1.mobile.focus();
	return;
}

if (document.form1.submitted.value == ""){
	alert("Please key in the submitted by for your submission.");
	document.form1.mobile.focus();
	return;
}


if (document.form1.imei.value == ""){
	alert("Please key in the IMEI for your submission.");
	document.form1.imei.focus();
	return;
}


document.form1.submit();
}

function register_vote()
{
if (trim(document.form1.name.value) == ""){
	alert("Please key in your name.");
	document.form1.name.focus();
	return;
}

if (trim(document.form1.nric.value) == ""){
	alert("Please key in your NRIC.");
	document.form1.nric.focus();
	return;
	}

		if (document.getElementById("nric").value.length != 9){ //9
		alert("Your NRIC format does not seem to be correct. E.g.: SXXXXXXXA");
		document.getElementById("nric").focus();
		return;
		}

		if (document.getElementById("nric").value.charAt(0).toUpperCase() != "S"){ //9 first character must be S
			alert("Your NRIC format does not seem to be correct.Must be prefix with 'S'. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		
		if (isNaN(document.getElementById("nric").value.charAt(8).toUpperCase()) == false){ //9 last character must be aplhabeth
			alert("Your NRIC format does not seem to be correct.Must be suffix with Alphabeth. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		status1 = true;
		for(i=1;i<8;i++){
			if (isNaN(document.getElementById("nric").value.charAt(i).toUpperCase()) == true){ 
				status1=false;
			}
		}
		
		if (status1==false){ 
			alert("Your NRIC format does not seem to be correct. Digit 2-8 must be numeric. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
	

if (trim(document.form1.gender.value) == ""){
	alert("Please select your gender.");
	document.form1.gender.focus();
	return;
	}
if (trim(document.form1.dob.value) == ""){
	alert("Please select your date of birth.");
	document.form1.dob.focus();
	return;
	}
	if (IsEmpty(document.getElementById("mobile").value))
		{
			alert("Please enter your mobile number.");
			document.getElementById("mobile").focus();
			return;
		}
	else{
			if ((Left(document.getElementById("mobile").value.toLowerCase(),1)!="9") && (Left(document.getElementById("mobile").value.toLowerCase(),1)!="8") || document.getElementById("mobile").value.length < 8)
			{
				alert("Please validate your mobile number, first number must be 9 or 8.");
				document.getElementById("mobile").focus();
				return;
			}
	}
if (document.form1.email.value == "" || !check_email(document.form1.email.value)){
	alert("Please key in a valid email.");
	document.form1.email.focus();
	return;
}
if (trim(document.form1.password.value) == ""){
	alert("Please key in your password.");
	document.form1.password.focus();
	return;
	}
	
	if (trim(document.form1.password2.value) == ""){
	alert("Please re-enter your password.");
	document.form1.reenterpassword.focus();
	return;
	}
	
	if ((trim(document.form1.password.value))!=(trim(document.form1.password2.value))){
	alert("Your password does not match. Please re-enter your password");
	document.form1.renterpassword.focus();
	
	return;
	}




if (document.form1.tnc.checked==false) 
{
alert("You must agree with the terms and conditions in order to proceed.");

document.form1.focus();
return;
}

 document.form1.submit();
 
}

function register_login()
{

if (trim(document.getElementById("name").value) == ""){
	alert("Please key in your name.");
	document.getElementById("name").focus();
	return;
}

if (trim(document.getElementById("nric").value) == ""){
	alert("Please key in your NRIC.");
	document.getElementById("nric").focus();
	return;
	}

		if (document.getElementById("nric").value.length != 9){ //9
		alert("Your NRIC format does not seem to be correct. E.g.: SXXXXXXXA");
		document.getElementById("nric").focus();
		return;
		}

		if (document.getElementById("nric").value.charAt(0).toUpperCase() != "S"){ //9 first character must be S
			alert("Your NRIC format does not seem to be correct.Must be prefix with 'S'. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		
		if (isNaN(document.getElementById("nric").value.charAt(8).toUpperCase()) == false){ //9 last character must be aplhabeth
			alert("Your NRIC format does not seem to be correct.Must be suffix with Alphabeth. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}
		status1 = true;
		for(i=1;i<8;i++){
			if (isNaN(document.getElementById("nric").value.charAt(i).toUpperCase()) == true){ 
				status1=false;
			}
		}
		
		if (status1==false){ 
			alert("Your NRIC format does not seem to be correct. Digit 2-8 must be numeric. E.g.: S1234567A");
			document.getElementById("nric").focus();
			return;
		}

if (trim(document.getElementById("gender").value) == ""){
	alert("Please select your gender.");
	document.getElementById("gender").focus();
	return;
	}
if (trim(document.getElementById("dob").value) == ""){
	alert("Please select your date of birth.");
	document.getElementById("dob").focus();
	return;
	}
if (document.getElementById("mobile").value == ""){
	alert("Please key in your phone number.");
	document.getElementById("mobile").focus();
	return;
}
	if (IsEmpty(document.getElementById("mobile").value))
		{
			alert("Please enter your mobile number.");
			document.getElementById("mobile").focus();
			return;
		}
	else{
			if ((Left(document.getElementById("mobile").value.toLowerCase(),1)!="9") && (Left(document.getElementById("mobile").value.toLowerCase(),1)!="8") || document.getElementById("mobile").value.length < 8)
			{
				alert("Please validate your mobile number, first number must be 9 or 8.");
				document.getElementById("mobile").focus();
				return;
			}
	}
if (document.getElementById("email").value == "" || !check_email(document.getElementById("email").value)){
	alert("Please key in a valid email.");
	document.getElementById("email").focus();
	return;
}
if (trim(document.getElementById("password").value) == ""){
	alert("Please key in your password.");
	document.getElementById("password").focus();
	return;
	}
	
	if (trim(document.getElementById("password2").value) == ""){
	alert("Please re-enter your password.");
	document.getElementById("password2").focus();
	return;
	}
	
	if ((trim(document.getElementById("password").value))!=(trim(document.getElementById("password2").value))){
	alert("Your password does not match. Please re-enter your password");
	document.getElementById("password2").focus();
	
	return;
	}

if (document.getElementById("tnc").checked==false) 
{
alert("You must agree with the terms and conditions in order to proceed.");

document.getElementById("tnc").focus();
return;
}

 document.form1.submit();
 
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
