function callInProgress (xmlhttp) {
       switch (xmlhttp.readyState) {
         case 1: case 2: case 3:
            return true;
            break;
         // Case 4 and 0
            default:
            return false;
            break;
        }
}

function showFailureMessage() {
         //alert('uh oh, it looks like the network is down. Try again shortly');
}

// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({

     onCreate: function(request) {

     request['timeoutId'] = window.setTimeout(
          function() {
             // If we have hit the timeout and the AJAX request is active, abort it and let the user know
             if (callInProgress(request.transport)) {
                 request.transport.abort();
                 //showFailureMessage();
                 // Run the onFailure method if we set one up when creating the AJAX object
                if (request.options['onFailure']) {
                    request.options['onFailure'](request.transport, request.json);
                }
             }
          },
          5000 // Five seconds
    );
    },

    onComplete: function(request) {
         // Clear the timeout, the request completed ok
          window.clearTimeout(request['timeoutId']);
    }
});


var zbeautyguide = {
	msg_error:"",
	post : function()
	{
		if(this.checkRequiredInfomation()){
		$("zbeautyguide-loader").innerHTML = '<img src="/design/ellehk/images/global/ajax-loader-formulaire.gif" alt="Loading" title="Loading" />';
		new Ajax.Request
		( 
		'/zbeautyguide/post',{
		asynchronous:false, 
		evalScripts:false, 
                requestTimeout:2,
		method:'post', 
		parameters:Form.serialize('zbeautyguideform'), 
		onComplete:this.after_post,
                onFailure: this.timeout
		})}else{
			alert(this.msg_error);
			this.msg_error="";
		}
	},
        timeout:function(){
		//$("zbeautyguide-message").innerHTML = '<strong style="font-size: 14px; color: red;">謝謝您給該產品發表意見！</strong>';
		//$("zbeautyguide-loader").innerHTML = "";
		alert('謝謝您給該產品發表意見！');
		window.location.reload();

        },
	after_post:function(ajax)
	{
		//var res = eval('('+ajax.responseText+')');
		//$("zbeautyguide-message").innerHTML = res.content;
		//$("zbeautyguide-loader").innerHTML = "";
		alert('謝謝您給該產品發表意見！');
		window.location.reload();
	},
        checkRequiredInfomation:function(){
    		var flag = true;
    		if(!check_radio($("question0").getElementsByTagName('input'))){
        		this.msg_error += "賞讚商品 是一個必選欄位. \n";
			flag = false;
		}
   		if(Trim($('txtarea-product-opinion').value) == '' || Trim($('txtarea-product-opinion').value) == null){
        		this.msg_error += "意見 是一個必填欄位. \n";
			flag = false;
    		}
	       	if(!check_radio($("question1").getElementsByTagName('input'))){
        		this.msg_error += "色調 是一個必選欄位. \n";
			flag = false;
    		}
    		if(!check_radio($("question2").getElementsByTagName('input'))){
			this.msg_error += "質感 是一個必選欄位. \n";
       			flag = false;
    		}
    		if(!check_radio($("question3").getElementsByTagName('input'))){
        		this.msg_error += "持久 是一個必選欄位. \n";
       			flag = false;
    		}
    		if(!check_radio($("question4").getElementsByTagName('input'))){
        		this.msg_error += "包裝 是一個必選欄位. \n";
			flag = false;
    		}
    		if(!check_radio($("question5").getElementsByTagName('input'))){
        		this.msg_error += "抵買 是一個必選欄位. \n";
			flag = false;
    		}
    		return flag;
	}
	
};

function Trim(ori)
{
    while (ori.substring(0,1) == ' ')
    {
        ori = ori.substring(1, ori.length);
    }
    while (ori.substring(ori.length-1, ori.length) == ' ')
    {
        ori = ori.substring(0,ori.length-1);
    }
    return ori;
}

function check_radio(elements){
    var flag = false;
    if(elements){
        for(i=0; i<elements.length; i++){
	    if(elements[i].checked == true){
	     	flag = true;
	    }
	}
    } 
    return flag;
}




