// JavaScript Document
(function($){
	$.fn.jquery=function(){
		$('#loginlink').click(function(){
			$('#loginform').fadeIn('slow');
			return false;
		});
		$('#logoutlink').click(function(){
			fncSendForm("logout");
			return false;
		});
		$('#closelogin').click(function(){
			$('#loginform').fadeOut('slow');
			return false;
		});
		if($('#products').length>0){
			intSeriesID=$('#series_id').attr("title");
			fncQueryProduct(1, intSeriesID);
			$('.page').click(function(){
				var regPattern=/[\w]+_([\d]+)/;
				intPageNumber=this.id.replace(regPattern, "$1");
				fncQueryProduct(intPageNumber, intSeriesID);
				return false;
			});
		}
		if($('#sendform').length>0){
			$('#sendform').click(function(){
				fncCheckForm("contact");
				return false;
			});
		}
		$('#send').click(function(){
			fncCheckForm("login");
			return false;
		});
		$('#addnew').click(function(){
			fncCheckForm("addnew");
			return false;
		});
		$('.tr_border_top input').each(function(){
			$(this).click(function(){
				var regPattern=/([\w]+)_([\d]+)/;
				if(regPattern.test(this.name)){
					intRequestID	=this.name.replace(regPattern, "$2");
					strFunction		=this.name.replace(regPattern, "$1");
					if(strFunction=="modifymember"){
						fncCheckForm("managemember_"+strFunction+"_"+intRequestID);
					}else if(strFunction=="deletemember"){
						fncSendForm("managemember_"+strFunction+"_"+intRequestID);
					}
				}
				return false;
			});
		});
		if($('#dologin').length>0){
			$('#loginform').fadeIn('slow');
			return false;
		}
		$('.delete input').click(function(){
			var regPattern=/delete_([\d]+)/;
			intDeleteID=this.id.replace(regPattern, "$1");
			fncSendForm("webmaster_deletecontact_"+intDeleteID);
		});
		$('.info a').click(function(){
			if(this.innerHTML=="Contact Info"){
				$(this).text("Close");
				fncShowContactInfo(this.rel);
			}else{
				$(this).text("Contact Info");
				fncShowContactInfo(this.rel, 1);
			}
			return false;
		});
	}
})(jQuery);


function fncCheckForm(strFormValue){
	var intError=0;
	if(strFormValue=="contact"){
		if($('input[name=yourname]').val()==""){
			intError=1;
		}else{
			regPattern=/[\w\d\.]+/;
			if(!regPattern.test($('input[name=yourname]').val())){
				intError=1;
			}
		}
		if($('input[name=email]').val()==""){
			intError=2;
		}
		if($('input[name=telephone]').val()==""){
			intError=3;
		}
		if($('textarea[name=comments]').val()==""){
			intError=4;
		}
	}else if(strFormValue=="login"){
		if($('input#username').val()==""){
			intError=5;
		}else if($('input#userpassword').val()==""){
			intError=6;
		}
	}else if(strFormValue=="addnew"){
		if($('input#username').val()==""){
			intError=7;
		}else if($('input#userpassword').val()==""){
			intError=8;
		}else if($('select#level').val()==-1){
			intError=9;
		}
	}else{
		var regPattern=/([\w]+)_([\w]+)_([\d]+)/;
		intRequestID=strFormValue.replace(regPattern, "$3");
		strSite			=strFormValue.replace(regPattern, "$1");
		strFormName	=strFormValue.replace(regPattern, "$2");
		if(strFormName=="modifymember"){
			if($('input#username_'+intRequestID).val()==""){
				intError=10;
			}else if($('input#userpassword_'+intRequestID).val()==""){
				intError=11;
			}
		}
	}
	if(intError>0){
		switch(intError){
			case 1:	alert("Please input your name!");
							$('input[name=yourname]').focus();
			break;
			case 2:	alert("Please input your email!");
							$('input[name=email]').focus();
			break;
			case 3:	alert("Please input telephone!");
							$('input[name=telephone]').focus();
			break;
			case 4:	alert("Please input comments!");
							$('textarea[name=comments]').focus();
			break;
			case 5:	alert("Please input user name!");
							$('input#username').focus();
			break;
			case 6:	alert("Please input password!");
							$('input#userpassword').focus();
			break;
			case 7:	alert("Please input user name!");
							$('input#username').focus();
			break;
			case 8:	alert("Please input password!");
							$('input#userpassword').focus();
			break;
			case 9:	alert("Please select user identity!");
							$('select#level').css("background-color", "#FFEE00");
			break;
			case 10:alert("Please input user name!");
							$('input#username_'+intRequestID).focus();
			break;
			case 11:alert("Please input password!");
							$('input#userpassword_'+intRequestID).focus();
			break;
			default:alert("Unknown error!");
		}
	}else{
		fncSendForm(strFormValue);
	}
}


