//product dropdowns: select color, populates size, select size, populates quant
function setSize(val){
	size = document.getElementById('size');
	size.options.length = 0;
	j = 0;
	for(var i=0; i<prodArr.length; i++){
		if(prodArr[i][0]==val){//if color selection = color in array, set size to to corresponding size
			size.options[j] = new Option (prodArr[i][1],prodArr[i][1]);
			j++;
		}
	}
	if(size.options.length !=0){
	 setAmt(val, size[size.selectedIndex].value);
	 } else {
		document.getElementById('amt').options.length = 0;
	 }
}

//now we have both color and size, populate quantity with amounts in stock and also set the invid hidden variable and the invid var for the wishlist link
function setAmt(color, size){
	amt = document.getElementById('amt');
	amt.options.length = 0;//reset the amount to 0
	for(var i=0; i<prodArr.length; i++){
		if(prodArr[i][0]==color && prodArr[i][1]==size){
			var j = prodArr[i][2];//the amount in stock
			document.getElementById('invid').value= prodArr[i][3];//sets the invid of the hidden field
			document.getElementById('wish').href += '?item='+prodArr[i][3];
			//if(j==0 && prodArr[i][4]==1){//check if amt=0 and mto=1, if true then set the mto div to display:block, else set it to display:none
			//	document.getElementById('mto').style.display = 'block';
			//	j=9;
			//} else {
			//	document.getElementById('mto').style.display = 'none';
			//}
		}
	}	

	for(var i=0; i<j; i++){//create an option for each amount - ie 1,2,3
		amt.options[i] = new Option (i+1,i+1);
	}
	if(j==0){//if none in stock and mto not true, set quantity to 'not in stock'
		amt.options[0] = new Option('Out of stock','');
	}
}

function prodInfo(id){
	var tabs = new Array('descript','story','shipping');
	for(var i=0; i<tabs.length; i++){
		if(tabs[i]==id){
			document.getElementById(tabs[i]).style.display = 'block';
			document.getElementById(tabs[i]+'_li').className = 'active';
		} else{
			document.getElementById(tabs[i]).style.display = 'none';
			document.getElementById(tabs[i]+'_li').className = 'inactive';
		}
	}
}	

function popup(url,w,h){
	winpops=window.open(url,"","width="+w+",height="+h+",")
}

function showImg(key,length){
	for(var i=0; i<length; i++){
		document.getElementById('img'+i).style.display = 'none';
		if(i==key){
			document.getElementById('img'+i).style.display = 'block';
		} 
	}
}

function showHide(show, hide){
	var showArr = new Array();
	var hideArr = new Array();
	var showArr = show.split(',');
	var hideArr = hide.split(',');
	for(var i=0; i<hideArr.length; i++){
		if(hideArr[i]) document.getElementById(hideArr[i]).style.display = 'none';
	}
	for(var j=0; j<showArr.length; j++){
		if(showArr[j]) document.getElementById(showArr[j]).style.display = 'block';
	}
}

function view(id, indicator, inum){
	if(indicator == 'on'){
		id.innerHTML = '-';
		id.onclick=new Function("view(this, 'off', '"+inum+"')");
		document.getElementById('comments'+inum).style.display = 'block';
	} else {
		id.innerHTML = '+';
		id.onclick=new Function("view(this, 'on', '"+inum+"')");
		document.getElementById('comments'+inum).style.display = 'none';
		document.getElementById('addComment'+inum).style.display = 'none';
	}
}

function formCheck(inputs, email, zip){
var bool;
bool = true;
	if(inputs){
		inputArr = new Array();
		inputArr = inputs.split(',');
		for(i=0; i<inputArr.length; i++){
			if(document.getElementById(inputArr[i]).value==""){
				document.getElementById(inputArr[i]).style.background = '#ffffcc';
				document.getElementById(inputArr[i]+"_err").style.display = 'block';
				bool = false; 
			} else{
				document.getElementById(inputArr[i]).style.background = '#fff';
				document.getElementById(inputArr[i]+"_err").style.display = 'none';
			}
		}
	}
	if(email){
		val = document.getElementById(email).value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(val)){ 
			document.getElementById(email+"_err").style.display = 'none';
			document.getElementById(email).style.background = '#fff';
			} else {
				document.getElementById(email).style.background = '#ffffcc';
				document.getElementById(email+'_err').style.display="block";
				bool= false;
		} 
	}
	return bool;
}

function passCheck(){
	pwd = document.getElementById('pwd').value;
	confirm = document.getElementById('confirm').value
	if( pwd != confirm ){
		document.getElementById('confirm').value = '';
		document.getElementById('confirm2_err').style.display = 'block';
		return false;
	} 
}

