// JavaScript Document
var secrities = new Array();
function sending(){ 
var tag = test();
var form = $("#frm");
if(tag==1){
form.submit();
}
}

function test(){
var emails = $("#myemail"); 
var myerror = new Array();
var items = new Array('name','country','company');
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 !='') ? myerror.push(temp) : '';
}

temp = verifyEmail(emails);  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 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 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;
}
var obj = security();
secrity_setting(obj);