//////*
$(document).ready(function () {
  //$.get("/avail.php", { id: $("#pid").val() },
  //function(data){
  //  if($(".avail_btn") && data.match(/^ok: ([0-9a-z]+) \[/i)){
      //$(".avail_btn").attr("src","/images/avail.gif");
  //  }
  //  if($(".avail_btn") && data.match(/^err: noURL \[/i)){
      //$(".avail_btn").css("display","none");
  //  }
  //  if($(".aprice") && data.match(/^ok: \[([0-9\.]+)\]$/i)){
      //$(".aprice").html("<font color=red>"+RegExp.$1+"</font>");
  //  }
  //});
  
});
/////*/

$(document).ready(function(){
	$("#firstone").click(function () { 
	      rating2(); 
	});
	$(".addreview").click(function () { 
	      rating(); 
	});
	$(".addquestion").click(function () { 
	      question(); 
	});
	$(".addcomment").click(function () { 
	      comment(); 
	});
	
	
});






function chkf(){
	var is_ok=false;
	if(document.review_form.Title.value==""){
		alert("Please enter Your Review Title!");
		document.review_form.Title.focus();
	}else if(document.review_form.Rating_All.value==""){
		alert("Please Make a Rating!");
		//document.review_form.Rating_All.focus();	
	}else if(document.review_form.Name.value==""){
		alert("Please enter Your Name!");
		document.review_form.Name.focus();	
	}else if(document.review_form.Email.value==""){
		alert("Please enter Your Email!");
		document.review_form.Email.focus();	
	}else if(document.review_form.Contents.value==""){
		alert("Please enter Full Review!");
		document.review_form.Contents.focus();	
	}else{
		is_ok=true;
	}
	return is_ok;	
}

function rating(){
	hide_rpls();
	if(document.getElementById("ratingform").style.display=="none"){
		hide_forms();
		document.getElementById("ratingform").style.display="";
	}else{
		hide_forms();
		//document.getElementById("ratingform").style.display="none"; 
	}
        document.review_form.Contents.focus();
}

function question(){
	hide_rpls();
	if(document.getElementById("askQ").style.display=="none"){
		hide_forms();
		document.getElementById("askQ").style.display="";
	}else{
		hide_forms();
		//document.getElementById("askQ").style.display="none"; 
	}
    document.q.comment.focus();
}

function comment(){
	hide_rpls();
	if(document.getElementById("cmform").style.display=="none"){
		hide_forms();
		document.getElementById("cmform").style.display="";
	}else{
		hide_forms();
		//document.getElementById("cmform").style.display="none"; 
	}
    document.postf.Contents.focus();
}


function hide_rpls(){
	$("div").each(function(i){	
			var id = $(this).attr('id');
			if(id.match(/^aform_[0-9]+/i)){
				if(document.getElementById(id)) document.getElementById(id).style.display="none";
			}
	});
	
}

function hide_forms(){
	if(document.getElementById("ratingform")) document.getElementById("ratingform").style.display="none";
	if(document.getElementById("askQ")) document.getElementById("askQ").style.display="none";
	if(document.getElementById("cmform")) document.getElementById("cmform").style.display="none";

}

function rating2(){
	document.getElementById("ratingform").style.display="";
	document.review_form.Contents.focus();
}

function vote_review(pid,rid,rate){
	var data='';
	if(rate=='yes'){
		data='id='+pid+'&vote='+rid+'&yes=1';
	}else{
		data='id='+pid+'&vote='+rid+'&no=1';
	}
	var resultid='#'+'vote_rv_'+pid+'_'+rid;
	$(resultid).html('<img src="/images/loading.gif" height="16">'); 
			$.ajax({  
		     url: "/straightener_review.php",   
		     type: "get",        
		     data: data,       
		     cache: false,
		     success: function (html) {  
		     		 if(html.match(/^err: (.+)$/ig)){
		     		 		html="<font color=red>Error: "+RegExp.$1+"</font>";
		         }
		         $(resultid).html(html); 
		         $(resultid).fadeIn(200, function () {
		             $(resultid).fadeOut(2000);  
		         });           
		     }         
		 }); 
}

function click_rating(n) {
	var obj=document.getElementById("voting");
	var trg=document.getElementById("Rating_All");
	//alert(obj.className);
	if(n>=1 && n<=5){
		if(n==1){
			obj.className="rating onestar";
			trg.value=1;
		}
		if(n==2){
			obj.className="rating twostar";
			trg.value=2;
		}
		if(n==3){
			obj.className="rating threestar";
			trg.value=3;
		}
		if(n==4){
			obj.className="rating fourstar";
			trg.value=4;
		}
		if(n==5){
			obj.className="rating fivestar";
			trg.value=5;
		}
	}
}

function more_poco(name){
	
	$("span").each(function(i){	
			var id = $(this).attr('id');
			if(id.match(/^more_poco_[0-9]+/i) && "more_poco_"+name!=id){
				if(document.getElementById(id)) document.getElementById(id).style.display="none";
			}
	});
	
	var obj=document.getElementById("more_poco_"+name);
	if(obj){
		if(obj.style.display=="none"){
			obj.style.display="";
		}else{
			obj.style.display="none";
		}
	}
}

function add_poco(v){
	var trg=document.review_form;
	if(trg.poco.value==""){
		trg.poco.value=v;
	}else{
		var arr=arr_unique(trg.poco.value.split(','));
		if(!arr_contains(arr, v)){
			trg.poco.value=trg.poco.value+","+v;
		}
	}
	trg.poco.focus();
	trg.poco.select();
}

function arr_unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!arr_contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

function arr_contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}

function brand_thumb(cid,yes,no){
    var TimeoutId;
    $("#brand_vote").html('<img src=/images/loading.gif width=16>');
    $.get("/thumb.php", { 'id': cid, 't': 'c', 'yes': yes, 'no': no},
    function(data){
      if($("#brand_vote") && data.match(/^ok: exists/i)){
        $("#brand_vote").html("<font color=blue>You have voted.</font>");
      }
      if($("#brand_vote") && data.match(/^err: /i)){
        $("#brand_vote").html("<font color=red>Failed to vote.</font>");
      }
      if($("#brand_vote") && data.match(/^ok: done/i)){
        $("#brand_vote").html("<font color=green>Thanks for vote.</font>");
      }
      if($("#brand_vote") && data.match(/^ok: done\[yes\]/i)){
        $("#ryes").html(parseInt($("#ryes").html())+1);
        $("#rall").html(parseInt($("#rall").html())+1);
        $("#brand_vote").html("<font color=green>Thanks for vote.</font>");
      }
      if($("#brand_vote") && data.match(/^ok: done\[no\]/i)){
        $("#rno").html(parseInt($("#rno").html())+1);
        $("#rall").html(parseInt($("#rall").html())+1);
        $("#brand_vote").html("<font color=green>Thanks for vote.</font>");
      }
      TimeoutId = setTimeout(function(){$("#brand_vote").html("")}, 3000);
    });
    
  }
