
// if( Drupal.jsEnabled){
	$(document).ready(function(){


			if( existingName == "-1"){
				$("#deleteButton").hide();
			}

			$("#help").click(function(){
				$("#instructions").toggle();
			});

			$("#clearButton").click(function(){
				window.location = "/carbo/index.cfm";

			});

			$("#saveButton").click(function(){

				if( uid == 0){
					alert("Please login to be able to save your custom carbohydrate.");
				}else{
					if( existingName != "-1"){
						var name=prompt("Please enter name", existingName);
					}else{
						var name=prompt("Please enter name","My sugar");
					}
					if (name!=null && name!="")
					  {

						var locationStr = "process.cfm" + "?action=save&title=" + name +"&sq0=" + (squares[0].sub+1);

						for(i=1;i<squares.length;i++){
							if( squares[i].group != -1 ){
								locationStr = locationStr + "&sq"+i+"="+(squares[i].group+1) + "," + (squares[i].sub+1) + "," + squares[i].branched;
							}

						}
						// add user notes if any
					var notes = jQuery.trim(document.getNotes.notes.value);
					if(notes.length ==0){
						locationStr = locationStr + "&notes=-1";
					}else{
						locationStr = locationStr + "&notes=" + document.getNotes.notes.value;
					}
					locationStr = locationStr.replace(/\s+/g,"%20");
						window.location=locationStr;
					  }
				}

			});

			$("#saveToCartButton").click(function(){ //  note: not addToCart

				var locationStr = "cart.cfm?sugN="+(sugN+1)+"&action=modify&sq0=" + (squares[0].sub);
					for(i=1;i<squares.length;i++){
						if (squares[i].group != -1) {
							locationStr = locationStr + "&sq" + i + "=" + (squares[i].group+1) + "," + (squares[i].sub+1) + "," + squares[i].branched;
						}
					}
					// add user notes if any
					var notes = jQuery.trim(document.getNotes.notes.value);
					if(notes.length ==0){
						locationStr = locationStr + "&notes=-1";
					}else{
						locationStr = locationStr + "&notes=" + document.getNotes.notes.value;
					}
					locationStr = locationStr.replace(/\s+/g,"%20");
					window.location=locationStr;
			});

			$("#deleteButton").click(function(){
				if( existingName != "-1"){
					var locationStr = "delete.cfm" + "?title=" + existingName;
					locationStr = locationStr.replace(/\s+/g,"%20");
					window.location=locationStr;
				}
			});


			$("#addToCartButton").click(function(){

				var currentLoc = window.location.href.replace(/\?.*/,'');
				var locationStr = currentLoc + "?action=add&sq0=" + (squares[0].sub+1);
					for(i=1;i<squares.length;i++){
						if( squares[i].group != -1 ){
							locationStr = locationStr + "&sq"+i+"="+(squares[i].group+1) + "," + (squares[i].sub+1) + "," + squares[i].branched;
						}
					}
					// add user notes if any
					var notes = jQuery.trim(document.getNotes.notes.value);
					if(otherSugarIsChosen() && notes.length ==0){ // if user chose other sugar and didn't input any notes
							alert("Please tell us more about the 'other' sugars you have chosen.");
					}else{
						if(notes.length ==0){
							locationStr = locationStr + "&notes=-1";
						}else{
							locationStr = locationStr + "&notes=" + document.getNotes.notes.value;
						}
					locationStr = locationStr.replace(/\s+/g,"%20");
					window.location=locationStr;
					}
			});

			$("#cancelButton").click( function(){
				$("#tablePopUp").hide();
				$("#tableChooseGroup").hide();
				unHighlightSqaure();
			});
			
			$("#cancelButton2").click( function(){
				$("#tablePopUp").hide();
				$("#tableChooseGroup").hide();
				unHighlightSqaure();
			})

			$("#calcButton").click(function(){

				var currentLoc = window.location.href.replace(/\?.*/,'');
				if(action != -1){
					var locationStr = currentLoc + "?action=" + action + "&sugN=" + (sugN) + "&sq0=" + (squares[0].sub);
				}else{
					var locationStr = currentLoc + "?sq0=" + (squares[0].sub);
				}

				for(i=1;i<squares.length;i++){
					if( squares[i].group != -1 ){
						locationStr = locationStr + "&sq"+i+"="+(squares[i].group+1) + "," + (squares[i].sub+1) + "," + squares[i].branched;
					}

				}

				window.location=locationStr;
			});



	});
 //}