/*----------------------NEW SIGNUP ------------------------------*/

var last_plan_el;
function selectPlan(type, plan_id){
	var form = $('pricing_form');
	form.plan.value = (plan_id && !isNumeric(plan_id)) ? plan_id : type;
	if(type == 'custom'){
		if($('plan_options')) $('plan_options').style.display='block';
		$('plan_form').style.display='block';
		if($('customize_plan') && $('pay_as_you_go')){
			$('customize_plan').className = '';
			$('pay_as_you_go').className = 'inactive';
		}
		window.location.href = '#plans';
	} else if(type == 'none'){
		if($('plan_options')) $('plan_options').style.display='none';
		$('plan_form').style.display='none';
		if($('customize_plan') && $('pay_as_you_go')){
			$('customize_plan').className = 'inactive';
			$('pay_as_you_go').className = '';
		}
	} else if(type == 'subzone'){
		
		if( $('plan_form')){
			if(plan_id == 'custom'){
				 $('plan_form').style.display='block';
				 window.location.href = '#plans';
			}else{
				 $('plan_form').style.display='none';
			}
		}
		if(plan_id == 'none'){
			if($('plan_options')) $('plan_options').style.display='none';
			//form.submit();
		} else if(plan_id){
			if($('plan_options')) $('plan_options').style.display='block';
		}
		for(var i=0, input; input=form.plan_id[i]; i++){
			if(input.value == plan_id){
				input.checked = true;
				if(last_plan_el) removeClassName(last_plan_el, "selected");
				addClassName(input.parentNode, "selected");
				last_plan_el = input.parentNode;	
			}
		}
	}
}

/*---------------------------------------------------------------------*/
var toggleOrderDetails = function(el){
	if(hasClassName(el,'view')){
		var h = el.lastChild.clientHeight;
		el.lastChild.style.height = el.lastChild.clientHeight+'px';
		var tween = Animate.Tween(el.lastChild.style,'height','regularEaseOut',h,0,0.6,'px');
		tween.onMotionFinished = function(){removeClassName(el,'view');}
		
	} else{
		addClassName(el,'view');
		el.lastChild.style.height = 'auto';
		var h = el.lastChild.clientHeight;
		el.lastChild.style.height = "0px";
		var tween = Animate.Tween(el.lastChild.style,'height','regularEaseIn',0,h,0.6,'px');
		tween.onMotionFinished = function(){el.lastChild.style.height = 'auto';}
	}
}



var toggleHelp = function(parent,div){
	parent_el = $(parent);
	div_el = $(div);
	if(!div_el || !parent_el) return;
	if(parent_el.current_explanation){
		var tween = Animate.Tween(parent_el.current_explanation.style,'height','regularEaseOut',parent_el.current_explanation.clientHeight,0,0.2,'px');
		tween.onMotionFinished = function(){
			parent_el.current_explanation.style.display = 'none';
			if(parent_el.current_explanation != div_el){
				parent_el.current_explanation = false;
				toggleHelp(parent,div);
			} else{
				parent_el.current_explanation = false;
			}
			
		}
		return;
	} else{
		div_el.style.display = 'block';
		div_el.style.height = 'auto';
		var h = div_el.clientHeight;
		div_el.style.height = "0px";
		var tween = Animate.Tween(div_el.style,'height','regularEaseIn',0,h,0.2,'px');
		tween.onMotionFinished = function(){div_el.style.height = 'auto';}
		parent_el.current_explanation = div_el;
	}
}
var validateOrder = function(form){
	form = typeof(form) == "string" ? document.getElementById(form) : form;
	if(form.pu_type){
		if(form.pu_type.value == ''){
			alert('Please select the type of pickup you would like');	
			 return false;
		} else if(form.db_type.value == ''){
			alert('Please select the type of delivery you would like');	
			 return false;
		}
		else{
			return true;
		}
	}
}


