var theurl;
var thankyoupage = 'http://www.dancepartydjs.com/celebrations_fld/weddings_fld/thankyou.asp';
function check_save(){
	if($("bp_save").checked){
		$('saverow1').setStyle('display', '');
		$('saverow2').setStyle('display', '');
	}else{
		$('saverow1').setStyle('display', 'none');
		$('saverow2').setStyle('display', 'none');
	}
}

function submittheform(){
	var ct_id = $("ct_id").value;
	var wf_id = $("wf_id").value;
	error_array = "";
	checktext("ct_bride_first_name");
	checktext("ct_bride_last_name");
	checktext("ct_groom_first_name");
	checktext("ct_groom_last_name");
	
	checkcheckbox("wf_toast_by_yes","form1","wf_toast_by_row");
	checkcheckbox("wf_centerpiece_yes","form1","wf_centerpiece_row");
	checkcheckbox("wf_bride_dad_dance_yes","form1","wf_bride_dad_dance_row");
	checkcheckbox("wf_groom_mom_dance_yes","form1","wf_groom_mom_dance_row");
	checkcheckbox("wf_bouquet_yes","form1","wf_bouquet_row");
	checkcheckbox("wf_garter_yes","form1","wf_garter_row");
	checkcheckbox("wf_garter_toss_yes","form1","wf_garter_toss_row");
	checkcheckbox("wf_cut_cake_yes","form1","wf_cut_cake_row");

	
	if (checkstyle($('wf_toast_by'))){
		checktext("wf_toast_by")
	};
	
	if (checkstyle($('wf_centerpiece_by'))){
		checktext("wf_centerpiece_by")
	};
	
	if (checkstyle($('wf_bride_dad_dance'))){
		checktext("wf_bride_dad_dance")
	};
	
	if (checkstyle($('wf_groom_mom_dance'))){
		checktext("wf_groom_mom_dance")
	};
	
	if (checkstyle($('wf_bouquet'))){
		checktext("wf_bouquet")
	};
	
	if (checkstyle($('wf_garter'))){
		checktext("wf_garter")
	};
	
	if (checkstyle($('wf_garter_toss'))){
		checktext("wf_garter_toss")
	};
	
	if (checkstyle($('wf_cut_cake'))){
		checktext("wf_cut_cake")
	};
	
	try
	{
		var bp_save = $("bp_save").checked;
	}
	catch(err)
	{
		var bp_save = false;
	}


	if(bp_save){
		var ct_email = $("ct_email").value;
		var ct_password = $("ct_password").value;
		checkemail("ct_email");
		checktext("ct_password");
	}else{
			ct_email = '';
			ct_password = '';
	}
		
	
	if (error_array != ""){
		showerrors();
		alert('You have some errors in your form. Please correct.');
		return;
	}
	
	if (wf_id != ''){
		updatelisting(wf_id,ct_id);
	}else{
			//create the user account
			theurl = "/moo/wedding_details.asp";
			theurl = theurl + '?ct_bride_first_name=' + $("ct_bride_first_name").value;
			theurl = theurl + '&ct_bride_last_name=' + $("ct_bride_last_name").value;
			theurl = theurl + '&ct_groom_first_name=' + $("ct_groom_first_name").value;
			theurl = theurl + '&ct_groom_last_name=' + $("ct_groom_last_name").value;
			theurl = theurl + '&ct_wedding_date=' + $("ct_wedding_date").value;
			theurl = theurl + '&ct_email=' + ct_email;
			theurl = theurl + '&ct_password=' + ct_password;
			theurl = theurl + '&action=adduser';
			
			var req = new Request({url:theurl, 
				onSuccess: function(id) {
					insertlisting(id);
				},
				onFailure: function() {
					alert('Error creating user account.');
					return;
				}
			});
			req.send();
	
	}
}

function insertlisting(id){
	theurl = "/moo/wedding_details.asp";
	theurl = theurl + '?wf_ct_id=' + id;
	buildtheurl();
	theurl = theurl + '&action=insertlisting';
	//location.href = theurl;
	var req = new Request({url:theurl, 
		onSuccess: function(test) {
			location.href = 'thankyou.asp?msg=Wedding Details Updated&ct_id=' + id;;
		},
		onFailure: function() {
			alert('Error Adding a listing.');
			return;
		}
	});
	req.send();
}

