window.onload = prepareLinks;

function showSelect(themenu){
	var el = document.getElementsByClassName("smenu");
	for(i=0; i<el.length; i++){
		el[i].style.display = 'none';
	}
	var opt = themenu.options[themenu.selectedIndex].value;
	var el = document.getElementById(opt);
	el.style.display = 'block';	
	return false;

}

function CshowSelect(themenu){
	var el = document.getElementsByClassName("cmenu");
	for(i=0; i<el.length; i++){
		el[i].style.display = 'none';
	}
	var opt = themenu.options[themenu.selectedIndex].value;
	var el = document.getElementById(opt);
	el.style.display = 'block';	
	return false;

}


function mainImage(){
	var r = Math.round(Math.random()*4)+1;
	var mainimage = $('mainimage');
	mainimage.src = "/images/home_mainpic_" + r + ".jpg";
}

function emailedResponse(originalRequest){
	var response = originalRequest.responseText;
	var joinlist = $("joinlist");
	joinlist.innerHTML = "<div id=\"response\">" + response + "</div>";
}

function prepareLinks(){

	var links = document.getElementsByClassName("zoom");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "zoom", "status=no,scrollbars=no,width=650,height=540");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("joinbutton");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			var emailfield = document.getElementsByClassName("joinlistemail");
			var email = emailfield[0].value;
			if(email != "your email" && email != ""){
				var vars = "email=" + email;
				var myAjax = new Ajax.Request(
					'/ajax_joinlist.php',
					{method: 'post', parameters: vars, onComplete: emailedResponse}
				);
			}
			return false;
		}
	}
	
	var links = document.getElementsByClassName("shipping");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "shipping", "status=no,scrollbars=yes,width=600,height=500");
			return false;
		}
	}

	var links = document.getElementsByClassName("newsurl");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "newslink", "status=yes");
			return false;
		}
	}

	var links = document.getElementsByClassName("news");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "news", "status=no,scrollbars=yes,width=620,height=450");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("copyright");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "copyright", "status=no,scrollbars=yes,width=470,height=280");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("coolkidpop");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "coolkid", "status=no,scrollbars=no,width=325,height=410");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("coolestkid");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "coolestkid", "status=no,scrollbars=yes,width=340,height=680");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("privpol");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "privpol", "status=no,scrollbars=yes,width=470,height=320");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("contest");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "contestreg", "status=no,scrollbars=yes,width=470,height=320");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("forgotpw");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "forgotpw", "status=no,scrollbars=yes,width=440,height=220");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("buyerlogin");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "buyerlogin", "status=no,scrollbars=no,width=360,height=252");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("pressdetail");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "pressdetail", "status=no,scrollbars=yes,resizable=yes,width=610,height=580");
			return false;
		}
	}
	

	var links = document.getElementsByClassName("showphoto");
	for(i=0; i<links.length; i++){
		links[i].onmouseover = function(){
			var frame = $("frame");
			frame.style.background = "url("+this.title+") no-repeat center center";
			return false;
		}
		links[i].onclick = function(){
			window.open(this.href, "zoom", "status=no,scrollbars=no,width=750,height=640");
			return false;
		}
	}	
	
	var links = document.getElementsByClassName("swatchthumb");
	for(i=0; i<links.length; i++){
		links[i].onmouseover = function(){
			var swatchname = $("swatchname");
			swatchname.firstChild.nodeValue = this.title;
		}
		links[i].onclick = function(){
			window.open(this.href, "zoom", "status=no,scrollbars=no,width=750,height=640");
			return false;
		}
	}
	
	var links = document.getElementsByClassName("pressshow");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			var photo = $("photo");
			photo.src = this.href;
			return false;
		}
	}	
	
	var links = document.getElementsByClassName("enlarge");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.open(this.href, "zoom", "status=no,scrollbars=no,width=750,height=640");
			return false;
		}
	}	
	var links = document.getElementsByClassName("thumbp");
	for(i=0; i<links.length; i++){
		links[i].onmouseover = function(){
			var feature = $("feature");
			feature.style.background = "url(/prod_images/shop_" + this.id + ".jpg) no-repeat center center";
			var el = document.getElementsByClassName("desc");
			for(i=0; i<el.length; i++){
				el[i].style.display = 'none';
			}
			var el = document.getElementById(this.title);
			el.style.display = 'block';				
			return false;
		}
		links[i].onclick = function(){
			window.open( "/prod/" + this.id, "_self");
			return false;
		}
	}	

	var links = document.getElementsByClassName("stylep");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			var feature = $("feature");
			feature.style.background = "url(/styleguide/sg_" + this.id + "-large.jpg) no-repeat center center";
			var el = document.getElementsByClassName("sg");
			for(i=0; i<el.length; i++){
				el[i].style.display = 'none';
			}
			var el = document.getElementById(this.title);
			el.style.display = 'block';
			
			return false;
		}
	}
	var links = document.getElementsByClassName("closewindow");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			window.close();
		}
	}

	var button = $("addtobag");
	if(button){
		button.onclick = function(){
			
			
			var errormsg = "";
			var fields = document.getElementsByClassName("requiredoption");
			var fieldsQ = document.getElementsByClassName("requiredqty");
			
			for(i=0; i<fields.length; i++){
			if(fields[i].value != "0" && fieldsQ[i].value == ""){ errormsg = "You need to enter a quantity.\n"; }
			if(fields[i].value == "0" && fieldsQ[i].value != ""){ errormsg = errormsg + "You need to choose a category.\n"; }
			} 
			
			var fields = document.getElementsByClassName("optionaloption");
			var fieldsQ = document.getElementsByClassName("optionalqty");
			
			for(i=0; i<fields.length; i++){
			if(fields[i].value != "0" && fieldsQ[i].value == ""){ errormsg = errormsg + "You need to enter a quantity.\n"; }
			if(fields[i].value == "0" && fieldsQ[i].value != ""){ errormsg = errormsg + "You need to choose a category.\n"; }
			} 
			
			var fields = document.getElementsByClassName("requiredoption");
			var fieldsQ = document.getElementsByClassName("requiredqty");
			var fieldsO = document.getElementsByClassName("optionaloption");
			var fieldsOQ = document.getElementsByClassName("optionalqty");
			for(i=0; i<fields.length; i++){
			if(fields[i].value == "0" && fieldsQ[i].value == "" && fieldsO[i].value == "0" && fieldsOQ[i].value == ""){ errormsg = errormsg + "You need to enter a quantity.\n"; }
			} 
			
//			for(i=0; i<fields.length; i++){
//				if(fields[i].value == "0" && fieldsOQ[i].value == "0" && fieldsOO[i].value == "0"){ errormsg = "You need to make a selection for each option.\n"; }
//			} 


//			var fields = document.getElementsByClassName("requiredqty");
//			for(i=0; i<fields.length; i++){
//				if(fields[i].value == 0){ errormsg = errormsg + "You need to enter a quantity.\n"; }
//			}
//			var fields = document.getElementsByClassName("optionalqty");
//			for(i=0; i<fields.length; i++){
//				if(fields[i].value > 0){
//					var fields = document.getElementsByClassName("optionaloption");
//					for(i=0; i<fields.length; i++){
//						if(fields[i].value == "0"){ errormsg = errormsg + "You need to make a selection for each option if you enter a quantity.\n"; }
//					}
//				}
//			}
			if(errormsg){ alert(errormsg); return false; }
		}
	}
	
	var links = document.getElementsByClassName("useshipping");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			fillbillinginfo();
			return false;
		}
	}

}


