// JavaScript Document
var numWinesInBasket = 0

/*************************
 *      div as window     *
 *************************/

function closeWindow(obj){
	$(obj).parentNode.removeChild($(obj));
}

function openWindow(windowAddress, windowName){
	if($(windowName)){
		$(windowName).parentNode.removeChild($(windowName));
	}
	openWindows = $('content').childNodes
	newDiv = document.createElement('div')
	newDiv.id = windowName
	newDiv.style.border= "3px inset grey"
	newDiv.style.background = "#FFFFFF"
	newDiv.appendChild(document.createTextNode('Loading...'))
	document.body.appendChild(newDiv)
	newDiv.style.position = "absolute"
	newDiv.style.top = "60px"
	newDiv.style.left = "10px"
	new Ajax.Updater(newDiv, windowAddress, {asynchronous:true, evalScripts:true});
}




/*************************
 *    Form Validation    *
 *************************/

	 function validateLoginForm(){
        if ($('user_name').value == ""){
       	 alert("Please enter your user name.")
       	 return false;
        }
		if ($('password').value == ""){
       	 alert("Please enter your password.")
       	 return false;
        }
		return true;
	 }
	 
	 
	 
	function checkPasswords(){
		password = $('newPassword');
        password2 = $('newPassword2');
        password_alert_span = $('password_alert');
        password_alert_text = document.createTextNode("Your passwords don't match.");

        if (password.value == password2.value){
			if (password_alert_span.firstChild != null){
            	rem = password_alert_span.firstChild;
                password_alert_span.removeChild(rem);
            }
            return "passmatch";
        }
        else{
        	if (password_alert_span.firstChild != null){
				
            	rem = password_alert_span.firstChild;
                password_alert_span.removeChild(rem);
            }
            password_alert_span.appendChild(password_alert_text);
            return 'passnomatch';
        }
        
    }
	
	
	
    
    function checkUserName(){
        user_name= $('user_name');
		if (navigator.appName=="Netscape"){
			user_name_alert = $('user_name_alert');
		}
      
        user_name_alert_text = document.createTextNode("That user name is not available.  Please choose another.");  
        new Ajax.Request(('/accounts/check_user_name?user_name='+$('user_name').value), {onSuccess: 
       		function(request){ 
				if(request.responseText=="in_use"){
					if (user_name_alert.firstChild != null){
              		 	user_name_alert.removeChild(user_name_alert.firstChild);
					}
                    user_name_alert.appendChild(user_name_alert_text);
					return "nogo"
                }
                else{
                	if (user_name_alert.firstChild != null){
            			rem = user_name_alert.firstChild;
              		 	user_name_alert.removeChild(rem);
            		}
					return "go"	
                }
             }});
    }
	
	
	
	
    
    function validateNewAccountForm(){
        if ($('first_name').value == ""){
       	 alert("Please enter your first name.")
       	 return false;
        }
		 if ($('last_name').value == ""){
       	 alert("Please enter your last name.")
       	 return false;
        }
		if ($('address').value == ""){
       	 alert("Please enter your address.")
       	 return false;
        }
		if ($('city').value == ""){
       	 alert("Please enter your city.")
       	 return false;
        }
		if ($('state').value == ""){
       	 alert("Please enter your state.")
       	 return false;
        }
		if ($('zip').value == ""){
       	 alert("Please enter your zip code.")
       	 return false;
        }
		if ($('phone').value == ""){
       	 alert("Please enter your phone number.")
       	 return false;
        }
		if ($('email').value == ""){
       	 alert("Please enter your email address.")
       	 return false;
        }
		if ($('userName').value == ""){
       	 alert("Please choose a user name.")
       	 return false;
        }

       	if ($('newPassword').value == ""){
       	 alert("Please choose a password.")
       	 return false;
        }

		if (checkPasswords() == "passnomatch"){
        	alert("The passwords you entered do not match.  Please fix this before proceeding.")
       	 return false;
        }       
		return true;	        
    }
	
	
	 function validateCheckOutForm(){
		if ($('CCNumber').value == "" || $('CCNumber').value.length < 5){
       	 alert("Please enter a valid credit card number.")
       	 return false;
        }
		if ($('billing_first_name').value == "" || $('billing_last_name').value == "" || $('billing_address_name').value == "" || $('billing_city_name').value == "" || $('billing_state_name').value == "" || $('billing_zip_name').value == ""){
       	 alert("Please enter your complete billing address.")
       	 return false;
        }
		
		return true;
	 }
	 
	 
	 
