var url = "jc001/feedback.php";


	// register loading...
	var myGlobalHandlers = {
		onCreate: function(){
			Element.show('systemWorking');
		},

		onComplete: function() {
			if(Ajax.activeRequestCount == 0){
				Element.hide('systemWorking');
			}
		}
	};

	Ajax.Responders.register(myGlobalHandlers);
	document.write("<div id='systemWorking'>请在读取留言请稍等...<br /><img src='http://dev-test.jc001.cn/ajax/images/loading.gif' alt='loading...' /></div>");
	// end register
	
feedback_init();

function feedback_init()
{
	if(typeof(shop_id) == "undefined"){
		alert("Please init shop_id");
		return false;
	}
	
	url += '?shop_id=' + shop_id;
	
	document.write('<div id="ajax_feedback_list"></div>');
	document.write('<div id="ajax_feedback_form"></div>');
	
	feedback_getForm();
	feedback_getList();
}

// ------------------------------------------------- //
// -------------------ajax functions --------------- //
// ------------------------------------------------- //

// 加载输入框
function feedback_getForm(){
    var pars = 'ac=form';
    var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars,
			onComplete: feedback_showForm
		});
}
function feedback_showForm(response){
	$('ajax_feedback_form').innerHTML = response.responseText;
	initForm();
}

// 加载列表
function feedback_getList(page){
	if(page == undefined){
		page = 1;
	}
	
	var pars = 'ac=list&page=' + page;
	var myAjax = new Ajax.Updater(
		'ajax_feedback_list',
		url, 
		{
			method: 'get', 
			parameters: pars
		});
}

// 增加留言
function feedback_post(){
	
	var pars = 'ac=insert';
	
	pars += '&username=' + frm.username.value;
	pars += '&tel=' + frm.tel.value;
	pars += '&addr=' + frm.addr.value;
	pars += '&zip=' + frm.zip.value;
	pars += '&email=' + frm.email.value;
	pars += '&url=' + frm.url.value;
	pars += '&title=' + frm.title.value;
	pars += '&content=' + frm.content.value;
	pars += '&shop_id=' + shop_id;
	pars += '&user_id=' + frm.user_id.value;
	pars += '&validateCode=' + frm.validateCode.value;
	
 	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars,
			onComplete: feedback_insertcallback
		});
}
function feedback_insertcallback(response){
	if(response.responseText == "succ"){
		alert("您的留言发布成功，谢谢您的留言");
		frm.content.value = '';
		frm.content.value = '';
	}else{
		alert("respons:" + response.responseText);
	}
	
	$('img_validateCode').src = '/validateCodeCreate.php';
	frm.validateCode.value = '';
	//bbs_getList();
}

// ------------------------------------------------- //
// -------------------client functions ------------- //
// ------------------------------------------------- //

var frm;
var chk_objs = new Array();

function submitForm(){	
	if(frm == undefined){
		return false;	
	}
	for(i = 0; i < chk_objs.length; i++){
		if(chk_objs[i].pass == 0){
			alert(chk_objs[i].msg);
			chk_objs[i].focus();
			return false;
		}
	}
	
	feedback_post(); // post data
	return false;
}

function initForm(){
	frm = $("forminfo");
	if(frm == undefined){
		return false;	
	}
	
	for(i = 0,j = 0; i < frm.length; i++){
		if(frm[i].type == "text" || frm[i].type == "textarea"){
			if(frm[i].pass != undefined){
				chk_objs[j++] = frm[i];
			}
			frm[i].onchange = function(e){
				obj = this;
				if(obj.pattern){
					regex = new RegExp(obj.pattern,"g");
					if(!regex.test(obj.value)){
						if(obj.pass != "undefined"){
							obj.pass = 0;
						}
						if(obj.msg){
							alert(obj.msg);
							obj.select();
							obj.focus();
						}
					}else{
						if(obj.pass != "undefined"){
							obj.pass = 1;
						}
					}
				}
			}
		}
	}
}