function fncSendForm(strFormValue){
	strFormName=strFormValue;
	if(strFormValue=="contact"){
		strURL="index.php?site=contact&action=sendcontact";
		arrPostInfo=decodeURIComponent($("form[name=frm_onlinecontact]").serialize());
	}else if(strFormValue=="login"){
		strURL="index.php?site=login&action=login";
		arrPostInfo=$("form[name='frm_login']").serialize();
	}else if(strFormValue=="logout"){
		strURL="index.php?site=login&action=logout";
		arrPostInfo="";
	}else if(strFormValue=="addnew"){
		strURL="index.php?site=webmaster&action=addnew";
		arrPostInfo="username="+$('input[name=username_new]').val()+"&userpassword="+$('input[name=userpassword_new]').val()+"&level="+$('select[name=level_new]').val();
	}else{
		var regPattern=/([\w]+)_([\w]+)_([\d]+)/;
		intRequestID=strFormValue.replace(regPattern, "$3");
		strSite			=strFormValue.replace(regPattern, "$1");
		strFormName	=strFormValue.replace(regPattern, "$2");

		if(strSite=="webmaster"){
			strURL="index.php?site=webmaster&action="+strFormName;
			if(strFormName=="deletecontact"){
				arrPostInfo="id="+intRequestID;
			}
		}else{
			strURL="index.php?site=webmaster&action="+strFormName;
			if(strFormName=="modifymember"){
				arrPostInfo="id="+intRequestID+"&username="+$('input#username_'+intRequestID).val()+"&userpassword="+$('input#userpassword_'+intRequestID).val()+"&level="+$('select#level_'+intRequestID).val();
			}else if(strFormName=="deletemember"){
				arrPostInfo="id="+intRequestID;
			}
		}
	}
	fncAppendOverlay();
	$.ajax({
		url:strURL, 
		type:'POST', 
		dataType:'json', 
		data:{postinfo:arrPostInfo}, 
		complete:function(){
			
		},
   	success:function(json){
			if(strFormName=="contact"){
				if(json.error=="no_error"){
					$('input').attr("value", "");
					$('textarea').attr("value", "");
					alert("Message sent successfully!");
				}else{
					alert(json.error);
				}
			}else if(strFormName=="login"){
				if(json.error){
					alert(json.error);
				}else{
					$('#logout').show();
					$('#login').hide();
					if(json.webmaster){
						$('#webmaster').html(json.webmaster);
					}
				}
				$('#loginform').fadeOut('slow');
			}else if(strFormName=="logout"){
				strLocationAddress=window.location.href;
				intMatch=strLocationAddress.search("site=webmaster");
				if(intMatch>=0){
					window.location="?site=webmaster";
				}else{
					$('#logout').hide();
					$('#login').show();
					$('#webmaster').html("");
				}
			}else{
				if(strFormName=="modifymember"){
					$('#username_'+intRequestID).attr("value", json.username);
					$('#userpassword_'+intRequestID).attr("value", json.password);
				}else if(strFormName=="deletemember"){
					$('#table_row_'+intRequestID).remove();
				}else if(strFormName=="addnew"){
					$('#member_list').append('<tbody id="table_row_'+json.id+'" style="display:none;"></tbody>');
					$('#member_list tbody:last').append(json.addnew);
					$('#table_row_'+json.id).show(1000);
					$('input[name=username_new]').attr("value", "");
					$('input[name=userpassword_new]').attr("value", "");
					$('select[name=level_new] option[value=-1]').attr("selected", "selected");
				}else if(strFormName=="deletecontact"){
					$('#post_'+intRequestID).remove();
				}
				$('body').jquery();
			}
			fncAppendOverlay(1);
		}, 
		error:function(xhr, desc, e){
			alert(xhr.responseText);
			fncAppendOverlay(1);
		}
	});
}

function fncAppendOverlay(intClose){
	if(!intClose){
		var arrPageSize=getPageSize();
		strOverlay='<div id="overlay"></div><div id="loadingImg" style="margin:0 auto; position:absolute; top:50%; left:50%; margin:-16px 0 0 -16px; z-index:500; display:none;"><img src="images/loading.gif" /></div>';
		$("body").append(strOverlay);
		$('#overlay').height(arrPageSize[1]);
		$('#overlay').fadeIn('fast');
		$("#loadingImg").fadeIn('fast');
	}else{
		$("#loadingImg").fadeOut('fast');
		$('#overlay').fadeOut('fast');
	}
}

function fncQueryProduct(intPageNumber, intSeriesID){
	for(i=1; i<=2; i++){
		if(i==intPageNumber){
			$('#page_'+i).css('display', 'none');
			$('#pageselected_'+i).css('display', 'block');
		}else{
			$('#page_'+i).css('display', 'block');
			$('#pageselected_'+i).css('display', 'none');
		}
	}

	$.ajax({
		type:'GET', 
		url:'index.php?site=products&sid='+intSeriesID+'&queryproducts='+intPageNumber, 
		dataType:'json', 
		beforeSend:function(XMLHttpRequest){
			$('#products').fadeOut('slow');
			fncAppendOverlay();
		}, 
		complete:function(XMLHttpRequest, textStatus){
			fncAppendOverlay(1);
		}, 
		error:function(msg){
			//alert("jquery error!");
		}, 
		success:function(json){
			var strExpendHTML="";
			if(json.thumb.length>0){
				for(i=0; i<6; i++){
					if(json.thumb[i]){
						strExpendHTML+=json.thumb[i]+json.itemno[i];
					}else{
						strExpendHTML+='<div style="float:left; background-color:#FFF; width:300px; height:285px;"></div>';
					}
				}
			}else{
				strExpendHTML='<div style="position:absolute; width:100px; top:50%; left:50%; margin-left:-50px; color:#F00;">No item found!</div>';
			}
			$('#products')
				.html(strExpendHTML)
				.css('visibility','visible')
				.fadeIn('slow');
		}
	});
}
function getPageSize(){
	var jqueryPageSize=new Array($(document).width(), $(document).height(), $(window).width(), $(window).height());
	return jqueryPageSize;
};
function fncShowContactInfo(intRequestID, intCloseValue){
	if(!intCloseValue){
		$('#info_'+intRequestID).show(300);
		$('#info_'+intRequestID+' div').fadeIn(1200);
	}else{
		$('#info_'+intRequestID+' div').hide();
		$('#info_'+intRequestID).hide(300);
	}
}
