	/*	WPSHOP 1.9
	File: version2_admin.js

	Author: Andy Pieters for Cregy Web Design
	x_terminat_or_3@yahoo.fr

	JavaScript functions for the client part of the WP-SHOP

	Released under the terms of the GPL (see COPYING)*/

	

//Version 2 starts here
var stars=new Array();
var http=(navigator.appName.indexOf('Microsoft')==-1?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP'));
var busy=false;

wait(); //wait for body to load

function do_hook() {
	//find any input elements with name 'score_x' and attach an event handler to them

	var coll=document.getElementsByTagName('input'),counter;

	if(coll && coll.length) {
		for(counter=0; counter<coll.length; counter++) {
			if(coll[counter].name.match(/^score_\d{1,3}$/)) {
				coll[counter].onmousemove=mouse_enter_score;
				coll[counter].onmouseout=mouse_leave_score;
				coll[counter].onclick=function () { return mouse_click_score(this); }
				stars[coll[counter].name]=coll[counter].src;
			}
		}
	}

	//attach event handlers to image select buttons

	coll=document.getElementsByTagName('a');

	if(coll && coll.length) {
	
		for(counter=0; counter<coll.length; counter++) {
		
			if(coll[counter].getAttribute('imagebutton')=='yes') {
				coll[counter].onclick=function() { return changePicture(this); };
			}
		}
	}
}

function changePicture(what) {

	if(busy) {
		return true; //won't wait for http object to be finished
	}

	var req='',xtarget='';

	req='./wp-content/plugins/wpshop/wpshop_rpc.php?procedure=get_product_picture' +
				'&wpshop_product='+what.getAttribute('product') +
				'&picture='+what.getAttribute('pictureindex');

	if(xtarget=what.getAttribute('img')) {
		req+='&img='+xtarget; //extra parameters are put in the answer
	}

	if(xtarget=what.getAttribute('caption')) {
		req+='&caption='+xtarget;
	}
	
	http.open('get',req,true);

	http.onreadystatechange=function() {
		var answer,pic,counter,xtarget='',xid='';
		
		if(http && http.readyState==4) {
			busy=false;

			if(http.responseXML) {
			
				if( (answer=http.responseXML.getElementsByTagName('rpc')) && (answer.length)) {
				
					if( (answer[0].getAttribute('procedure')=='get_product_picture') &&
						(answer[0].getAttribute('success')=='yes')) {

						xid=(answer[0].getAttribute('caption')?answer[0].getAttribute('caption'):'myproductpicturecaption');
												
						if(pic=document.getElementById(xid)) {
							pic.innerHTML='';

							if(answer[0].childNodes.length) {
								pic.innerHTML=answer[0].childNodes[0].data;
							}
							
						}

						xid=(answer[0].getAttribute('img')?answer[0].getAttribute('img'):'myproductpicture');
						
						if(pic=document.getElementById(xid)) {
							pic.src=answer[0].getAttribute('src');
							changeSelectedPictureLink(answer[0].getAttribute('picture'),xid);
						}
					}
				}
			}
		}
	}

	busy=true;

	http.send(null);

	return false;
}

function changeSelectedPictureLink(pictureindex,group) {

	var coll,counter=0,alink;

	oldsearchmode=( (typeof(group)=='undefined') || (group.length==0));
	
	if( (coll=document.getElementsByTagName('a')) && (coll.length)) {

		for(counter=0; counter<coll.length; counter++) {

			alink=coll[counter];
			
			if(oldsearchmode) {
				if(alink.getAttribute('imagebutton')='yes') {
					alink.className=alink.className.replace(/\s?focused\s?/,'');
	
					if(alink.getAttribute('pictureindex')==pictureindex) {
						alink.className+=' focused';
					}
				}
			} else {
				if( (alink.getAttribute('imagebutton')=='yes') && (alink.getAttribute('img')==group)) {
					alink.className=alink.className.replace(/\s?focused\s?/,'');
	
					if(alink.getAttribute('pictureindex')==pictureindex) {
						alink.className+=' focused';
					}					
				}
			}

		}
	}
}

function mouse_enter_score() {
	var score=0;
	var curscore=this.name.match(/^score_(\d{1,3})$/)[1];
	var curel;
	
	for(var counter=20; counter<120; counter+=20) {
		score=(counter<=curscore?100:0);
		curel=document.getElementsByName('score_'+counter);
		if(curel && curel.length) {
			curel[0].src=curel[0].src.replace(/\d{1,3}/g,score);
		}
		
	}
}

function mouse_click_score(what) {
	var product=document.getElementsByName('wpshop_view_product');
	var request;
	
	product=(product.length?product[0].value:0);

	if(product) {
		request='./wp-content/plugins/wpshop/wpshop_rpc.php?procedure=productrating&product='+
				product+'&rating='+what.name.match(/^score_(\d{1,3})$/)[1];
		
		if(busy) {
			alert('The Ajax object is busy.  Please wait a few seconds and try again');
			return false;
		}
	
		busy=true;
		http.open('get',request,true);
		http.onreadystatechange=ajax_callback;
		http.send(null);
	
	}
	
	return false;
}

function mouse_leave_score() {
	var curel;
	
	for(var counter=0; counter<120; counter+=20) {
		curel=document.getElementsByName('score_'+counter);
		
		if(curel && curel.length) {
			curel[0].src=stars[curel[0].name];
		}
		
	}
}

function updaterating(full,half,count) {
	var ctrl,rating,counter,cur,base;

	if(ctrl=document.getElementById('ratingcount')) {
		ctrl.innerHTML='('+count+' '+(count==1?'rating':'ratings')+')';
	}

	//draw stars, remember to update stars array as well!

	for(counter=0; counter<5; counter++) {
		rating=(full-->0?100:0);
		rating=(rating?rating:(half-->0?50:rating));

		but='score_'+(counter+1)*20;
		
		if( (cur=document.getElementsByName(but)) && (cur.length)) {
			base=cur[0].src.match(/^(.*star_)\d{1,3}(.*)$/);
			cur[0].src=stars[but]=base[1]+rating+base[2];
		}
	}
}

function wait() { //improvised onLoad event handler

	var body=document.getElementsByTagName('body');

	if(body.length) {
		do_hook();
		return;
	}

	window.setTimeout('wait()',200);

	return;
}

function ajax_callback() {
	var answer;
	
	if(http && http.readyState==4) {

		busy=false;
		
		if(http.responseXML) {
			
			if( (answer=http.responseXML.getElementsByTagName('rpc')) && (answer.length)) {

				switch(answer[0].getAttribute('procedure')) {
					case 'productrating':
						if(answer[0].getAttribute('status')=='ok') {
							fullstars=answer[0].getAttribute('fullstars');
							halfstars=answer[0].getAttribute('halfstars');
							ratings=answer[0].getAttribute('ratings');
							alert('Thank you for your vote');
							updaterating(fullstars,halfstars,ratings);
						} else {
							switch(answer[0].getAttribute('error')) {
								case 'SOON':
									alert('Only one vote per product per month is allowed');
									break;
								case 'PRODUCT':
									alert('The product does not exist anymore!');
									break;
								default:
									alert('Uknown error'+' '+answer[0].getAttribute('error'));
									break;
							}
						}
				}

			}
		}

	}
}

