// trim empty space
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function checkEmail(mail){
   var emailfilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
   var result=emailfilter.test(mail)
   return result
} 

function checkPostCode(pc)
{
   var pwfilter=/^[a-zA-Z0-9]{6}$/
   var result=pwfilter.test(pc)
   return result;
} 
/* clear all the fields */
function clearField()
{
	$('span.errMsg').each(function(){$(this).html("")})
}
/* form test*/
		$(function(){
				 // setting up navigation
				
				   
				 $("#submitBtn").click(function(){
								
									clearField();
									var empty = false;	
											//check each input field	
											$('#submitArea input').each(function(){
												
												if($(this).val()=='' && $(this).attr('name') !='add2')
												{
												
														$(this).next('span').html("X");	
														empty = true;
												}
												else // validate phone and email
												{
														if($(this).attr('name') =='email')
														{
																if(	!checkEmail($(this).val()))
																{
																		$(this).next('span').html("X - invalid ");	
																		empty = true;
																}
														}
														
														if($(this).attr('name') =='postalCode')
														{
																if(	!checkPostCode($(this).val()))
																{
																		$(this).next('span').html("X - invalid ");	
																		empty = true;
																}
														}
														
												}
											
											})	// end of each input closure						
											
											if($('#comment').val().trim()=='')
											{
													$('#comment').next("span").html("X")
													empty = true;
											}
											
											
											if(!empty) // 
											{
													var type =  $("#submitBtn").attr("alt") // for submit different services
													
													
													if(type =='service') 
													{
														
														
														
														$.post('contactSupport.php',{type:type,contactName: $('#contactName').val(),position:$('#position').val(),email:$('#email').val(),add1:$('#adr1').val(),add2:$('#add2').val(),city:$('#city').val(),province:$('#province').val(),postalCode:$('#postalCode').val(),phone:$('#phone').val(),fax:$('#fax').val(),request:$('#request').val(),url:$('#url').val(),bussinessType:$('#bussinessType').val(),comment:$('#comment').val()},function(data){
																																																																																																																					//$('#feedback').html(data)
	alert(data);																																																																																																																								  });												}
													else if(type =='consultation')
													{
														
					
						
														$.post('contactSupport.php',{type:type,contactName: $('#contactName').val(),companyName:$('#companName').val(),url:$('#url').val(),services:$('#services').val(),comment:$('#comment').val()},function(data){
																					//	$('#feedback').html(data)
																					
																					alert(data);
																																																																																																																										
																																																																																																																																																																																																																																															  });													
													}
												
											}
											
											
										})//end of click
				  })// outer closure