/*************************
 *    Cart Stuff         *
 *************************/	 
 function addedToCartNotifier(itemDesc){
	notice= itemDesc+'Notice'
	$(notice).parentNode.removeChild($(notice))
	newDiv2 = document.createElement('div')
	newDiv2.id = notice
	newDiv2.appendChild(document.createTextNode('Cart Updated'))
	$(itemDesc).appendChild(newDiv2)
 }
 
 
 function smallCartNotifier(itemDesc){
 	$('shoppingCart_content').parentNode.removeChild($('shoppingCart_content'))
	newDiv = document.createElement('div')
	newDiv.id = 'shoppingCart_content'
	newDiv.appendChild(document.createTextNode('Updating Cart...'))
	$('shoppingCart').appendChild(newDiv)
	
	notice= itemDesc+'Notice'
	if($(notice) != null){
			$(notice).parentNode.removeChild($(notice))
	}	
	newDiv2 = document.createElement('div')
	newDiv2.id = notice
	newDiv2.appendChild(document.createTextNode('Updating Cart...'))
	$(itemDesc).appendChild(newDiv2)
 }
 
 
 function updateCart(){
	 $('right_bar_content').parentNode.removeChild($('right_bar_content'))
		newDiv = document.createElement('div')
		newDiv.id = 'blocksContent'
		newDiv.appendChild(document.createTextNode('Saving Changes...'))
		$('right_bar').appendChild(newDiv)
	 
	 
	 $('shoppingCart_content').parentNode.removeChild($('shoppingCart_content'))
		newDiv = document.createElement('div')
		newDiv.id = 'shoppingCart_content'
		newDiv.appendChild(document.createTextNode('Updating Cart...'))
		$('shoppingCart').appendChild(newDiv)
 }
	 
	 
	 
	 
 /*************************
 *    Basket Builder      *
 *************************/	 
 
 
 function showWineList(color){
	var children = $('wineDescriptions').childNodes;               
		 // Loop through the children
     for(var c=0; c < children.length; c++) {
     	if(children[c].style) {
     		children[c].style.display = 'none';
    	}  
	 }
	var children = $('wineLists').childNodes;               
		 // Loop through the children
     for(var c=0; c < children.length; c++) {
     	if(children[c].style) {
     		children[c].style.display = 'none';
    	}  
	 }
	 
 	$(color).style.display='block';
 }
 
  
  
  function showWineDescription(wine){
	 var children = $('wineDescriptions').childNodes;               
		 // Loop through the children
     for(var c=0; c < children.length; c++) {
     	if(children[c].style) {
     		children[c].style.display = 'none';
    	}  
	 }
	$(wine).style.display='block';
 }
 
 
 
 function addToBasket(wineId, wineName, price){
	if(numWinesInBasket<basketNumWines){
		numWinesInBasket ++;
		totalPrice = parseFloat(totalPrice)
		totalPrice += parseFloat(price)
		$('wineId'+numWinesInBasket).value = wineId;
		$('winePrice'+numWinesInBasket).value = price;
		$('wineNameStorage'+numWinesInBasket).value = wineName;
		
		var children = $('wineName'+numWinesInBasket).childNodes;               
		 // Loop through the children
		 for(var c=0; c < children.length; c++) {
			$('wineName'+numWinesInBasket).removeChild(children[c])
		 }
		//$('wineName'+numWinesInBasket).removeChild($('wineName'+numWinesInBasket).firstChild);
		 removeLink = document.createElement('a')
		 removeLink.setAttribute('href', 'javascript:removeWine("'+numWinesInBasket+'")')
		 removeLink.setAttribute('id', 'remove'+numWinesInBasket)
		 removeLink.appendChild(document.createTextNode(" Remove"))
		 removeLink.style.fontSize = ".7em"
		$('wineName'+numWinesInBasket).appendChild(document.createTextNode(wineName));
		$('wineName'+numWinesInBasket).appendChild(removeLink);
		
		$('totalPrice').removeChild($('totalPrice').firstChild);
		$('totalPrice').appendChild(document.createTextNode('$'+totalPrice.toFixed(2)))
	}
	else{alert('Your basket is already full.')}
 }
 
 
 function resetBasket(){
	while(numWinesInBasket>0){
		$('wineName'+numWinesInBasket).removeChild($('wineName'+numWinesInBasket).firstChild);
		$('wineName'+numWinesInBasket).removeChild($('remove'+numWinesInBasket));
		$('wineId'+numWinesInBasket).value = "";
		$('winePrice'+numWinesInBasket).value = "";
		$('wineNameStorage'+numWinesInBasket).value = "";
		$('wineName'+numWinesInBasket).appendChild(document.createElement("br"));
		numWinesInBasket-=1
	}
	$('totalPrice').removeChild($('totalPrice').firstChild);
	$('totalPrice').appendChild(document.createTextNode('$'+basketPrice))	
	totalPrice = basketPrice
 }
 
 function removeWine(wineNum){
	if($('winePrice'+wineNum).value != ""){
		price = $('winePrice'+wineNum).value
		totalPrice -= price 
		$('totalPrice').removeChild($('totalPrice').firstChild);
		$('totalPrice').appendChild(document.createTextNode('$'+totalPrice.toFixed(2)))	
		
		$('wineName'+wineNum).removeChild($('wineName'+wineNum).firstChild);
		$('wineName'+wineNum).removeChild($('remove'+wineNum));
		$('wineId'+wineNum).value = "";
		$('winePrice'+wineNum).value = "";
		$('wineNameStorage'+wineNum).value = "";
		numWinesInBasket-=1
		
		//need to move all the wines up one slot
		while(wineNum<=numWinesInBasket){
			movedWineNum = parseInt(wineNum)+1
			movedPrice =  $('winePrice'+movedWineNum).value
			movedId = $('wineId'+movedWineNum).value
			movedName = $('wineNameStorage'+movedWineNum).value
			
			$('wineId'+wineNum).value = movedId;
			$('winePrice'+wineNum).value = movedPrice;
			$('wineNameStorage'+wineNum).value = movedName;
			
			 removeLink = document.createElement('a')
			 removeLink.setAttribute('href', 'javascript:removeWine("'+wineNum+'")') 
			 removeLink.setAttribute('id', 'remove'+wineNum)
			 removeLink.appendChild(document.createTextNode(" Remove"))
			 removeLink.style.fontSize = ".7em"
			$('wineName'+wineNum).appendChild(document.createTextNode(movedName));
			$('wineName'+wineNum).appendChild(removeLink);			
			
			$('wineName'+movedWineNum).removeChild($('wineName'+movedWineNum).firstChild)
			$('wineName'+movedWineNum).removeChild($('remove'+movedWineNum));
			$('wineId'+movedWineNum).value = "";
			$('winePrice'+movedWineNum).value = "";
			$('wineNameStorage'+movedWineNum).value = "";
			$('wineName'+movedWineNum).appendChild(document.createElement("br"));
			wineNum +=1
		}
	}
	
	
 }
 
 
 /*************************
 *       Check Out        *
 *************************/	
 
 function sameAsShipping(){
	$('billing_first_name').value = shippingFirstName 
	$('billing_last_name').value = shippingLastName 
	$('billing_address').value = shippingAddress 
	$('billing_city').value = shippingCity 
	$('billing_state').value = shippingState
	$('billing_zip').value = shippingZip 
 }