var secrities = new Array();
function sending(){ 
var tag = test();
var form = $("#tryto");
if(tag==1){ form.submit();}
}

function test(){ 
var emails = $("#myemail");
var myerror = new Array();
var items = new Array('first_name','last_name','company_name','myusername', 'username_confirm');
var temp_obj = '';
var temp  = '';
var errors  = ''; 

for(var i =0; i<items.length;i++){
 temp_obj = '#'+items[i];  temp_obj = $(temp_obj);  temp = veryfy(temp_obj); 
 if(temp !='') { errors = errors　+ temp; }
}

temp = verifyEmail(emails);      if(temp !='') errors = errors　+ temp; 
temp = keepTheSame($('#myemail'), $('#email_confirm'));  if(temp !='') errors = errors　+ temp;
temp = keepTheSame($('#myusername'), $('#username_confirm'));  if(temp !='') errors = errors　+ temp;
temp = security_check($("#sec")); if(temp !='') errors = errors　+ temp;

if(errors != ''){
 $('#myerror').css("color","#cc0000");
 $('#myerror').css("margin","20px");
 $('#myerror').html("Error:<br>"+errors);
 return 0;
} else{ 
 $('#myerror').html(null);
 return 1;
 }
}

function secrity_setting(obj){
 var tip = "<br><span style='font-size:10px;'> Solve this simple math problem and enter the result. E.g. for 1+3, enter 4. \r\n Help fight spam. </span>"
 var items = "<input type='text' name='sec' id='sec' size='5' />";
 var txt = obj[1] + " + " + obj[2] + " = " + items + tip;
 $("#security").append(txt);
}

function security(){
 secrities[1] =	Math.floor(Math.random()*100);
 secrities[2] = Math.floor(Math.random()*100);
 secrities[3] = secrities[1] + secrities[2];
 return secrities;
}

function security_check(o){
 var error='';
 if(obj[3]!=$('#sec').val()){ 
 error ='<li>Security question error!!</li>';
 hightlight(o,1);
 }else{
	 hightlight(o,2);
	 }
 return error;
}

function keepTheSame(a,b){
 var error = '';
 if(a.val() != b.val()){ error = "<li>"+a.attr("id") + ' and '+ b.attr("id") + " should be the same! <br></li>"; 
 hightlight(a,1);  hightlight(b,1);
 }else{
	 hightlight(a,2);  hightlight(b,2);
	 }
 return error;
}

function hightlight(obj,i){
 if(i==1){ $(obj).css("border","1px solid red");} else {$(obj).css("border","1px solid grey");}
 }

function verifyEmail(obj){
var email = obj.val(); 
var error='';
var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
flag = pattern.test(email);
　if(flag){ 
    hightlight(obj,2); 
	return "";
	}else{ 
	hightlight(obj,1);
	error = 'Your email address is incorrect! \r\n'; 	
	return error;
	}
 }

function veryfy(obj){
    var o= obj.val();
	var error=""; 	
	if(o=='' || o == null){ 
	hightlight(obj,1);
	error = "<li>"+obj.attr("id") + " can not be NULL! <br></li>"; 
	return error;
	}else{
	hightlight(obj,2);
	return '';
	}
  }

function sendingssss(){	
  var frm=$("#tryto"); 

  if($('#first_name').val()=="" || $('#last_name').val()=="" || $('#company_name').val()=="" || $('#last_name').val()=="" || $('#myemail').val()=="") {
   alert("Please make sure you have filled up all the fileds of the form!!");
   return false;
   } 

 
 if($('#myemail').val()== $('#email_confirm').val()) {
	 frm.submit();
	 }else{ 
	 alert("Keep the confirmed email same with the email!!"); 
	 return false;
	 }

}


var obj = security();
secrity_setting(obj);
