// JavaScript Document
var reg_username		=	/^[A-Za-z0-9_\s]{6,20}$/;
var reg_mail			=	/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
var reg_selection		=	/^[A-Za-z0-9_\s\.\-]{4,120}$/;
var reg_link			=	/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
var reg_ordtitle		=	/^[A-Za-z0-9_\s\:\(\)\-]{1,60}$/;
var reg_firstlastname	=	/^[A-Za-z0-9_]{1,40}$/;
var reg_telephone		=	/^\d{1,2}-\d{3,4}-\d{6,10}$/;
var reg_cellphone		=	/^[0-9\-]{8,20}$/;
var reg_ordwordcanemp	=	/^[A-Za-z0-9_\s\.]{0,160}$/; //Can be empty, Department, Street, Otherresearch, City
var reg_ordword			=	/^[A-Za-z0-9_\s\.]{1,160}$/; //Cannot be empty
var reg_postalcode		=	/^\d{6}$/;
var reg_pic_widandhei	=	/^\d{0,3}$/;//Pictures' width and height
var reg_haveother 		=	/(.*)(other)(.*)(area)(.*)/;
var reg_middleinitial	= 	/^[A-Za-z0-9_]{0,20}$/;
var reg_year			=	/^\d{4}$/;

function changeverifypic(){
	$.get("verifycode.php", {ajaxxxx: true, async: false}, function(data){
		$("#p_verifycodenum").val(data);
		$("#p_verifycode").attr("src", "verifycode"+data+".JPEG");
	});
}
//To judging submit the login data
function front_index_inputbox_submit(){
	if (!reg_username.test($("#p_name").val())){
		alert("Sorry, the username and the password are not matched!");
		return false;
	}
	return true;
}
function front_forgetpasswd_submit(){
	if (!reg_username.test($("#forgetpasswd_name").val())){
		alert("Sorry, the format of username is illegal!");
		return false;
	}
	if (!reg_mail.test($("#forgetpasswd_mail").val())){
		alert("Sorry, the format of email is illegal!");
		return false;
	}
	return true;
}
function front_resetpasswd_submit(){
	if (!reg_username.test($("#resetpasswd_name").val())){
		alert("Sorry, the format of username is illegal!");
		return false;
	}
	if ($("#resetpasswd_passwd").val().length<6){
		alert("Sorry, the format of password is illegal!");
		return false;
	}
	if (!($("#resetpasswd_passwd").val()==$("#resetpasswd_conpasswd").val())){
		alert("Sorry, two passwords are not the same!");
		return false;
	}
	return true;
}
function front_regist_submit(){
	var p_name = $("#caccp_name").val();
	var p_mail = $("#caccp_mail").val();
	var p_passwd = $("#caccp_passwd").val();
	var p_conpasswd = $("#caccp_conpasswd").val();
	var p_firstName = $("#caccp_firstName").val();//prefix
	var p_middleInitial = $("#caccp_middleInitial").val();
	var p_LastName = $("#caccp_lastName").val();
	var p_f_id = $("#caccp_f_id").val();
	var p_h_id = $("#caccp_h_id").val();
	var p_s_id = $("#caccp_s_id").val();
	var p_otherresearch = $("#caccp_otherresearch").val();
	var p_accept = $("#caccp_accept").val();
	if (!reg_username.test(p_name)){
		alert("The format of Username is not correct!");
		changeverifypic();
		return false;
	}
	if (!reg_mail.test(p_mail)){
		alert("The format of Email is not correct!");
		changeverifypic();
		return false;
	}
	if (p_passwd.length<6){
		alert("The format of Password is not correct!");
		changeverifypic();
		return false;
	}
	if (!reg_ordwordcanemp.test(p_middleInitial)){
		alert("The format of Middle initial is not correct!");
		changeverifypic();
		return false;
	}
	if (!reg_firstlastname.test(p_firstName)){
		alert("The format of First Name is not correct!");
		changeverifypic();
		return false;
	}
	if (!reg_firstlastname.test(p_lastName)){
		alert("The format of Last Name is not correct!");
		changeverifypic();
		return false;
	}
	if (parseInt(p_f_id)<=0){
		alert("Please select your prefix!");
		changeverifypic();
		return false;
	}
	if (parseInt(p_h_id)<=0){
		alert("Please select your highest education level!");
		changeverifypic();
		return false;
	}
	if (parseInt(p_s_id)<=0){
		alert("Please select your research fields!");
		changeverifypic();
		return false;
	}
	if (reg_haveother.test($("#caccp_s_id").find("option:selected").text())){
		if (!reg_ordword.test(p_otherresearch)){
			alert("The format of research fields is not correct!");
			changeverifypic();
			return false;
		}
	}else{
		if (!reg_ordwordcanemp.test(p_otherresearch)){
			alert("The format of research fields is not correct!");
			changeverifypic();
			return false;
		}
	}
	//verify code judging
	if ($("#p_verifycodenum").val()!=$("#p_verify").val()){
		alert("The verify code is not correct!");
		changeverifypic();
		return false;
	}
	if (!p_accept){
		alert("Please tick the accept box!");
		changeverifypic();
		return false;	
	}
	$("#otherareas").val($("#caccp_s_id").find("option:selected").text());
	return true;
}
function front_indmod_submit(){
	var p_mail = $("#modindp_mail").val();
	if (!reg_mail.test(p_mail)){
		alert("The mail format is illegal!");return false;
	}
	var p_firstName = $("#modindp_firstName").val();
	if (!reg_firstlastname.test(p_firstName)){
		alert("The first Name format is illegal!");return false;
	}
	var p_middleInitial = $("#modindp_middleInitial").val();
	if (!reg_middleinitial.test(p_middleInitial)){
		alert("The middle Initial format is illegal!");return false;
	}
	var p_lastName = $("#modindp_lastName").val();
	if (!reg_firstlastname.test(p_lastName)){
		alert("The last Name format is illegal!");return false;
	}
	var p_f_id = $("#modindp_f_id").val();
	if (parseInt(p_f_id)<=0){
		alert("You haven't select the prefix!");return false;
	}
	var p_h_id = $("#modindp_h_id").val();
	if (parseInt(p_h_id)<=0){
		alert("You haven't select the highest education level!");return false;
	}
	var p_s_id = $("#modindp_s_id").val();
	if (parseInt(p_s_id)<=0){
		alert("You haven't select the research fields!");return false;
	}
	var p_otherresearch = $("#modindp_otherresearch").val();
	if (reg_haveother.test($("#modindp_s_id").find("option:selected").text())){
		if (!reg_ordword.test(p_otherresearch)){
			alert("The format of other research fields is not correct! if you select OTHER AREAS, you should enter this blank!");return false;			
		}
	}else{
		if (!reg_ordwordcanemp.test(p_otherresearch)){
			alert("The format of other research fields is not correct!");return false;			
		}
	}
	//professional information
	var p_telephone = $("#modindp_telephone").val();
	if ((!reg_telephone.test(p_telephone))&&(p_telephone)){
		alert("The telephone format is illegal!");return false;
	}
	var p_cellphone = $("#modindp_cellphone").val();
	if ((!reg_cellphone.test(p_cellphone))&&(p_cellphone)){
		alert("The cellphone format is illegal!");return false;
	}
	var p_company = $("#modindp_company").val();
	if (!reg_ordwordcanemp.test(p_company)){
		alert("The company or college format is illegal!");return false;
	}
	var p_department = $("#modindp_department").val();
	if (!reg_ordwordcanemp.test(p_department)){
		alert("The department format is illegal!");return false;
	}
	var p_street = $("#modindp_street").val();
	if (!reg_ordwordcanemp.test(p_street)){
		alert("The address format is illegal!");return false;
	}
	var p_city = $("#modindp_city").val();
	if (!reg_ordwordcanemp.test(p_city)){
		alert("The city format is illegal!");return false;
	}
	var p_postalCode = $("#modindp_postalCode").val();
	if ((!reg_postalcode.test(p_postalCode))&&(p_postalCode)){
		alert("The postal code format is illegal!");return false;
	}
	var p_c_id = $("#modindp_c_id").val();
	var p_month = $("#modindp_month").val();
	var p_date = $("#modindp_date").val();
	var p_year = $("#modindp_year").val();
	if ((!reg_year.test(p_year))&&(p_year)){
		alert("The year format is illegal!");return false;
	}else{
		if ((parseInt(p_year)>1905)&&(parseInt(p_year)<2030)&&(parseInt(p_month)>0)&&(parseInt(p_date)>0)){}else{
			alert("Your birth information is not complete, it will not be saved!");
		}
	}
	return true;
}
window.onload = function(){
	if ($("#ppp")){
		$("#ppp").children().filter(".font_red").click(function(){
			var file = $(this).children().filter("img").attr("src")=="img/unfold.png"?"img/fold.png":"img/unfold.png";
			$(this).children().filter("img").attr("src", file);
			$("#"+$(this).attr("id").substr(0,2)+"_2").slideToggle("fast");
		})
	};
//Press the Enter Key to Submit the data
	if ($("#newslist_search")){
		$("#newslist_search").keypress(function(event){
			if (event.keyCode==13){
				location.href = "newslist.php?search="+$(this).val();
			}
		}); 
	}
//Click the login button
	if (($("#login_info"))&&($("#login_inputbox"))){
		$("#frontlogin_button").click(function(){
			$("#login_info").toggle();
			$("#login_inputbox").toggle();
		})
	};
//Submit search data
	if ($("#frontsearch_button")){
		$("#frontsearch_button").click(function(){
		//front_filename, frontsearch_input
			var reg = /^[A-Za-z0-9_\s]{1,40}$/;
			if (reg.test($("#frontsearch_input").val())){
				location.href = "search.php?search="+$("#frontsearch_input").val();
			}else{
				alert("The search data is illegal!");
			}
		})
	};
	if ($("#down")){
		var fnn = function(){
			var mm=0;//0:not found yet;1:has found
			$("#headline_img").children().filter("img").each(function(i, n){
				if ((mm==0)&&($(this).css("display")!="none")){
					$(this).fadeOut('300', function(){
						if ($(this).next().attr('id')){
							$(this).next().fadeIn('300');
							$('#headline_title').html($('#headline_title'+(i+2)).val());
							$('#headline_content').html($('#headline_content'+(i+2)).val());
							$('#morehref').attr("href", $('#headline_href'+(i+2)).val());
						}else{
							$(this).parent().children().filter('img').eq(0).fadeIn('300');
							$('#headline_title').html($('#headline_title'+1).val());
							$('#headline_content').html($('#headline_content'+1).val());
							$('#morehref').attr("href", $('#headline_href'+1).val());
						}
					})
					mm=1;
				}
			})
		};
		var fnn2 = function(){
			var mm=0;//0:not found yet;1:has found
			$("#headline_img").children().filter("img").each(function(i, n){
				if ((mm==0)&&($(this).css("display")!="none")){
					$(this).fadeOut('300', function(){
						if ($(this).prev().attr('id')){
							$(this).prev().fadeIn('300');
							$('#headline_title').html($('#headline_title'+(i+2)).val());
							$('#headline_content').html($('#headline_content'+(i+2)).val());
							$('#morehref').attr("href", $('#headline_href'+(i+2)).val());
						}else{
							var domlength = $(this).parent().children().filter('img').length;
							$(this).parent().children().filter('img').eq(domlength-1).fadeIn('300');
							$('#headline_title').html($('#headline_title'+1).val());
							$('#headline_content').html($('#headline_content'+1).val());
							$('#morehref').attr("href", $('#headline_href'+1).val());
						}
					})
					mm=1;
				}
			})
		};
		$("#down").click(fnn);
		$("#up").click(fnn2);
		setInterval(fnn, 3000);
	}
//modify information
	if($("#indmod_form")){
		$("#wantbemember").click(function(){
			$("#bemember").val(1);
			if (front_indmod_submit()){$("#indmod_form").submit();}
		});
		$("#goback").click(function(){location.href="individual.php";});
		
	}
}
