function saveForm(formName,ignore,params)
{
	var theform = Form.serialize($(formName),true);
	var saveMe = Object.toJSON(theform);
	new Ajax.Request('/index.php?mode=beaners&func='+formName+'&'+params,
  {
    method:'post',
    parameters:{savejson: saveMe},
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      data = response.evalJSON();
      //alert(data.msg);
		// DO the bad
		if (data.errors==1)
		{
			//alert('in');
			for ( var i2=0, len2=data.fixme.length; i2<len2; ++i2 ){
				$(data.fixme[i2]).style.background = "yellow";
			}
		}
		// DO the Good
		if (data.good.length!=0){
			for ( var i=0, len=data.good.length; i<len; ++i ){
				$(data.good[i]).style.background = "white";
			}
			
		}
		
		
		if (formName=='franchise_create_appointment' && data.errors!=1)
		{
			var num_appointments = data.appointments.length
			
			for (i=0; i<num_appointments;i++)
			{
				insert_appointment(data.time,data.appointments[i]);
			}
			hideOnscren();
			window.location.href='#'+data.time;
		}
		if ((formName=='customer_login' || formName=='customer_create_account')&& data.errors!=1)
		{
			$('customer_login_result').innerHTML = 'You are now logged in!';
			hideOnscren();
		}
		
		alert(data.msg);
		// Reload Page
		if (formName=='edit_day' || formName=="edit_appointment")
		{
			window.location.reload( false );
		}
		
    },
    onFailure: function(){ alert('Something went wrong...');savebutton.disabled = false; savebutton.value = savevalue; }
  });
}


function create_child()
{
	var par = window.parent.document;
	var children = par.getElementById('children');	
	var new_div = par.createElement('div');	
	var num_children = children.getElementsByTagName('div').length+1;
	new_div.id = 'children_'+ num_children;
	children.appendChild(new_div);
	new Ajax.Request('/?mode=beaners&func=template&template=child_template&child_num='+num_children,{
		method: 'post',
		parameters: {},
		onSuccess: function(t){	
			var data = t.responseText;
			$('children_'+ num_children).innerHTML = data;
		},
		onFailure: function(){ alert('Something went wrong...'); }
	});
	return false;
	
}

function updateLocation(func,location_id,val)
{
	new Ajax.Request('/?mode=beaners&func=updateLocation',{
		method: 'post',
		parameters: {func:func,location_id:location_id,val:val},
		onSuccess: function(t){	
			var data = t.responseText.evalJSON();
			if (data.saved==true)
			{
				alert("saved:"+data.msg);
			}
			else
			{
				alert(data.msg);
			}
		},
		onFailure: function(){ alert('Something went wrong...'); }
	});
	return false;
}

function updateLocationEmail(location_id)
{
	var email = $('email_'+location_id).value;
	var password = $('password_'+location_id).value;
	
	new Ajax.Request('/?mode=beaners&func=updateLocationEmail',{
		method: 'post',
		parameters: {email:email,password:password,location_id:location_id},
		onSuccess: function(t){	
			var data = t.responseText.evalJSON();
			if (data.saved==true)
			{
				alert("saved:"+data.msg);
			}
			else
			{
				alert(data.msg);
			}
		},
		onFailure: function(){ alert('Something went wrong...'); }
	});
	return false;
}
function updateLocationAvailability(location_id)
{
	var availability = $('availability_'+location_id).value;

	new Ajax.Request('/?mode=beaners&func=updateLocationAvailability',{
		method: 'post',
		parameters: {availability:availability,location_id:location_id},
		onSuccess: function(t){	
			var data = t.responseText.evalJSON();
			if (data.saved==true)
			{
				alert("saved:"+data.msg);
			}
			else
			{
				alert(data.msg);
			}
		},
		onFailure: function(){ alert('Something went wrong...'); }
	});
	return false;
}
function appointment_info()
{
	var gen_id = $('Gen_hidden').value;
	var date = $('date').value;
	var time = $('time').value;
	if (gen_id!="")
	{
		hideOnscren();
		onScreen('/?mode=appointment&func=onscreen&call=appointment_info&customer_id='+gen_id+'&date='+date+'&time='+time);
	}
}
function create_account()
{
	var theform = Form.serialize($('createAccountForm'),true);
	var saveMe = Object.toJSON(theform);
	new Ajax.Request('/?mode=beaners&func=create_account',{
		method: 'post',
		parameters: {savejson:saveMe},
		onSuccess: function (t){
			var data=t.responseText.evalJSON();
			alert(data.msg);
			if (data.saved == 1)
			{
				$('Gen_hidden').value = data.gen_id;
				appointment_info();
			}
		},
		onFailure: function(){ alert('Could not complete');}
	});
}