// JavaScript Document
var song_array = new Array(); 
var lastid = -1;
var emailsent = false;
var songlistadded = false;
var songsaddedtolist = false;
var thankyoupage = 'http://www.dancepartydjs.com/celebrations_fld/weddings_fld/thankyou.asp';
var ct_id;
window.addEvent('domready', function(){
	$('add_song').setStyle('display', 'none');
	
});

function buildsong(sgl_id,sgl_title,sgl_artist){
	lastid = lastid + 1;
	song_array[lastid] = new Array (sgl_id,sgl_title,sgl_artist);

};

function startsong(){
		$('add_song').setStyle('display', '');
		$('songAdditions').setStyle('display', 'none');
		$('songID_text').set('text', lastid + 2);
}

function addsong(){
	FormUpdated=true;
	error_array = "";
	checktext("sgl_title");
	checktext("sgl_artist");
	if (error_array != ""){
		showerrors();
		alert('You have some errors in your form. Please correct.');
		return;
	}
	
	var sgl_artist = $("sgl_artist").value;
	var sgl_title = $("sgl_title").value;
	lastid = lastid + 1;
	song_array[lastid] = new Array ('',sgl_title,sgl_artist);
	$("sgl_artist").value = '';
	$("sgl_title").value = '';
	
	//get the class to use
	//if (isEven(lastid)){
		//theclass = 'row2'
	//}else{
		theclass = 'row1'
	//};
	
	var song_row  = new Element('div', {
		id: 'song_row' + lastid
		});
	
	var song_td_count  = new Element('div', {
		id: 'song_td_count' + lastid
		});
	var song_td_title  = new Element('div', {
		id: 'song_td_title' + lastid
		});
	var song_td_artist  = new Element('div', {
		id: 'song_td_artist' + lastid
		});
	var song_td_edit  = new Element('div', {
		id: 'song_td_edit' + lastid
		});
	var song_td_del  = new Element('div', {
		id: 'song_td_del' + lastid
		});
	var song_td_clear = new Element('div', {
		id: 'song_td_clear' + lastid
		});
		
	song_row.inject($('add_song'), 'before');
	
	song_td_count.inject(song_row);
	song_td_title.inject(song_td_count, 'after');
	song_td_artist.inject(song_td_title, 'after');
	song_td_edit.inject(song_td_artist, 'after');
	song_td_del.inject(song_td_edit, 'after');
	song_td_clear.inject(song_td_del, 'after');
	
	//IE Errored out when i set everything up as i built the element so i guess i have to do it one at a time. 
	var label = eval(lastid + 1);
	$('song_td_count' + lastid).set('class', 'songID ' + theclass);
	$('song_td_count' + lastid).set('html','<label>' +  label + '</label>');
	$('song_td_title' + lastid).set('class', theclass + ' songtitle');
	$('song_td_title' + lastid).set('html', sgl_title);
	$('song_td_artist' + lastid).set('class', theclass + ' artist');
	$('song_td_artist' + lastid).set('html', sgl_artist);
	$('song_td_edit' + lastid).set('class', theclass  + ' greenButton');
	$('song_td_edit' + lastid).set('html', '<img src="/common_fld/edit-accept.png" width="12" height="12" style="cursor:pointer;" onClick="editsong(' + lastid + ')">');
	$('song_td_del' + lastid).set('class', theclass + ' redButton');
	$('song_td_del' + lastid).set('html', '<img src="/common_fld/delete_cross.png" width="12" height="12" style="cursor:pointer;" onClick="deletesong(' + lastid + ')">');
	$('song_td_clear' + lastid).setStyle('clear', 'both');
	song_row.set('class',theclass);
	
	$('add_song').setStyle('display', 'none');
	$('songAdditions').setStyle('display', '');	
	
}

function deletesong(id){
	FormUpdated=true;
	song_array[id][1] ='';
	song_array[id][2] ='';
	$('song_row' + id).dispose();
}

