// ## contact_form.js
// Contains form checking for contact

// FUNCTION LIST:
// 	isempty();
//  val();
// 	validvehicle();
//  controlparticular() for change_add.html;
//  control() for change_owner.html;
//  controlfeedback() for feedback_sales.html; feedback_parts.html; feedback_general.html
//  controlfeedbacksvc() for feedback_service.html;

//check whether the given value (thevalue) of specific field (fieldname) is empty
//if it is empty ; alert and the return value=1
function isempty(fieldname,thevalue){
	if (thevalue=="") {
		alert ("Please enter your "+fieldname);
		return 1;
	}
}


//check whether the input is number
function val(field) {
var valid = "0123456789./,;:-_&"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
	alert("Invalid entry! numbers only!");
	field.focus();
	field.select();
}
}//End Function	


//check whether the input is alphanumeric
function valalpha(field) {
var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -/"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
	alert("Invalid entry! Please enter alphanumeric only!");
	field.focus();
	field.select();
}
}//End Function	


//validation of vehicle number
function validvehicle(thenumber){
   var firstchr = thenumber.charAt(0);
   var lastchr = thenumber.charAt(thenumber.length-1);

	//check the length of string must be bigger than 1
	if (thenumber.length==1) {
		alert("Your Vehicle Registration No. is not submitted");
		return false;
	}	
   //check First and Last character must not be a number   
   if (isNaN(firstchr)==true && isNaN(lastchr)==true) 
   		return true;
   else {
		alert("Your Vehicle Registration No. is not submitted");   		
   		return false;
	}
}//end function


//update particular javascript checking
function controlparticular(theform){
    var thelength,tval,j,k,i;
    var flag=0;
    thelength=theform.length;	
/*    if(isempty("Vehicle Registration No.",theform.vehicleno.value)==1 || validvehicle(theform.vehicleno.value)==false) { 
		flag=2; theform.vehicleno.focus(); return;
   	} 
	if(isempty("name",theform.name.value)==1){
		flag=1;	theform.name.focus(); return;
	}
   if(theform.idtype.value=="NRIC"&&isempty("NRIC",theform.nric.value)==1){ 
   		flag=1; theform.nric.focus(); return;
	}
*/
   if(isempty("Address",theform.address.value)==1){ 
   		flag=1; theform.address.focus(); return;
	}
   if(isempty("Postal Code",theform.postal.value)==1){ 
   		flag=1; theform.postal.focus(); return;
   }    
   if((theform.home.value=="") && (theform.office.value=="") && (theform.hp.value=="")) { 
		flag=1; 
		alert("Please fill in at least one contact no."); 
		theform.home.focus(); 
		return;
   }    
   if(theform.hp.value.length != ""){
   		if(theform.hp.value.length != 8){
		flag=1; 
		alert("Please check your HP no."); 
		theform.hp.focus(); 
		return;
		}
   }    
   if(theform.home.value.length != ""){
	    if(theform.home.value.length != 8){
		flag=1; 
		alert("Please check your home contact no."); 
		theform.home.focus(); 
		return;
	    }    
   }    
   if(theform.office.value.length != ""){
   		if(theform.office.value.length != 8){
		flag=1; 
		alert("Please check your office contact no."); 
		theform.office.focus(); 
		return;
   		}    
   }    
   if(theform.email.value==""){ 
    	flag=1; 
		alert("Please fill in your E-mail Address"); 
		theform.email.focus(); 
		return;
   }        
   else {
		if((theform.email.value.indexOf('@') == -1) || (theform.email.value.indexOf('.') == -1)) {
			flag=1; 
			alert("Check Your E-mail Address"); 
			theform.email.focus(); 
			return;
		}
	}
   
   if (flag==0) { 
   		theform.submit(); 
  		return 1; 
	} 
}//end function

