/* open and close div panels */

function initDivs(){
	document.getElementById('crd-cart').style.display='none';
}

function togglePanels(panel,action){
	if(action=='show'){
		document.getElementById(panel).style.display='block';
		document.getElementById(panel+'-toggle').innerHTML="<a onclick=\"javascript:togglePanels(\'"+panel+"\',\'hide\');\">-</a>";
	}
	else if(action=='hide'){
		document.getElementById(panel).style.display='none';
		document.getElementById(panel+'-toggle').innerHTML="<a onclick=\"javascript:togglePanels(\'"+panel+"\',\'show\');\">+</a>";
	}
}

/* about us/company content creator */
function loadContent(url,pgcode,divClass){
	document.getElementById('crd-main-content').className=divClass;
	Fse.Ajax.updateContent(url,'crd-main-content');
	Fse.Ajax.updateContent('includes/inc_breadcrumb.cfm?pgcode='+pgcode,'breadcrumb-div-about');
	Fse.Ajax.updateContent('includes/inc_subnav_about_us.cfm?pgcode='+pgcode,'sub-left-content-about');
}

/* contact us sub nav */
function loadContactPage(url,pgcode,divClass){
	Fse.Ajax.updateContent(url,'crd-main-content');
	Fse.Ajax.updateContent('includes/inc_subnav_contact.cfm?pgcode='+pgcode,'sub-left-content-about');
}

/* dairy bar sub nav */
function loadDairyBarContent(url,pgcode){
	Fse.Ajax.updateContent(url,'crd-main-content');
	Fse.Ajax.updateContent('includes/inc_subnav_dairy_bar.cfm?pgcode='+pgcode,'sub-left-content');
}

/* item scroll */
function itmScroll(url,container){
	//document.getElementById(container).innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent(url,container);
}

/* account section */
function loadAccountPage(url,pgcode,divClass){
	document.getElementById('account-main').innerHTML='<blockquote>loading....</blockquote>';
	document.getElementById('account-main').className=divClass;
	Fse.Ajax.updateContent(url,'account-main');
	Fse.Ajax.updateContent('includes/inc_subnav_account.cfm?pgcode='+pgcode,'sub-left-content');
}

/* account section - standing orders */
function stOrderTurnOffAlert(){
	var x = document.getElementById('orig_autoSchedulingOn').value;
	var y = document.frm_StndOrdFreq.autoSchedulingOn;
	var yVal = "";
	
	for (var counter=0; counter<y.length; counter++)
    	if (y[counter].checked == true){
       		var yVal = y[counter].value;
	}
	
	if((yVal==0)&&(x==1)){
		var answer = confirm("You have changed your standing order settings from \'Yes'\ to \'No\'.  If you would like to cancel any future orders that you may have in the system, click \'OK\'.");
		if (answer==1){
			document.getElementById('dltFutureStOrdersInput').value='Yes';
			document.frm_StndOrdFreq.submit();
		}
	}
	else
		document.frm_StndOrdFreq.submit();
}

function addToCartFromSO(itmId,qty){
	document.getElementById('crd-cart').style.display='block';
	document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm?itmId='+itmId+'&qty='+qty,'crd-cart-container');
	document.getElementById('account-main').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_account_standing_orders.cfm?action=itmAdded2Cart','account-main');
}

function removeFromSO(itmId){
	/* these will need to update the standing order module */
	//document.getElementById('crd-cart').style.display='block';
	//document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
	//Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm?itmId='+itmId+'&qty='+qty,'crd-cart-container');
	document.getElementById('account-main').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_account_standing_orders.cfm?action=remove&itmId='+itmId,'account-main');
}

/* account section - order history, recent orders */
function loadOrder(orderId,action){
	document.getElementById('account-main').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_order.cfm?orderId='+orderId+'&action='+action,'account-main');
}

function removeFromOrder(orderId,itmId){
	document.getElementById('account-main').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_order.cfm?orderId='+orderId+'&itmId='+itmId+'&action=remove','account-main');
}