var initList = function(list){
	var list = typeof(list) == "string" ? document.getElementById(list) : list;
	var items = list.childNodes;

	for(var i=0,item;item=items[i];i++){
		if(hasClassName(item,'item') &&!hasClassName(item,'deleted')){
			new function(item){
				item.onmouseover = function(){addClassName(item,'hover');}
				item.onmouseout = function(){removeClassName(item,'hover');}
				item.onclick = function(event){toggleOrderDetails(item);}
			}(item);
			for(var j=0,as=item.getElementsByTagName('a'),a;a=as[j];j++){
				addEvent(a,"click",function(event){
					if(event && event.stopPropagation) event.stopPropagation();
					event.cancelBubble = true;
				});
			}
		}
	}
	 
}
var checkPlans = function(){
	var form  = $('plans');
	if(form.plan[0].checked){
		if(!planCalculator.getUsesPerMonth()){
			alert( 'Please choose the number of uses per month.');
			//$('error_message').innerHTML = 'Please choose the number of uses per month.';
			//$('error_message').style.display='block';
			return false;;
		} else if(!planCalculator.getPoundsPerUse() && !planCalculator.getCreditsPerUse()){
			alert('Please choose either the amount of pounds you prefer per use, the amount of credits you prefer per use, or both.');
			//$('error_message').innerHTML = 'Please choose either the amount of pounds you prefer per use, the amount of credits you prefer per use, or both.';
			//$('error_message').style.display='block';
			return false;
		} else{
			
			return true;
		}
	}else if(form.plan[1].checked){
		return true;
	} else{
		//$('error_message').innerHTML = 'Please select whether you would like to sign up for a plan or not.';
		//$('error_message').style.display='block';
	}
	
}
var selectPref = function(event,el){
	var target = event.target?event.target:event.srcElement;
	if(hasClassName(el.parentNode,'disabled') || hasClassName(el,'disabled')) return false;
	if(el.name == "allergies[]"){
		if(el.value == "starch"){
			var options = $('preferences').starch;
			if(el.checked){
				options.disabled = true;
				for(var i=0,option;option=options[i];i++){ option.selected=(option.value=="none"?true:false);}
			} else{
				options.disabled = false;
			}
		}
		
		if(el.value == "bounce"){
			var option = $('preferences')['extra_softener[]'][0];
			
			if(el.checked){
				addClassName(option.parentNode,'disabled');
				option.disabled = true;
				option.checked=false;
			}else{
				removeClassName(option.parentNode,'disabled');option.disabled = false;
			}
		} else if(el.value == "downy"){
			var option = $('preferences')['extra_softener[]'][1];
			
			if(el.checked){
				addClassName(option.parentNode,'disabled');
				option.disabled = true;
				option.checked=false;
			}else{
				removeClassName(option.parentNode,'disabled');option.disabled = false;
			}
		}
	
	}
}

var toggleBillingAddress = function(select){
	if(select.options[select.selectedIndex].value == "new"){
		tweenToggleDisplay(select.parentNode.parentNode.nextSibling,'show');
	} else{
		tweenToggleDisplay(select.parentNode.parentNode.nextSibling,'hide');
	}
}

var guessCardType = function(el){
	if(!el.nextSibling || el.nextSibling.nodeName != "BUTTON") insertAfter(newEl('BUTTON',{'class':'hidden'}),el);
	var form = parentForm(el);
	var cc_logo = form.card_type.previousSibling;
	var number = el.value;
	if(number.indexOf('*') != -1)	return true; // CARD NUMBER HAS A * IN IT, SO DON'T WORRY ABOUT IT
	if(!number.length){
		cc_logo.className = '';
		return false;
	}
	el.nextSibling.className = 'hidden';
	var types = ['husky','visa','mastercard','discover','amex','campus_cash'];
	var start = {'husky':'^6(0(1(5|$)|$)|$)','mastercard':'^5([1-5]|$)','visa':'^4','amex':'^3(4|7|$)','discover':'^6(0(1(1|$)|$)|$)','campus_cash':'^6(0(3(9|(5|$)$)|$)|$)'};
	var full = {"husky":"^6015[0-9]{12}$","visa":"^4[0-9]{12}(?:[0-9]{3})?$","mastercard" : "^5[1-5][0-9]{14}$","discover" : "^6011[0-9]{12}$","amex" : "^3[47][0-9]{13}$", 'campus_cash':'^60395[0-9]{11}$'};
	
	var matches = 0;
	for(var i=0,type;type=types[i];i++){
		if(new RegExp(start[type]).test(number)){
			matches++;
			addClassName(cc_logo,type);
			form.card_type.value = type;
			
			if(new RegExp(full[type]).test(number)){
				if(mod10(number)){
					el.nextSibling.className = 'success';
					return true;
				} else{
					el.nextSibling.className =  'error';
					return false;
				}
			} else{
				el.nextSibling.className = 'hidden';
			}
		} else{
			removeClassName(cc_logo,type);
		}
	}
	if(!matches){
		
		el.nextSibling.className = 'error';
	}
	return false;
}