//change ownership with chassis mandatory field
function controlchassis(theform){
   var thelength,tval,j,k,i;
   var flag=0;
   thelength=theform.length;
   for(i=0;i<thelength;i++){
		if(i==0) { 
		   if(theform.vehicleno.value=="") { 
		   		flag=1; alert("Please enter your Vehicle Registration No."); theform.vehicleno.focus(); break; 
   			}
		   if (validvehicle(theform.vehicleno.value)==false) {
   				flag=1; theform.vehicleno.focus(); return;
		   }
   		}

		if(i==1) { 
   			if((theform.chassis.value=="")) { 
				flag=1; alert("Please enter your Chassis No"); 							theform.chassis.focus(); break; 
   			}
   		}	
		if(i==2) { 
   			if((theform.name.value=="")) { 
				flag=1; alert("Please enter your name"); theform.name.focus(); break; 
   			}
   		}  
		if(i==3) {
   			if((theform.nricno.value=="")) { 
				flag=1; alert("Please enter your NRIC no."); theform.nricno.focus(); break; 
   			}
   		}  
		if(i==4) { 
		   if((theform.address.value=="")) { 
		   		flag=1; alert("Please enter your address"); theform.address.focus(); break; 
   			}
   		}
		if(i==5) {	
    		if((theform.postal.value=="")){ 
				flag=1; alert("Please enter your Postal Code"); theform.postal.focus(); break; 
    		} 
    	} 
		if((i==6) || (i==7) || (i==8)) { 
   			if((theform.home.value=="") && (theform.office.value=="") && (theform.hp.value=="")) { 
				flag=1; alert("Please fill in at least one contact no."); theform.elements[i+1].focus(); break; 
   			}
			else { if(theform.hp.value.length != ""){
   				if(theform.hp.value.length != 8){
				flag=1; 
				alert("Please check your HP no."); 
				theform.hp.focus(); 
				return;
				}
   			}    
   			 if(theform.home.value.length != ""){
	    		if(theform.home.value.length != 8){
				flag=1; 
				alert("Please check your home contact no."); 
				theform.home.focus(); 
				return;
	    		}    
   			}    
   			 if(theform.office.value.length != ""){
   				if(theform.office.value.length != 8){
				flag=1; 
				alert("Please check your office contact no."); 
				theform.office.focus(); 
				return;
   				}    
   			}  
			}  
   		}  
		if(i==9) { 
   			if((theform.email.value=="")) { 
				flag=1; alert("Please enter your E-mail Address"); theform.email.focus(); break; 
   			}
   		}        
		if(i==9) {
			if((theform.email.value.indexOf('@') == -1) || (theform.email.value.indexOf('.') == -1)) {
				flag=1; alert("Check Your E-mail Address"); theform.email.focus(); break;
			}
		}
	} //end for	
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
} // end function

//change ownership javascript
   function control(theform){
   var thelength,tval,j,k,i;
   var flag=0;
   thelength=theform.length;
   for(i=0;i<thelength;i++){
		if(i==0) { 
		   if(theform.vehicleno.value=="") { 
		   		flag=1; alert("Please enter your Vehicle Registration No."); theform.vehicleno.focus(); break; 
   			}
		   if (validvehicle(theform.vehicleno.value)==false) {
   				flag=1; theform.vehicleno.focus(); return;
		   }
   		}	
		if(i==1) { 
   			if((theform.name.value=="")) { 
				flag=1; alert("Please enter your name"); theform.name.focus(); break; 
   			}
   		}  
		if(i==2) {
   			if((theform.nricno.value=="")) { 
				flag=1; alert("Please enter your NRIC no."); theform.nricno.focus(); break; 
   			}
   		}  
		if(i==3) { 
		   if((theform.address.value=="")) { 
		   		flag=1; alert("Please enter your address"); theform.address.focus(); break; 
   			}
   		}
		if(i==4) {	
    		if((theform.postal.value=="")){ 
				flag=1; alert("Please enter your Postal Code"); theform.postal.focus(); break; 
    		} 
    	} 
		if((i==5) || (i==6) || (i==7)) { 
   			if((theform.home.value=="") && (theform.office.value=="") && (theform.hp.value=="")) { 
				flag=1; alert("Please fill in at least one contact no."); theform.elements[i+1].focus(); break; 
   			}
			else { if(theform.hp.value.length != ""){
   				if(theform.hp.value.length != 8){
				flag=1; 
				alert("Please check your HP no."); 
				theform.hp.focus(); 
				return;
				}
   			}    
   			 if(theform.home.value.length != ""){
	    		if(theform.home.value.length != 8){
				flag=1; 
				alert("Please check your home contact no."); 
				theform.home.focus(); 
				return;
	    		}    
   			}    
   			 if(theform.office.value.length != ""){
   				if(theform.office.value.length != 8){
				flag=1; 
				alert("Please check your office contact no."); 
				theform.office.focus(); 
				return;
   				}    
   			}  
			}  
   		}  
		if(i==8) { 
   			if((theform.email.value=="")) { 
				flag=1; alert("Please enter your E-mail Address"); theform.email.focus(); break; 
   			}
   		}        
		if(i==8) {
			if((theform.email.value.indexOf('@') == -1) || (theform.email.value.indexOf('.') == -1)) {
				flag=1; alert("Check Your E-mail Address"); theform.email.focus(); break;
			}
		}
	} //end for	
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
}


