/* product center */
function loadProducts(cat,sc){
	document.getElementById('product-container').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_products_breadcrumb.cfm?cat='+cat+'&sc='+sc,'product-breadcrumbs');
	Fse.Ajax.updateContent('includes/inc_subnav_products.cfm?cat='+cat+'&sc='+sc,'products-subnav');
	Fse.Ajax.updateContent('includes/inc_products.cfm?cat='+cat+'&sc='+sc,'product-container');
}

function searchProducts(){
	var kywrd = document.getElementById('inputPrdSrch').value;
	document.getElementById('product-container').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_products_breadcrumb.cfm?cat=0sc=0&kywrd='+kywrd,'product-breadcrumbs');
	Fse.Ajax.updateContent('includes/inc_subnav_products.cfm?cat=0sc=0&kywrd='+kywrd,'products-subnav');
	Fse.Ajax.updateContent('includes/inc_products.cfm?cat=0sc=0&kywrd='+kywrd,'product-container');
}

function loadProducts2(cat,sc,kywrd){
	document.getElementById('product-container').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_products_breadcrumb.cfm?cat='+cat+'&sc='+sc+'&kywrd='+kywrd,'product-breadcrumbs');
	Fse.Ajax.updateContent('includes/inc_subnav_products.cfm?cat='+cat+'&sc='+sc+'&kywrd='+kywrd,'products-subnav');
	Fse.Ajax.updateContent('includes/inc_products.cfm?cat='+cat+'&sc='+sc+'&kywrd='+kywrd,'product-container');
}

function loadProductDetail(cat,sc,kywrd,pid,ptoken){
	document.getElementById('product-container').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_product_details.cfm?cat='+cat+'&sc='+sc+'&kywrd='+kywrd+'&pid='+pid+'&ptoken='+ptoken,'product-container');
}

/* toggle between product image and nutritionals */
function toggleImages(p){
	if(p=='I'){
		document.getElementById('prdDtls_image').style.display='block';
		document.getElementById('prdDtls_nutri').style.display='none';
		document.getElementById('prdDtl_imageToggle').innerHTML='<a href="javascript:toggleImages(\'N\');">View Nutritional Info</a><br />';
	}
	else if(p=='N'){
		document.getElementById('prdDtls_nutri').style.display='block';
		document.getElementById('prdDtls_image').style.display='none';
		document.getElementById('prdDtl_imageToggle').innerHTML='<a href="javascript:toggleImages(\'I\');">View Product Photo</a><br />';
	}
}

/* sign up form */
function openPaymentMethod(methodTyp){
	for(i=1;i<=3;i++){
		document.getElementById('signup-payment-method-'+[i]).style.display='none';
	}
	document.getElementById('signup-payment-method-'+methodTyp).style.display='block';
}

function postValidateSignUpForm(){

	var pswrd=document.frm_signUp.FSPro_Password
	var pswrd2=document.frm_signUp.Password2

	if (pswrd.value!==pswrd2.value){
		alert("Please re-type your password to verify that you entered it correctly.")
		pswrd.value=""
		pswrd2.value=""
		pswrd.focus()
		return false
	}
	
	// if payment method is cc, must validate those fields
	var paymentMthd=document.frm_signUp.paymentMethod
	var paymentMthdValue = "";
		
	for (var counter=0; counter<paymentMthd.length; counter++)
    	if (paymentMthd[counter].checked == true){
       		var paymentMthdValue = paymentMthd[counter].value;
	}

	
	
	if(paymentMthdValue=="CreditCard"){
	
		var ccType=document.frm_signUp.ccType;
		
		if (ccType.selectedIndex == 0) {
			alert("Please identify your credit card type.");
			ccType.focus();
			return false
		}
		
		var ccNum=document.frm_signUp.ccNum;
		
		if (Ck4Spaces(ccNum.value,1)==false){
			alert("Please input your credit card number.")
			ccNum.value='';
			ccNum.focus();
			return false
		}
		
		var ccExpMonth=document.frm_signUp.ccExpMonth;
		
		if (ccExpMonth.selectedIndex == 0) {
			alert("Please identify your credit card expiration date.");
			ccExpMonth.focus();
			return false
		}
		
		var ccExpYear=document.frm_signUp.ccExpYear;
		
		if (ccExpYear.selectedIndex == 0) {
			alert("Please identify your credit card expiration date.");
			ccExpYear.focus();
			return false
		}
		
	}
	
	
	document.getElementById('sbmtBtn').disabled=true;
	return true
}