function editsong(id){
	FormUpdated=true;
	$('song_td_title' + id).set('html', '<input name="song_' + id + 'title" type="text" id="song_' + id + 'title" size="12" class="text">');
	$('song_td_artist' + id).set('html', '<input name="song_' + id + 'artist" type="text" id="song_' + id + 'artist" size="12" class="text">');
	$('song_td_edit' + id).set('html', '<img src="/common_fld/edit-accept_new.png" width="12" height="12" style="cursor:pointer;" onClick="editsongsave(' + id + ')">');
	
	$("song_" + id + "title").value = song_array[id][1];
	$("song_" + id + "artist").value = song_array[id][2];
}

function editsongsave(id){
	error_array = "";
	checktext("song_" + id + "title");
	checktext("song_" + id + "artist");
	if (error_array != ""){
		showerrors();
		alert('You have some errors in your form. Please correct.')	
	}else{
		var song_title = $("song_" + id + "title").value;
		var song_artist = $("song_" + id + "artist").value;
		song_array[id][1] = song_title;
		song_array[id][2] = song_artist;
		$('song_td_title' + id).set('html',song_title);
		$('song_td_artist' + id).set('html',song_artist);
		$('song_td_edit' + id).set('html','<img src="/common_fld/edit-accept.png" width="12" height="12" style="cursor:pointer;" onClick="editsong(' + id + ')">');
	}
	
}

function removesong(){
	$("sgl_artist").value = '';
	$("sgl_title").value = '';
	$('add_song').setStyle('display', 'none');
	$('songAdditions').setStyle('display', '');
}

function check_save(){
	if($("sg_save").checked){
		$('saverow1').setStyle('display', '');
		$('saverow2').setStyle('display', '');
	}else{
		$('saverow1').setStyle('display', 'none');
		$('saverow2').setStyle('display', 'none');
	}
}


function isEven(num) {
  return !(num % 2);
}


function submittheform(){
	
	var action;
	var url;
	error_array = "";
	var ct_id = $("ct_id").value;
	var sg_id = $("sg_id").value;

	if (sg_id == ""){
		action = "add";
	}else{
		action = "edit";
	}
	
	
	if (song_array.length == 0){
		alert("You need to have at least one song on your list");
		return;
	}
	checktext("ct_bride_first_name");
	checktext("ct_bride_last_name");
	checktext("ct_groom_first_name");
	checktext("ct_groom_last_name");
	checktext("ct_wedding_date");
	
	
	var ct_bride_first_name = $("ct_bride_first_name").value;
	var ct_bride_last_name = $("ct_bride_last_name").value;
	var ct_groom_first_name = $("ct_groom_first_name").value;
	var ct_groom_last_name = $("ct_groom_last_name").value;
	var ct_wedding_date = $("ct_wedding_date").value;
	
	try
	{
		var sg_save = $("sg_save").checked;
	}
	catch(err)
	{
		var sg_save = false;
	}
	
	if(sg_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 (action == "add"){	
				//create the user account
				theurl = "/moo/song_list.asp";
				theurl = theurl + '?ct_bride_first_name=' + ct_bride_first_name;
				theurl = theurl + '&ct_bride_last_name=' + ct_bride_last_name;
				theurl = theurl + '&ct_groom_first_name=' + ct_groom_first_name;
				theurl = theurl + '&ct_groom_last_name=' + ct_groom_last_name;
				theurl = theurl + '&ct_wedding_date=' + ct_wedding_date;
				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) {
						add_songs_list(id,ct_bride_first_name,ct_bride_last_name,ct_groom_first_name,ct_groom_last_name,ct_wedding_date);
					},
					onFailure: function() {
						alert('Error creating user account.');
						return;
					}
				});
				req.send();
		
	}else{
		update_songs_list(ct_id,ct_bride_first_name,ct_bride_last_name,ct_groom_first_name,ct_groom_last_name,ct_wedding_date,sg_id);
	}	
	
	//send out the email
	sendemail();
}