function shipping(val){
	switch(true){
		case val == "Standard" || val=="UNITED STATES":
			showHide('standard,express','canada,intl');
			setRadio('stanRad');
			break;
		case val == "Express":
			showHide('standard,express','canada,intl');
			setRadio('expRad');
			break;			
		case val == "Canada" || val=="CANADA":
			showHide('canada','standard,express,intl');
			setRadio('canRad');
			break;
		default :
			showHide('intl','standard,express,canada');
			setRadio('intlRad');
			break;
	}
}

function setRadio(id){
	document.getElementById(id).checked = true;
}

function modShip(valu){
string = valu.split(",");
meth = string[1];
rate = parseFloat(string[0]);

window.opener.document.getElementById('shipRate').innerHTML = rate.toFixed(2);
window.opener.document.getElementById('meth').innerHTML = meth;
window.opener.document.getElementById('shipMeth').value = meth;
window.opener.document.getElementById('gTotal').innerHTML = (rate+parseFloat(window.opener.document.getElementById('subT').innerHTML)).toFixed(2);

window.close();
}

//display error field if customer doesn't input all required info
function validate(){
	bool = true;	
		for(j=0;j<2;j++){
			if(j==0)var el= 'b';
			if(j==1)var	el= 's';
			for (i=1;i<8;i++){
				if(document.getElementById(el+i).value==""){
					document.getElementById(el+i+'_err').style.display = 'block';
					bool = false;
				} else {
					document.getElementById(el+i+'_err').style.display = 'none';
				}
			}
		}
	verifyEmail(document.getElementById('email').value, document.getElementById('email_err'));
	if (bool==false){
		window.scrollTo(0,130);
	} else if(document.getElementById('paypal').checked == false) {
		Mod10(document.getElementById('cNum').value);
	}
	return bool;
}


function verifyEmail(e, err){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(e)){ 
		return true;
		} else { 
			err.style.display="block";
			bool = false;
			return false;
	}
}

//copy billing text fields to shipping text fields
function FillForm(){
	if (document.frm.rad.checked){
		for (var i=1; i<9; i++) {
		document.frm["s"+i].value=document.frm["b"+i].value;
		} 
	}
	if (!document.frm.rad.checked){
		for (var i=1; i<9; i++) {
			document.frm["s"+ i].value = '';
		}
	}
}