/* cart functions */
function addToCart(itmId){
	var qty = document.getElementById('itm_'+itmId).value
	document.getElementById('crd-cart').style.display='block';
	document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm?itmId='+itmId+'&qty='+qty,'crd-cart-container');
}

function addToWhere(addType,itmId){

	var qty = document.getElementById('itm_'+itmId).value;
	if (Ck4Spaces(qty,1)==false){
		alert("Please input a quantity before selecting an action.");
		document.getElementById('resetSlct_'+itmId).selected = true;
		return false
	}
	else if (qty==0){
		alert("Please input a quantity before selecting an action.");
		document.getElementById('resetSlct_'+itmId).selected = true;
		return false
	}
	
	if(addType==1){
		document.getElementById('crd-cart').style.display='block';
		document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm?itmId='+itmId+'&qty='+qty,'crd-cart-container');
	}
	else if(addType==2){
		document.getElementById('standing-order').style.display='block';
		document.getElementById('standing-order').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/subincludes/subInc_myStandingOrder.cfm?itmId='+itmId+'&qty='+qty,'standing-order-container');
	}
	else if(addType==3){
		document.getElementById('crd-cart').style.display='block';
		document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm?itmId='+itmId+'&qty='+qty,'crd-cart-container');
		document.getElementById('standing-order').style.display='block';
		document.getElementById('standing-order').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/subincludes/subInc_myStandingOrder.cfm?itmId='+itmId+'&qty='+qty,'standing-order-container');
	}
}

function removeFromCart(itmId){
	document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_cart_display.cfm?action=remove&itmId='+itmId,'cart-display');
	document.getElementById('crd-cart').style.display='block';
	document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm','crd-cart-container');
}

function addToStandingOrderFromCart(itmId,qty){
	document.getElementById('standing-order').style.display='block';
	document.getElementById('standing-order').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/subincludes/subInc_myStandingOrder.cfm?itmId='+itmId+'&qty='+qty,'standing-order-container');
	
}

function clearCart(){

	var answer = confirm("Are you sure you want to empty your cart?");
			
	if (answer==1){
		document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/inc_cart_display.cfm?action=clearCart','cart-display');
		document.getElementById('crd-cart').style.display='block';
		document.getElementById('crd-cart').innerHTML='<blockquote>loading....</blockquote>';
		Fse.Ajax.updateContent('includes/subincludes/subInc_myCart.cfm','crd-cart-container');
	}
}

function loadCart(){
	document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_cart_display.cfm','cart-display');
	Fse.Ajax.updateContent('includes/inc_subnav_cart.cfm?pgcode=cart','sub-left-content');
}

function loadLogin(){
	document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_cart_login.cfm','cart-display');
}

function loadCheckout(){
	document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_cart_checkout.cfm','cart-display');
	Fse.Ajax.updateContent('includes/inc_subnav_cart.cfm?pgcode=chkout','sub-left-content');
}

function loadPasswordReminder(rtn){
	document.getElementById('cart-display').innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/inc_cart_login_reminder.cfm?rtn='+rtn,'cart-display');
}

/* order history */
function loadOrderHistory(start,reccnt,container){
	document.getElementById(container).innerHTML='<blockquote>loading....</blockquote>';
	Fse.Ajax.updateContent('includes/subincludes/subInc_ordersTable.cfm?start='+start,container);
	Fse.Ajax.updateContent('utilities/utl_pager_order_history.cfm?start='+start+'&reccnt='+reccnt,'order-history-pager');
}

/* faqs */
function dspFaq(dspID,Total){
	for(i=1;i<=Total;i++){
		if(i!=dspID){
			a = "document.getElementById('faq-a_" + i + "')";
			b = eval(a);
			b.style.display='none';
		}else{
			x = "document.getElementById('faq-a_" + i + "')";
			y = eval(x);
			y.style.display='block';
		}
	}
}
function closeFAQ(dspID){
	document.getElementById('faq-a_'+dspID).style.display='none';
}

