var ws={
	currentCategory:null,
	procfile : "wsproc.php",
	coid : 1,
	catid:null,
	cart:0,
	selitems:[],  //only used on initpage
	sendreq : function(strin){
		if (this.coid) strin+="&coid="+this.coid;
		scmain.sendreq(strin, this.procfile);
	},
	initpage : function(){
		sendstr="&fname=initpage";
		if (this.selitems){
			for(i in this.selitems){
				if (funcs.isNumeric(i)&&funcs.isNumeric(this.selitems[i])) sendstr+="&selitems["+i+"]="+this.selitems[i];
			}
			if (!(this.selitems[1])) sendstr+="&selitems[1]=1"; 
		}else sendstr+="&selitems[1]=1"; 
		if (this.cart==1) sendstr+="&cart=1";
		this.sendreq(sendstr);
	},
	changeqty:function(objin, price){
		if ((objin)&&objin.value&&funcs.isNumeric(objin.value))qty=objin.value;
		else qty=0;
		if (!(funcs.isNumeric(price))) price=0;
		if (tmpobj=document.getElementById("span_ws_total")){
			if (tmpobj) tmpobj.innerHTML=(qty*price).toFixed(2);
		}
	},
	addtocart:function(keyid){
		sendstr="&fname=addtocart";
		bgood=false;
		if (tmpobj=document.getElementById("sel_ws_qty")){
			if (tmpobj){
				sendstr+="&qty="+tmpobj.value
				if (tmpobj.value>0) bgood=true;
			}
		}
		elist=document.getElementsByTagName("select");
		for (i=0;i<elist.length;i++){
			if ((elist[i].getAttribute)&&elist[i].getAttribute("dbid")&&elist[i].getAttribute("name")=="tl_web_eqty"&&elist[i].value>0){
				sendstr+="&e["+elist[i].getAttribute("dbid")+"]="+elist[i].value;
				bgood=true;
			}
		}
		if (funcs.isNumeric(keyid)) sendstr+="&keyid="+keyid;
		if (bgood) this.sendreq(sendstr);
		else alert("Nothing to add/change, change any quantity higher.  If you are trying to delete an item, use the delete button on the cart.");
	},
	giftcert_check:function(){
		bgood=true;
		sendarr=['gc_from', 'gc_to', 'gc_message', 'gc_amount'];
		sendstr="&fname=giftcert_check";
		if (!funcs.checktxtr('gc_from', 'gc_to', 'gc_amount')) bgood=false;
		if (document.getElementById("gc_delivery_email").checked==true){
			sendstr+="&gc_delivery=email";
			if (!funcs.checktxtr('gc_rec_email')) bgood=false;
			$.merge(sendarr, ['gc_rec_email']);
		}else{
			sendstr+="&gc_delivery=postal";
			if (!funcs.checktxtr('gc_rec_firstname', 'gc_rec_lastname', 'gc_rec_address', 'gc_rec_city', 'gc_rec_zip')) bgood=false;
			$.merge(sendarr, ['gc_rec_firstname', 'gc_rec_lastname', 'gc_rec_address', 'gc_rec_city', 'gc_rec_state', 'gc_rec_country', 'gc_rec_zip']);
		}
		if (bgood==false){document.getElementById("gc_error").innerHTML='Make sure all the required values are filled in';return;}
		for(i in sendarr){
			sendstr+="&gci["+sendarr[i].substring(3)+"]="+escape(document.getElementById(sendarr[i]).value);
		}
		this.sendreq(sendstr);
	},
	addWebMiscItem:function(){
		bgood=true;
		inputlist=document.getElementsByTagName("input");
		selectlist=document.getElementsByTagName("select");
		sendstr="";
		wmitypeid=0;
		if (tmpobj=document.getElementById("wmi_typeid")){
			if (tmpobj) wmitypeid=tmpobj.value;
		}
		for(i in inputlist){
			if (inputlist[i]&&inputlist[i].getAttribute&&inputlist[i].getAttribute("mytype")&&inputlist[i].getAttribute("mytype")=="wmi_op"&&inputlist[i].getAttribute("myid")){
				sendstr+="&wmi_op["+inputlist[i].getAttribute("myid")+"]="+escape(inputlist[i].value);
			}
		}
		for(i in selectlist){
			if (selectlist[i]&&selectlist[i].getAttribute&&selectlist[i].getAttribute("mytype")&&selectlist[i].getAttribute("mytype")=="wmi_op"&&selectlist[i].getAttribute("myid")){
				sendstr+="&wmi_op["+selectlist[i].getAttribute("myid")+"]="+escape(selectlist[i].value);
			}
		}
		if (sendstr!=""&&wmitypeid!=0) this.sendreq("&fname=addWebMiscItem&typeid="+wmitypeid+sendstr);
		//else alert(sendstr+" "+wmitypeid);
	},
	wmi_op_addprice:0,
	wmi_changeop:function(objin){
		if (tmpobj=document.getElementById("wmi_price")){
			if (tmpobj){
				if (tmpobj.getAttribute&&tmpobj.getAttribute("price")){
					price=parseFloat(tmpobj.getAttribute("price"));
					if (ws.wmi_op_addprice!=0){
						price-=ws.wmi_op_addprice;
						ws.wmi_op_addprice=0;
					}
					if (objin.options[objin.selectedIndex].getAttribute("addprice")){
						addprice=objin.options[objin.selectedIndex].getAttribute("addprice");
						if (!isNaN(parseFloat(addprice))) price+=parseFloat(addprice);
						if (!isNaN(price)){
							ws.wmi_op_addprice=parseFloat(addprice);
						}
					}
					tmpobj.innerHTML=price.toFixed(2);
					tmpobj.setAttribute("price",price);
				}
			}
		}
	},
	scrollimgs:function(intlength){
		if (tmpobj=document.getElementById("scroll_suggest")){
			if (tmpobj&&tmpobj.getAttribute){
				xpos=tmpobj.getAttribute("xpos");
				xpos=parseInt(intlength)+parseInt(xpos);
				scrollborder=document.getElementById("scroll_border");
				//if future xpos is either too far left or right, it won't scroll.
				if (xpos<=0&&xpos>=(-(tmpobj.offsetWidth-(scrollborder.offsetWidth-100)))){
					//store the new value in an attribute in case clicks happen while animate is still going
					tmpobj.setAttribute("xpos", xpos);
					//if jquery acts up, just move the comment to the jquery line
					$("#scroll_suggest").animate({marginLeft:xpos+'px'}, 200);
					//tmpobj.style.marginLeft=xpos+'px';
				}
			}
		}
	},
	delitem:function(keyid){
		if (confirm("Are you sure you want to remove this item?")) this.sendreq("&fname=removecartitem&keyid="+keyid);
	},
	sendSel:function(webpage, poststr){
		myform=document.createElement("form");
		myform.action=webpage;
		myform.method='post';
		myform.id="ws_selform";
		myinput=document.createElement("input");
		myinput.type="hidden";
		myinput.setAttribute("name","selarr");
		myinput.value=poststr;
		myform.appendChild(myinput);
		document.body.appendChild(myform);
		myform.submit();
	},
	showcvhelp:function(){
		if (cvobj=document.getElementById("divcvhelp")){
			if (cvobj){
				if (cvobj.style.display==''){
					cvobj.style.display='none';
				}else{
					cvobj.style.display='';
				}
			}
		}
	},
	chkCODetails:function(){
		sendstr="";
		if (tmpobj=document.getElementById("country")){
			if (tmpobj) sendstr+="&country="+tmpobj.value;
		}
		if (tmpobj=document.getElementById("ship_1")){
			if (tmpobj&&tmpobj.checked==true) sendstr+="&ship=1";
		}
		if (tmpobj=document.getElementById("ship_2")){
			if (tmpobj&&tmpobj.checked==true) sendstr+="&ship=2";
		}
		if (tmpobj=document.getElementById("ship_3")){
			if (tmpobj&&tmpobj.checked==true) sendstr+="&ship=3";
		}
		if (tmpobj=document.getElementById("txtsstate")){
			if (tmpobj) sendstr+="&state="+tmpobj.value;
		}
		if (sendstr!="") this.sendreq("&fname=chkCODetails"+sendstr);
	},
	addExtra:function(cartkey, extraid, qty){
		this.sendreq("&fname=addExtra&cartkey="+cartkey+"&extraid="+extraid+"&qty="+qty);
	},
	submitCheckout:function(){
		this.coerr("");
		if (funcs.checktxtr("txtfirstname", "txtlastname", "txtphone", "txtemail", "txtsaddress", "txtscity", "txtsstate", "txtszip", "selcardtype", "txtccname", "txtccnumber", "txtccexp", "txtcccvv")){
			mycardtype=document.getElementById("selcardtype").value;
			mycardnum=document.getElementById("txtccnumber").value.replace(/ -/g,"");
			if (ws.validateCC(mycardnum, mycardtype)){
				sendstr="";
				elist=null;
				if (document.getElementsByTagName){
					elist=document.getElementsByTagName("*");
				}else if (document.all) elist=document.all;
				for(i in elist){
					if (elist[i]&&elist[i].getAttribute&&elist[i].getAttribute("chkdata")&&(elist[i].value)){
						sendstr+="&d["+elist[i].getAttribute("chkdata")+"]="+escape(elist[i].value);
					}
				}
				var shipval=1;
				if (tmpobj=document.getElementById("ship_2")){
					if (tmpobj&&tmpobj.checked==true) shipval=2;
				}
				if (tmpobj=document.getElementById("ship_3")){
					if (tmpobj&&tmpobj.checked==true) shipval=3;
				}
				
				sendstr+="&ship="+shipval;
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				var address = document.getElementById("txtemail").value;
				if(reg.test(address) == false){
					alert('Invalid Email Address');
					return false;
				}
				if (sendstr){
					funcs.createwin("tmpwin", "<div style='text-align:center;font:bold 13px arial;padding:10px;'><img src='images/loader.gif' alt=''><br><br>Processing, please wait..</div>", "Processing", 10, 0, 0, "#232588", 400, false);
					this.sendreq("&fname=checkout"+sendstr);
				}
			}else this.coerr("Invalid credit card number. Please check the number and try again.");
		}else this.coerr("Please fill out the required fields that are marked in red");
	},
	coerr:function(strin){
		if (tmpobj=document.getElementById("coerr")){
			if (tmpobj){
				document.getElementById("coerr").innerHTML=strin;
			}
		}
	},
	isDigit:function(c){
		var strAllowed = "1234567890";
		return (strAllowed.indexOf (c) != -1);
	},
	validateCC:function (strNum, type){
		var nCheck = 0;
		var nDigit = 0;
		var bEven = false;
		for (n = strNum.length - 1; n >= 0; n--) {
			var cDigit = strNum.charAt (n);
			if (this.isDigit (cDigit)){
				var nDigit = parseInt(cDigit, 10);
				if (bEven){
					if ((nDigit *= 2) > 9) nDigit -= 9;
				}
				nCheck += nDigit;
				bEven = ! bEven;
			}else if (cDigit != ' ' && cDigit != '.' && cDigit != '-')return false;
		}
		if ((nCheck % 10) != 0) return false;
		var nLen = 0;
		for (n = 0; n < strNum.length; n++){
			if (this.isDigit (strNum.substring (n,n+1))) ++nLen;
		}
		if (type == 'Visa')
			return ((strNum.substring(0,1) == '4') && (nLen == 13 || nLen == 16));
		else if (type == 'Amex')
			return ((strNum.substring(0,2) == '34' || strNum.substring(0,2) == '37') && (nLen == 15));
		else if (type == 'MasterCard')
			return ((strNum.substring(0,2) == '51' || strNum.substring(0,2) == '52'
              || strNum.substring(0,2) == '53' || strNum.substring(0,2) == '54'
              || strNum.substring(0,2) == '55') && (nLen == 16));
		else if (type == 'Discover')
			return ((strNum.substring(0,4)=="6011"||strNum.substring(0,2)=="65")&&nLen==16);
		else
			return false;
	},
	submitForm:function(strid){
		if (tmpobj=document.getElementById(strid)){
			if (tmpobj){
				this.sendreq("&fname=submitForm&id="+strid+funcs.getquerystring(tmpobj));
				if (tmpobj=document.getElementById(strid+"_submit")){
					if (tmpobj){
						tmpobj.disabled=true;
						tmpobj.value="Sending..";
					}
				}
			}
		}
	}
};