function add_songs_list(id,ct_bride_first_name,ct_bride_last_name,ct_groom_first_name,ct_groom_last_name,ct_wedding_date,sg_id){

	//create the user account
	theurl = "/moo/song_list.asp";
	theurl = theurl + '?ct_bride_first_name=' + ct_bride_first_name;
	theurl = theurl + '&ct_bride_last_name=' + ct_bride_last_name;
	theurl = theurl + '&ct_groom_first_name=' + ct_groom_first_name;
	theurl = theurl + '&ct_groom_last_name=' + ct_groom_last_name;
	theurl = theurl + '&ct_wedding_date=' + ct_wedding_date;
	theurl = theurl + '&ct_id=' + id;
	theurl = theurl + '&action=add_songlist';
	var req = new Request({url:theurl, 
		onSuccess: function(id) {
			songlistadded = true;
			songstolist(id);
		},
		onFailure: function() {
			alert('Error adding Song List.');
			return;
		}
	});
	req.send();
}

function update_songs_list(ct_id,sg_bride_first_name,sg_bride_last_name,sg_groom_first_name,sg_groom_last_name,sg_wedding_date,sg_id){
	
	var ct_email = $('ct_email').value;
	var ct_password = $('ct_password').value;
	
	theurl = "/moo/song_list.asp";
	theurl = theurl + '?ct_bride_first_name=' + sg_bride_first_name;
	theurl = theurl + '&ct_bride_last_name=' + sg_bride_last_name;
	theurl = theurl + '&ct_groom_first_name=' + sg_groom_first_name;
	theurl = theurl + '&ct_groom_last_name=' + sg_groom_last_name;
	theurl = theurl + '&ct_wedding_date=' + sg_wedding_date;
	theurl = theurl + '&ct_id=' + ct_id;
	theurl = theurl + '&ct_email=' + ct_email;
	theurl = theurl + '&ct_password=' + ct_password;
	theurl = theurl + '&action=edit_song_list';
	//location.href = theurl;
	var req = new Request({url:theurl, 
		onSuccess: function() {
			 songlistadded  = true;
			 songstolist(sg_id);
		
		},
		onFailure: function() {
			alert('Error editing song list.');
			return;
		}
	});
	req.send();
}

function songstolist(sg_id){
	
	for(i=0; i<song_array.length; i++) {
		

		theurl = "/moo/song_list.asp";
		theurl = theurl + '?sgl_id=' + song_array[i][0];
		theurl = theurl + '&sgl_artist=' + song_array[i][2];
		theurl = theurl + '&sgl_title=' + song_array[i][1];
		theurl = theurl + '&sg_id=' + sg_id;
		theurl = theurl + '&action=songstolist';
		//alert(theurl);
		var req = new Request({url:theurl, 				
			onFailure: function() {
				//alert('Error adding songs to the song list.');
				//return;
			}
		});
		req.send();
	};
	songsaddedtolist   = true;

	nextpage();
}

function sendemail(){
		var ct_bride_first_name = $("ct_bride_first_name").value;
		var ct_bride_last_name = $("ct_bride_last_name").value;
		var ct_groom_first_name = $("ct_groom_first_name").value;
		var ct_groom_last_name = $("ct_groom_last_name").value;
		var ct_wedding_date = $("ct_wedding_date").value;
		try{
			var ct_email = $("ct_email").value;
		}catch(err){
			var ct_email = '';
		}
		
		var theurl = "/moo/song_list.asp";
			theurl = theurl + '?ct_bride_first_name=' + ct_bride_first_name;
			theurl = theurl + '&ct_bride_last_name=' + ct_bride_last_name;
			theurl = theurl + '&ct_groom_first_name=' + ct_groom_first_name;
			theurl = theurl + '&ct_groom_last_name=' + ct_groom_last_name;
			theurl = theurl + '&ct_wedding_date=' + ct_wedding_date;
			theurl = theurl + '&ct_email=' + ct_email;
			for(i=0; i<song_array.length; i++) {
				theurl = theurl + '&sgl_title' + i + '=' + song_array[i][1];
				theurl = theurl + '&sgl_artist' + i + '=' + song_array[i][2];
			};
			theurl = theurl + '&action=sendemail';
			
			
			var req = new Request({url:theurl, 
			onSuccess: function() {
			
				 emailsent   = true;
				nextpage();
			},
			onFailure: function() {
					alert('Error sending Email.');
					return;
				}
			});
			req.send();
}

function nextpage(){

	if ((emailsent) && (songlistadded) && (songsaddedtolist)){
		location.href = 'thankyou.asp?msg=Song List Updated&ct_id=' + ct_id;
	};
}