/* refer a friend */
function openGiftPack(pack){
	Fse.DialogManager.show( "crd_freeGiftChoice", { src: "includes/forms/frm_giftPack.cfm?pack="+pack});
}

function validateBkfstPk(){
	var pkslct=document.frm_bkfstPk.gift;
	
	if (pkslct.checked==true){
		
		var cfe=document.frm_bkfstPk.coffee;
		
		if (cfe.selectedIndex == 0) {
			alert("Please Select an Coffee Flavor.");
			cfe.focus();
			return false
		}
			
		var crmtype=document.frm_bkfstPk.crmtype
		var crmtypeRadio = false;
		
		for (var counter=0; counter<crmtype.length; counter++)
    		if (crmtype[counter].checked == true){
        		crmtypeRadio = true;
			}
		
		if (crmtypeRadio == false) {
			alert('Please Select a Pint of Cream');
			return false;
		}
		else 
			alert("Please continue with step 2.");
			loadGiftSelection(1);
	}
		
	else
		alert("Please make your selections first.");
		document.frm_bkfstPk.gift.checked = true;
		return false;
}

function validateBbqPk(){
	var pkslct=document.frm_bbqPk.gift;
	
	if (pkslct.checked==true){
		loadGiftSelection(2);
	}
		
	else
		var answer = confirm("Would you like to choose this gift?");
			if (answer==1){
				alert("Please continue with step 2.");
				loadGiftSelection(2);
			}
			else
				alert("Please choose one of the other gifts.");
				Fse.DialogManager.hide("crd_freeGiftChoice");
}

function validateSundaePk(){
	var pkslct=document.frm_sundaePk.gift;
		
	if (pkslct.checked==true){
			
	var flvr=document.frm_sundaePk.flavor;
		
	if (flvr.selectedIndex==0){
		alert("Please Select an Ice Cream Flavor.");
		flvr.focus();
		return false;
	}
			
	var tpng=document.frm_sundaePk.topping;
	var tpngRadio = false;
		
	for (var counter=0; counter<tpng.length; counter++)
    	if (tpng[counter].checked == true){
        	tpngRadio = true;
		}
		if (tpngRadio == false) {
			alert('Please Select a Jar of Topping');
			return false;
        }
			
		var tpng2=document.frm_sundaePk.topping2;
		
		if (tpng2.selectedIndex == 0) {
			alert("Please Select an Ice Cream Topping.");
			tpng2.focus();
			return false
		}
			
		else 
			alert("Please continue with step 2.");
			loadGiftSelection(3);
	}
		
	else
		alert("Please make your selections first.");
		document.frm_sundaePk.gift.checked = true;
		return false;
}

function loadGiftSelection(pack){
	Fse.DialogManager.hide( "crd_freeGiftChoice");
	document.getElementById('rafContainer').innerHTML='<br /><br /><blockquote>loading....</blockquote>';
	if(pack==1){
		var coffee = document.frm_bkfstPk.coffee.value;
		var cream = "";
		for (var i=0; i < document.frm_bkfstPk.crmtype.length; i++){
			if (document.frm_bkfstPk.crmtype[i].checked){
				var cream = document.frm_bkfstPk.crmtype[i].value;
			}
		}
		Fse.Ajax.updateContent('includes/inc_referafriend.cfm?gftSlct=1&coffee='+coffee+'&crm='+cream, 'rafContainer');
	}
	else if(pack==2){
		Fse.Ajax.updateContent('includes/inc_referafriend.cfm?gftSlct=2', 'rafContainer');
	}
	else if(pack==3){
		var flavor = document.frm_sundaePk.flavor.value;
		var topping1 = document.frm_sundaePk.topping.value;
		var topping2 = document.frm_sundaePk.topping2.value;
		Fse.Ajax.updateContent('includes/inc_referafriend.cfm?gftSlct=2&flvr='+flavor+'&tpng='+topping1+'&tpng2='+topping2, 'rafContainer');
	}
	
}