//********************
// add by zhichang 
//********************
(function($$$){
 	$$$(document).ready(function(){
		        $$$("#select-artist").change(function(){
				  var localUrls="beauty/beauty_guide/detail_choice_page/(user_id)/"+$$$(this).val();
				  location.href="/"+localUrls;
					});
			if(!!document.getElementById("sbm-product-comment")){
				$$$("#sbm-product-comment").click(function(){
						zbeautyguide.post();
					});
				}
			if(!!document.getElementById("guide-keyword-search")){
				$$$("#txt-guide-keyword-search").click(function(){
						$$$(this).val("");
					});
				$$$("#txt-guide-keyword-search").blur(function(){
						if(!$$$(this).val()){
							$$$(this).val("請輸入關鍵字");
						}
					});
				$$$(".go","#guide-keyword-search").click(function(){
						var keyword=$$$("#txt-guide-keyword-search").val();
						if(keyword=="請輸入關鍵字"){
							alert("請輸入關鍵字");
							return;
						}
						location.href="/"+localUrl+"/(keyword_search)/"+keyword;
						return false;
					});	
			}
			if(!!document.getElementById("form-advance-search")){
				$$$("#categories").change(function(){
						var sel_value=$$$(this).val();
						var sub_select=document.getElementById("sub-categories");
						var sub_options=sub_select.options;
						if(sel_value==0){
							for(var i=sub_options.length-1;i>-1;i--)
								sub_select.remove(i);					
							sub_options.add(new Option("--PLEASE SELECT SUB-CATEGORY--","0"));
							return;
						}
						var first_key=sel_value;
						for(var i=0; i<categories.length-1; i++){
							if(categories[i]==first_key){
						       		var last_key=categories[i+1];
								break;
							}
						}
						for(var i=sub_options.length-1;i>-1;i--)
							sub_select.remove(i);
						var dis_value=0;
						for(var i=0; i<=sub_categories.length-1; i++){
							if(sub_categories[i][0]==last_key) dis_value=0;
							if(dis_value==1){
								sub_options.add(new Option(sub_categories[i][1],sub_categories[i][0]));
							}
							if(sub_categories[i][0]==first_key) dis_value=1;
						}
					});
				$$$("#btn-clear").click(function(){
							$$$("#categories").val(0);
							var sub_select=document.getElementById("sub-categories");
							var sub_options=sub_select.options;
							for(var i=sub_options.length-1;i>-1;i--)
								sub_select.remove(i);					
							sub_options.add(new Option("--PLEASE SELECT SUB-CATEGORY--","0"));
							$$$("#inputaid_text_BeautyProductBrandNameDataSource").val("");
							$$$("#inputaid_text_BeautyProductProductNameDataSource").val("");
							$$$("#function").val(0);
							$$$("#price").val(0);
						});
				$$$("#btn-advance-search").click(function(){
							localUrl += "/(advance_result)/result";
							if($$$("#categories").val()!=0)
								localUrl += "/(category)/"+$$$("#categories").val();
							if($$$("#sub-categories").val()!=0)
								localUrl += "/(sub_category)/"+$$$("#sub-categories").val();
							if($$$("#inputaid_text_BeautyProductBrandNameDataSource").val()!="")
								localUrl += "/(brand_name)/"+$$$("#inputaid_text_BeautyProductBrandNameDataSource").val();
							if($$$("#inputaid_text_BeautyProductProductNameDataSource").val()!="")
								localUrl += "/(product_name)/"+$$$("#inputaid_text_BeautyProductProductNameDataSource").val();
							if($$$("#function").val()!="0")
								localUrl += "/(function)/"+$$$("#function").val();
							if($$$("#price").val()!="0")
								localUrl += "/(price)/"+$$$("#price").val();
							location.href="/"+localUrl;
							return false;
						});
				$$$("#btn-clear").click();
			}
		});	
 })(jQuery);