//control the feedback (sales, general, parts)
function controlfeedback(theform) {
   var flag=0;
	if(isempty("name",theform.name.value)==1){
		flag=1;	theform.name.focus(); return;
	}
    if(theform.vehicleno.value!=""){
		if(validvehicle(theform.vehicleno.value)==false) { 
		flag=2; theform.vehicleno.focus(); return;
		}
   	} 
   if(theform.email.value==""){ 
    	flag=1; 
		alert("Please enter your E-mail Address"); 
		theform.email.focus(); 
		return;
   }        
   else {
		if((theform.email.value.indexOf('@') == -1) || (theform.email.value.indexOf('.') == -1)) {
			flag=1; 
			alert("Check Your E-mail Address"); 
			theform.email.focus(); 
			return;
		}
	}
    if((theform.home.value=="") && (theform.office.value=="") && (theform.hp.value=="")) { 
		flag=1; 
		alert("Please fill in at least one contact no."); 
		theform.home.focus(); 
		return;
   }    
   
   if(theform.hp.value.length != ""){
   		if(theform.hp.value.length != 8){
		flag=1; 
		alert("Please check your HP no."); 
		theform.hp.focus(); 
		return;
		}
   }    
   if(theform.home.value.length != ""){
	    if(theform.home.value.length != 8){
		flag=1; 
		alert("Please check your home contact no."); 
		theform.home.focus(); 
		return;
	    }    
   }    
   if(theform.office.value.length != ""){
   		if(theform.office.value.length != 8){
		flag=1; 
		alert("Please check your office contact no."); 
		theform.office.focus(); 
		return;
   		}    
   }    
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
}




//control the feedback (service)
function controlfeedbacksvc(theform) {
   var flag=0;
	if(isempty("name",theform.name.value)==1){
		flag=1;	theform.name.focus(); return;
	}
   
 if(isempty("Vehicle Registration No.",theform.vehicleno.value)==1 || validvehicle(theform.vehicleno.value)==false) { 
		flag=2; theform.vehicleno.focus(); return;
   	} 

	
	
   if(theform.email.value==""){ 
    	flag=1; 
		alert("Please enter your E-mail Address"); 
		theform.email.focus(); 
		return;
   }        
   else {
		if((theform.email.value.indexOf('@') == -1) || (theform.email.value.indexOf('.') == -1)) {
			flag=1; 
			alert("Check Your E-mail Address"); 
			theform.email.focus(); 
			return;
		}
	}
    if((theform.home.value=="") && (theform.office.value=="") && (theform.hp.value=="")) { 
		flag=1; 
		alert("Please fill in at least one contact no."); 
		theform.home.focus(); 
		return;
   }    
   if(theform.hp.value.length != ""){
   		if(theform.hp.value.length != 8){
		flag=1; 
		alert("Please check your HP no."); 
		theform.hp.focus(); 
		return;
		}
   }    
   if(theform.home.value.length != ""){
	    if(theform.home.value.length != 8){
		flag=1; 
		alert("Please check your home contact no."); 
		theform.home.focus(); 
		return;
	    }    
   }    
   if(theform.office.value.length != ""){
   		if(theform.office.value.length != 8){
		flag=1; 
		alert("Please check your office contact no."); 
		theform.office.focus(); 
		return;
   		}    
   }    
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
}	


//control the update particular 
function particular(theform) {
   var flag=0;
    if(isempty("Vehicle Registration No.",theform.vehicleno.value)==1 || validvehicle(theform.vehicleno.value)==false) { 
		flag=2; theform.vehicleno.focus(); return;
   	} 
	if(isempty("NRIC/ROC",theform.nric.value)==1){
		flag=1;	theform.nric.focus(); return;
	}
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
}	

//control the nac update particular
function memberparticular(theform) {
   var flag=0;
	if(isempty("NRIC/Passport/ROC",theform.nric.value)==1){
		flag=1;	theform.nric.focus(); return;
	}
	if (flag==0) { 
	theform.submit(); 
  	return 1; 
	} 
}	