/* make payment form */
function postValidatePaymentForm(){

	document.getElementById('sbmtBtn').disabled=true;
	return true
}

/* checkout CC form */
function openCCform(){
	document.getElementById('checkout-cc-check').value='1';
	document.getElementById('checkout-cc-form').style.display='block';
}

//function validateFrmReferral() {
	
	//var gft=document.frm_referral.gftslct;
	//var gftRadio = false;
		
	//for (var counter=0; counter<gft.length; counter++)
    	//if (gft[counter].checked == true){
        	//gftRadio = true;
		//}
		//if (gftRadio == false) {
			//alert('Please choose your free gift.');
			//return false;
        //}
	
	//var rfn=document.frm_referral.refFN;
	//var rln=document.frm_referral.refLN;

	//if ((rfn.value==null)||(rfn.value=="")){
		//alert("Please provide your first name.");
		//rfn.focus();
		//return false;
	//}
		
	//if ((rln.value==null)||(rln.value=="")){
		//alert("Please provide your last name.");
		//rln.focus();
		//return false;
	//}		
			
	//var rfeml=document.frm_referral.refEml

	//if ((rfeml.value==null)||(rfeml.value=="")){
		//alert("Please provide your email address.");
		//rfeml.focus();
		//return false;
	//}

	//if (echeck(rfeml.value)==false){
		//rfeml.focus();
		//return false;
	//}
			
	//var rfcc=document.frm_referral.refCC;
	
	//if ((rfcc.value==null)||(rfcc.value=="")){
		//alert("Please provide your customer number.");
		//rfcc.focus();
		//return false;
	//}
	
	//var isOne=false;
	
	//for ( i=1; i < 6; i++ )
	
		//alert([i])
		
		//var a = document.frm_referral.fname_[i];
		//b = eval(a);
	
		//a = "document.frm_referral.fname_" + [i] + "')";
		//b = eval(a.value.length);
	
		//if (b.value.length > 1){
			//a = "document.frm_referral.fname_1";
			//alert(b.value.length);
		//}
	
   		//isOne=true;
		//document.frm_referral.ref_[i].value = 1;
			
			//var lm_[i]=document.frm_referral.lname_[i]

			//if ((lm_[i].value==null)||(lm_[i].value=="")){
				//alert("Please provide a last name for your referral")
				//lm_[i].focus()
				//return false
			//}
			
			//var em_[i]=document.frm_referral.email_[i]

			//if ((em_[i].value==null)||(em_[i].value=="")){
				//alert("Please provide an email address for your referral")
				//em_[i].focus()
				//return false
			//}

			//if (echeck(em_[i].value)==false){
				//em_[i].focus()
				//return false
			//}
			
			//var ph=document.frm_referral.phone_[i]
	
			//if ((ph.value==null)||(ph.value=="")){
				//alert("Please provide a phone number for your referral")
				//ph.focus()
				//return false
			//}
	
			//if (ph.value.length < 8){
   				//alert("Please enter at least 8 characters in the \"Phone\" field.");
			  //  ph.focus()
				//return false
			//}
	
	
	//if (isOne == false) {
    	//alert('Please provide info on at least one referral before submitting.');
		//document.frm_referral.fname_1.focus()
		//return false
	//}
	
	//alert("theEnd");
	//document.frm_referral.submit();
//}

/* end refer a friend */	


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr-1){
   		alert("Invalid E-mail Address")
   		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1){
	    alert("Invalid E-mail Address")
    	return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail Address")
    	return false
 	}

 	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    	alert("Invalid E-mail Address")
    	return false
 	}

 	if (str.indexOf(dot,(lat+2))==-1){
    	alert("Invalid E-mail Address")
    	return false
 	}

 	if (str.indexOf(" ")!=-1){
    	alert("Invalid E-mail Address")
    	return false
 	}

	 	return true					
}

function Ck4Spaces(txt,minlength) {
    chkKeyword = txt.replace(/\s+$/gi, "");
    chkKeyword = chkKeyword.replace(/^\s*/gi, "");
	if(chkKeyword.length<minlength){
		return false;		
	}
}