function makeWish(){	
	for(var i in prod){
		wDiv = document.createElement('div');
		wDiv.className = 'wishItem';
		document.getElementById('wish').appendChild(wDiv);
		
		img = new Image(70,93);
		img.src = 'prod_images/'+prod[i].Img;
		wDiv.appendChild(img);
		
		descrip = document.createElement('div');
		descrip.className = 'descrip';
		wDiv.appendChild(descrip);
		
		title = document.createElement('h5');	
		title.innerHTML = prod[i].Genre+' '+prod[i].Prodname;	
		descrip.appendChild(title);
		
		prodText = document.createElement('p');
		prodText.innerHTML = prod[i].Descrip;
		descrip.appendChild(prodText);

		prodLink = document.createElement('a');
		prodLink.href= 'products.'+prod[i].Genre+'-'+prod[i].Prodname;
		prodLink.innerHTML = 'Product Link';
		descrip.appendChild(prodLink);
		
		avail = document.createElement('div');
		avail.className = 'avail';
		avail.innerHTML = "Availability: <span class='"+prod[i].aColor+"'>"+prod[i].Avail+"</span>";
		descrip.appendChild(avail);
		
		pForm = document.createElement('form');
		pForm.method = 'post';
		pForm.action = 'cart.php';
		descrip.appendChild(pForm);
		
		price = document.createElement('input');
		price.setAttribute("type", "hidden");
		price.setAttribute("name", "price");
		price.setAttribute("value", prod[i].Price);
		pForm.appendChild(price);
		
		genre = document.createElement('input');
		genre.setAttribute("type", "hidden");
		genre.setAttribute("name", "genre");
		genre.setAttribute("value", prod[i].Genre);	
		pForm.appendChild(genre);
		
		color = document.createElement('input');
		color.setAttribute("type", "hidden");
		color.setAttribute("name", "color");
		color.setAttribute("value", prod[i].Color);
		pForm.appendChild(color);
		
		prodname = document.createElement('input');
		prodname.setAttribute("type", "hidden");
		prodname.setAttribute("name", "prodname");
		prodname.setAttribute("value", prod[i].Prodname);
		pForm.appendChild(prodname);
		
		size = document.createElement('input');
		size.setAttribute("type", "hidden");
		size.setAttribute("name", "size");
		size.setAttribute("value", prod[i].Size);
		pForm.appendChild(size);
		
		invid = document.createElement('input');
		invid.setAttribute("type", "hidden");
		invid.setAttribute("name", "invid");
		invid.setAttribute("value", prod[i].Invid);	
		pForm.appendChild(invid);
		
		submit = document.createElement('input');
		submit.setAttribute("type","image");
		submit.setAttribute("src","images/add_to_cart.gif");
		pForm.appendChild(submit);
		
		rm = document.createElement('a');
		rm.href = '?delete='+prod[i].Invid;
		rm.innerHTML = 'Remove Item';
		pForm.appendChild(rm);
		
		clrDiv = document.createElement('div');
		clrDiv.className = 'clear';
		descrip.appendChild(clrDiv);
		
		pDiv = document.createElement('div');
		pDiv.className = 'price';
		wDiv.appendChild(pDiv);
		
		priceDiv = document.createElement('div');
		priceDiv.innerHTML = 'Price $'+prod[i].Price;
		pDiv.appendChild(priceDiv);
		
		priceFrm = document.createElement('form');
		priceFrm.method = 'get';
		priceFrm.action = 'wishlist.php';
		priceFrm.setAttribute('id', 'priceFrm'+i);
		if(prod[i].Priority==1){
			priceFrm.innerHTML = "Priority:<select name='priority' onchange='document.getElementById(\"priceFrm"+i+"\").submit();'><option value=1>1. I need this!</option><option value=2>2. I'm thinking about it</option><option value=3>3. Eh, we'll see</option></select>";			
			}else if(prod[i].Priority==2){
			priceFrm.innerHTML = "Priority:<select name='priority' onchange='document.getElementById(\"priceFrm"+i+"\").submit();'><option value=1>1. I need this!</option><option value=2 selected>2. I'm thinking about it</option><option value=3>3. Eh, we'll see</option></select>";			
			}else{
			priceFrm.innerHTML = "Priority:<select name='priority' onchange='document.getElementById(\"priceFrm"+i+"\").submit();'><option value=1>1. I need this!</option><option value=2 selected>2. I'm thinking about it</option><option value=3 selected>3. Eh, we'll see</option></select>";			
		}	
		pDiv.appendChild(priceFrm);
		
		invid2 = document.createElement('input');
		invid2.setAttribute("type", "hidden");
		invid2.setAttribute("name", "invid");
		invid2.setAttribute("value", prod[i].Invid);	
		priceFrm.appendChild(invid2);
		
		clrDiv2 = document.createElement('div');
		clrDiv2.className = 'clear';
		pDiv.appendChild(clrDiv2);
	}
}

function wishSort(sortBy){
//remove all div's with classname 'wishItem' b/c we will be rebuilding that content in new order
	if(prod){
		var divObj = new Object;
		var divs = document.getElementsByTagName('div');
		for(var i = 0; i<divs.length; i++){
			while(divs[i].className == 'wishItem'){
				divs[i].parentNode.removeChild(divs[i]);
			}
		}
		switch (sortBy) {
			case 'priority' :
				prod.sort(sortPriority);
				makeWish();
				break;
			case 'recent' :
				prod.sort(sortNewest);
				makeWish();
				break;
			case 'low' :
				prod.sort(sortLowest);
				makeWish();
				break;
			case 'high' :
				prod.sort(sortHighest);
				makeWish();
				break;
		}
	}
}

function sortPriority(a,b){
	var x = a.Priority;
	var y = b.Priority;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortNewest(a,b){
	var x = a.Order;
	var y = b.Order;
	return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}

function sortHighest(a,b){
	var x = a.Price;
	var y = b.Price;
	return ((x > y) ? -1 : ((x < y) ? 1 : 0));	
}

function sortLowest(a,b){
	var x = a.Price;
	var y = b.Price;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));	
}

function fckEditMode(editor){
	// This is a hack for Gecko... it stops editing when the editor is hidden.
	if ( !document.all ){
		var oEditor = FCKeditorAPI.GetInstance(editor) ;
		
		if (  oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
			oEditor.MakeEditable() ;
	}
}

function bookmark(title,url){
	if (window.sidebar){ // firefox
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all) {// ie
		window.external.AddFavorite(url, title);
	}
}

function updateTime(){
	timeCount++;
	var recur = setTimeout("updateTime()", 1000);
}

function setCookie(){
	document.cookie = "timeCookie="+timeCount;
}

function setCounter(){
	el = document.getElementById('countDown');
	val = parseInt(el.innerHTML);
	if(val > 0){
		el.innerHTML = val-1;
		countDown();
	} else {
		window.location = "http://www.FetishBuzz.com/"
	}
}

function countDown(){
	var t = setTimeout("setCounter();", 1000);
}