function updatelisting(wf_id,ct_id){
	
	var ct_email = $('ct_email').value;
	var ct_password = $('ct_password').value;
	
	theurl = "/moo/wedding_details.asp";
	theurl = theurl + '?wf_id=' + wf_id;
	theurl = theurl + '&ct_bride_first_name=' + $("ct_bride_first_name").value;
	theurl = theurl + '&ct_bride_last_name=' + $("ct_bride_last_name").value;
	theurl = theurl + '&ct_groom_first_name=' + $("ct_groom_first_name").value;
	theurl = theurl + '&ct_groom_last_name=' + $("ct_groom_last_name").value;
	theurl = theurl + '&ct_wedding_date=' + $("ct_wedding_date").value;
	theurl = theurl + '&ct_email=' + ct_email;
	theurl = theurl + '&ct_password=' + ct_password;
	buildtheurl();
	theurl = theurl + '&action=updatelisting';
	//location.href = theurl;
	var req = new Request({url:theurl, 
		onSuccess: function(id) {
			location.href = location.href = 'thankyou.asp?msg=Wedding Details Updated&ct_id=' + ct_id;
		},
		onFailure: function() {
			alert('Error updating listing.');
			return;
		}
	});
	req.send();
}

function buildtheurl(){
	theurl = theurl + '&wf_first_dance=' + Trim($("wf_first_dance").value);
	theurl = theurl + '&wf_wedding_party_dance=' + Trim($("wf_wedding_party_dance").value);
	theurl = theurl + '&wf_grace_by=' + Trim($("wf_grace_by").value);
	theurl = theurl + '&wf_toast_by_yes=' + get_radio_value("wf_toast_by_yes");
	theurl = theurl + '&wf_toast_by=' + Trim($("wf_toast_by").value);
	theurl = theurl + '&wf_centerpiece_yes=' + get_radio_value("wf_centerpiece_yes");
	theurl = theurl + '&wf_centerpiece_by=' + Trim($("wf_centerpiece_by").value);
	theurl = theurl + '&wf_bride_dad_dance_yes=' + get_radio_value("wf_bride_dad_dance_yes");
	theurl = theurl + '&wf_bride_dad_dance=' + Trim($("wf_bride_dad_dance").value);
	theurl = theurl + '&wf_groom_mom_dance_yes=' + get_radio_value("wf_groom_mom_dance_yes");
	theurl = theurl + '&wf_groom_mom_dance=' + Trim($("wf_groom_mom_dance").value);
	theurl = theurl + '&wf_bouquet_yes=' + get_radio_value("wf_bouquet_yes");
	theurl = theurl + '&wf_bouquet=' + Trim($("wf_bouquet").value);
	theurl = theurl + '&wf_garter_yes=' + get_radio_value("wf_garter_yes");
	theurl = theurl + '&wf_garter=' + Trim($("wf_garter").value);
	theurl = theurl + '&wf_garter_toss_yes=' +get_radio_value("wf_garter_toss_yes");
	theurl = theurl + '&wf_garter_toss=' + Trim($("wf_garter_toss").value);
	theurl = theurl + '&wf_cut_cake_yes=' + get_radio_value("wf_cut_cake_yes");
	theurl = theurl + '&wf_cut_cake=' + Trim($("wf_cut_cake").value);
	theurl = theurl + '&wf_requests=' + Trim($("wf_requests").value);
	theurl = theurl + '&wf_hall=' + Trim($("wf_hall").value);
	theurl = theurl + '&wf_hall_address=' + Trim($("wf_hall_address").value);
	theurl = theurl + '&wf_hall_phone=' + Trim($("wf_hall_phone").value);
	theurl = theurl + '&wf_manager=' + Trim($("wf_manager").value);
	theurl = theurl + '&wf_up_down=' + Trim($("wf_up_down").value);
	theurl = theurl + '&wf_room=' + Trim($("wf_room").value);

}

function checkstyle(id){
	if (id.getStyle('display') == 'block'){
		return true;
	}else{
		return false;
	}
}

function get_radio_value(radioObj){
	radioObj = document.forms['form1'][radioObj];
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked){
		
			return radioObj.value;
		}else{
			return "";
		}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
		
			return radioObj[i].value;
		}
	}
	return "";	
}

function Trim(s) 
   {
   	// Remove leading spaces and carriage returns
   	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
   	 { s = s.substring(1,s.length); }
     
   	// Remove trailing spaces and carriage returns
 while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
   	 { s = s.substring(0,s.length-1); }
     
   	return s;
   }