var mod10 = function(number){
	var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;
    for (digitCounter = number.length - 1; digitCounter >= 0; digitCounter--){
      checkSumTotal += parseInt (number.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((number.charAt(digitCounter) * 2));
      for (var c = 0; c < numberProduct.length; c++){
        checkSumTotal += parseInt(numberProduct.charAt(c));
      }
    }
    return (checkSumTotal % 10 == 0);
}
var checkTermsAgreement = function(form){
	form = typeof(form) == "string" ? document.getElementById(form) : form;
	if(form.agree.checked == false){
		form.agree.parentNode.firstChild.className = 'error';
		alert('You must agree to our terms and conditions before finalizing your account registration');
		return false;
	} else{
		form.agree.parentNode.firstChild.className = 'success';
		return true;
	}
	
}
var checkBilling = function(form){
	form = typeof(form) == "string" ? document.getElementById(form) : form;
	if(form.full_name.value || form.card_number.value || form.addr_1.value || form.city.value || form.zip.value){
		if(!guessCardType(form.card_number)){
			alert('Please enter a valid credit card number');
			return false;
		}
		var complete = true;
		var message = "";
		if(!form.full_name.value){		message += "You did not supply a name. ";complete=false;}
		if(!form.billing_address || form.billing_address.options[form.billing_address.selectedIndex].value == "new"){
			if(!form.card_number.value){	message += "You did not supply a credit card number. ";complete=false;}
			if(!form.addr_1.value){			message += "You did not supply a billing address. ";complete=false;}
			if(!form.city.value){			message += "You did not supply a billing city. ";complete=false;}
			if(!form.zip.value){			message += "You did not supply a billing zip code. ";complete=false;}
		}
		if(!complete){
			message += "Your billing profile is incomplete. Are you sure you want to continue?";
			return confirm(message);
		} 
	}
	return true;
}
var datePicker = function(el,y,m,d){
	var daysInMonth = function(y,m) {return new Date(y, m+1, 0).getDate();}
	var starting_year = 2000;
	var year_select = el.firstChild;//document.createElement('SELECT');
	
	var selected_year = year_select.options[0].value;
	year_select.length = 0;
	for(var year=starting_year,option;year>1900;year--){
		option = document.createElement('OPTION');
		option.value = option.text = year;
		if(option.value == selected_year) option.selected = true;
		try {
			year_select.add(option, null); // standards compliant; doesn't work in IE
		}catch(ex) {
			year_select.add(option); // IE only
		}
	}
	var month_select = el.childNodes[1];//document.createElement('SELECT');
	var selected_month = month_select.options[0].value;
	month_select.length=0;
	for(var month=0,option,months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];month<12;month++){
		option = document.createElement('OPTION');
		option.value = month+1;
		if(option.value == selected_month){option.selected = true;}
		option.text = months[month];
		try {
			month_select.add(option, null); // standards compliant; doesn't work in IE
		}catch(ex) {
			month_select.add(option); // IE only
		}
	}
	var day_select = el.childNodes[2];//document.createElement('SELECT');
	var selected_day = day_select.options[0].value;
	day_select.length = 0;
	var fillDays = function(y,m,d){
		index = Math.max(0,day_select.selectedIndex);
		day_select.options.length = 0;
		for(var day=1,option,days_in_month=daysInMonth(y,m-1);day<=days_in_month;day++){
			option = document.createElement('OPTION');
			option.value = option.text = day;
			if(option.value == d) option.selected = true;
			try {
				day_select.add(option, null); // standards compliant; doesn't work in IE
			}catch(ex) {
				day_select.add(option); // IE only
			}
		}
		if(!d) day_select.selectedIndex = day_select.options.length <= index ? day_select.options.length -1: index;
	}
	fillDays(selected_year,1,selected_day);
	month_select.onchange = function(){
		fillDays(year_select.options[year_select.selectedIndex].value,month_select.options[month_select.selectedIndex].value);
	}
	year_select.onchange = function(){
		fillDays(year_select.options[year_select.selectedIndex].value,month_select.options[month_select.selectedIndex].value);
	}
	var div = document.createElement('div');
	//removeChildren(el);
	//el.appendChild(year_select);
	//el.appendChild(month_select);
	//el.appendChild(day_select);
	el.onmousedown = null;
	return;
}

function showManagerPermissions(checkbox, div){
	setCheckboxParent(checkbox,div);
	tweenToggleDisplay(div);
}
function hideManagerPermissions(checkbox, div){
	tweenToggleDisplay(div);
}
