// All scripts in this file require prototype
//       some Require scrip.ac.lious
//

function fieldChange(a,b){
	
	$('nameType').innerHTML = a;
	$('example').innerHTML = b;
	
}

function markImage(idx,msg){
	$('whatSelected').innerHTML = msg + " image selected";
	$('imageMakeId').value = idx;
}

function addEmail(){
	$('exampleEmail').hide();
	$('topper').show();
}

function manualEmails(){

	// make a JSON holder for the email
	var temp = {idx: "m", fName: "", sName: "", eMail: ""}
	
	//get values
	var fname = prompt('please enter the first name of the person you wish to add...');
	var sname = prompt('and there last name...');
	var temail = prompt('also their email address...');
	
	//alert(fname + " " + sname + ", " + temail);
	
	//set values
	temp.fName = fname;
	temp.sName = sname;
	temp.eMail = temail;
	
	//alert(temp.eMail);
	
	//add values to the email stack
	emails.add(temp);
	updateCount();
}

function outlookEmails(){
	window.open("add-emails.php?type=o","","width=420,height=520,resizable")
}

function hotmailEmails(){
	window.open("add-emails.php?type=h","","width=420,height=520,resizable")
}

function yahooEmails(){
	window.open("add-emails.php?type=y","","width=420,height=520,resizable")
}
function googleEmails(){
	window.open("add-emails.php?type=g","","width=420,height=520,resizable")
}


function ajaxFileBit(){
	$('csvFile').hide();
	$('ajaxResults').innerHTML = "Sending file...";
}

function swapButtons(){
	$('csvFile').hide();
	$('submitButton').show();
	$('tryagain').show();
}

function backtocsv(){
	$('csvFile').clear();
	$('csvFile').show();
	$('submitButton').hide();
	$('tryagain').hide();
}

function swapButtons2(){
	$('csvFile2').hide();
	$('submitButton2').show();
	$('tryagain2').show();
}

function backtocsv2(){
	$('csvFile2').clear();
	$('csvFile2').show();
	$('submitButton2').hide();
	$('tryagain2').hide();
}

function swapButtons3(){
	$('csvFile3').hide();
	$('submitButton3').show();
	$('tryagain3').show();
}

function backtocsv3(){
	$('csvFile3').clear();
	$('csvFile3').show();
	$('submitButton3').hide();
	$('tryagain3').hide();
}

var secs
var timerID = null
var timerRunning = false
var delay = 1000

function IntTimer(s,fun)
{
    // Set the length of the timer, in seconds
    secs = s
    StopTheClock()
    StartTheTimer(fun)
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer(fun)
{
    if (secs==0)
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
	Effect.Fade('flashMob');
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function flashMob(msg){
	
	var ele = $('flashMob');
	Effect.Appear('flashMob');
	ele.innerHTML = msg;
	IntTimer(5,"");
	updateCount();

}

function outlookAddEmail(idx){

	//set our ele
	var ele = $('row-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: '#3B5998', color: '#fff'});
	
	//get decendents so we can collect our values
	var bace = ele.descendants();
	
	// make a JSON holder for the email
	var email = {idx: idx, fName: "", sName: "", eMAil: ""}
	
	//set values
	email.fName = bace[0].innerHTML.stripTags();
	email.sName = bace[1].innerHTML.stripTags();
	email.eMail = bace[2].innerHTML.stripTags();
	
	//add values to the email stack
	emails.add(email);

	// replace the add button with a remove
	bace[3].innerHTML = "<a href='#row-"+idx+"' onClick='outlookRemoveEmail("+idx+")' style='color: #fff;'>Remove Email</a>";

}

function outlookRemoveEmail(idx){
	var ele = $('row-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: "",color: "#000"});
	
	//remove email from array
	emails.remove(idx);
	
	var bace = ele.descendants();
	bace[3].innerHTML = "<a href='#row-"+idx+"' onClick='outlookAddEmail("+idx+")'>Add Email</a>";
}

function hotmailAddEmail(idx){

	//set our ele
	var ele = $('hrow-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: '#3B5998', color: '#fff'});
	
	//get decendents so we can collect our values
	//var bace = ele.descendants();
	var bace = $A(ele.getElementsByTagName('td')).each(Element.extend);
	// make a JSON holder for the email
	var email = {idx: idx, fName: "", sName: "", eMAil: ""}
	
	//set values
	email.fName = bace[0].innerHTML.stripTags();
	email.sName = bace[1].innerHTML.stripTags();
	email.eMail = bace[2].innerHTML.stripTags();
	
	//add values to the email stack
	emails.add(email);

	// replace the add button with a remove
	bace[3].innerHTML = "<a href='#hrow-"+idx+"' onClick='hotmailRemoveEmail("+idx+")' style='color: #fff;'>Remove Email</a>";

}

function hotmailRemoveEmail(idx){
	var ele = $('hrow-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: "",color: "#000"});
	
	//remove email from array
	emails.remove(idx);
	
	var bace = $A(ele.getElementsByTagName('td')).each(Element.extend);
	bace[3].innerHTML = "<a href='#hrow-"+idx+"' onClick='hotmailAddEmail("+idx+")'>Add Email</a>";
}

function yahooAddEmail(idx){

	//set our ele
	var ele = $('yrow-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: '#3B5998', color: '#fff'});
	
	//get decendents so we can collect our values
	//var bace = ele.descendants();
	var bace = $A(ele.getElementsByTagName('td')).each(Element.extend);
	// make a JSON holder for the email
	var email = {idx: idx, fName: "", sName: "", eMAil: ""}
	
	//set values
	email.fName = bace[0].innerHTML.stripTags();
	email.sName = bace[1].innerHTML.stripTags();
	email.eMail = bace[2].innerHTML.stripTags();
	
	//add values to the email stack
	emails.add(email);

	// replace the add button with a remove
	bace[3].innerHTML = "<a href='#yrow-"+idx+"' onClick='yahooRemoveEmail("+idx+")' style='color: #fff;'>Remove Email</a>";

}

function yahooRemoveEmail(idx){
	var ele = $('yrow-'+idx);
	
	//change style
	ele.setStyle({backgroundColor: "",color: "#000"});
	
	//remove email from array
	emails.remove(idx);
	
	var bace = $A(ele.getElementsByTagName('td')).each(Element.extend);
	bace[3].innerHTML = "<a href='#yrow-"+idx+"' onClick='yahooAddEmail("+idx+")'>Add Email</a>";
}

function selectAll(div,link){
	var link = $(link);
	if(link.innerHTML == "Select All"){
		link.innerHTML = "De-select All";
	} else { link.innerHTML = "Select All"; }
	var ele = $(div);
	var top = $A(ele.getElementsByTagName('tr')).each(Element.extend);
	top.each(function(s){
		var tds = $A(s.getElementsByTagName('td')).each(Element.extend);
		var action = tds[3];
		var a = $A(action.getElementsByTagName('a')).each(function(p){
			eval(p.readAttribute('onClick'));
		});
	});
}

function updateCount(){
	$('emailCount').innerHTML = "you now have emails in your list...";
}

var emails = {
	
	list: [],
	add: function(email){
		emails.list.push(email);
	},
	remove: function(idx){
		emails.list.each(function(s){
			if(s.idx == idx){
				emails.list = emails.list.without(s);
			}
		});
	},
	updateList: function(){
		var cur = $F('emailList');
		var add = "";
		emails.list.each(function(s){
			if(typeof s.sName=='undefined'){ s.sName = ""; }
			add += s.fName + " " + s.sName + ", " + s.eMail + "\n";
		});
		$('emailList').value = cur + "\n" + add;
		
		// wipe array to avoid dups
		emails.list = [];
		
		//alert(cur + "\n" + add);
	},
	provideCount: function(){
		var num = emails.list.size();
		return num;
	},
	debug: function(){
		emails.list.each(function(s){
			alert(s.eMail);
		});
	}

}

function buildForm(){
	$('formEnder').innerHTML = $('formHolder').innerHTML;
	$('formHolder').innerHTML = "";
	
	//add our emails to email list.
	emails.updateList();
	
	$('form2').submit();
}

var step = {

	current: 0,
	steps: [],
	house: 'ourForm',
	carpet: new Template("<div style='width: 20%;margin: auto; height: 20px;background-color: #ccc; border: 1px solid #999; border-top: none; color:#000; font-size: 10px;'><span style='float: right'><a href='#a#{nCount}' name='#a#{count}' onClick='step.next();'>Next Step</a></span></div>"),
	
	next: function(){
		var sNext = step.current + 1;
		if(sNext > step.steps.length){
			alert('no more steps');
		} else {
			$(step.steps[sNext].div).show();
			step.current = sNext;
		}
	},
	
	layCarpet: function(ele,idx){
		var out = step.carpet.evaluate({count: idx, nCount: idx + 1});
		$(ele).innerHTML = $(ele).innerHTML + out;
	},
	
	load: function(){
		var A = $A($(step.house).getElementsByTagName('div'));
		A.each(
			function(s, index){
				var a = { div: ""}
				if(s.hasAttribute('step')){
					a.div = s.id;
					step.steps.push(a);
					step.layCarpet(s.id,index);
					if(step.steps.length > 1){
						$(s.id).hide();
					}
				}
			});
		step.current = 0;
	}
}

function parent(id){
	var ele = $(id);
	var d = ele.ancestors();
	alert(d[0].id);
}

function secondAjax(id,string){
	new Ajax.Updater(id, './ajax/csv2.php?' + string);
}

function checkForm(){
	
	// we need to dump email array to textbox to avoid a fail
	emails.updateList();
	
	var errors = "Sorry, there where errors with your post\n\n";
	var haserrors = 0;
	[{id: 'fname', text: 'Name can not be blank'},
	 {id: 'fline1', text: 'Address line 1 can not be blank'},
	 {id: 'ftown', text: 'Town can not be blank'},
	 {id: 'fcounty', text: 'County can not be blank'},
	 {id: 'fcode', text: 'Postal Code can not be blank'},
	 {id: 'emailList', text: 'You must add emails to send the eCard too'},
	 {id: 'imageMakeId', text: 'You must select an eCard to send'}].each(function(s){
	
		if($(s.id).value == ""){
			errors += s.text + "\n";
			haserrors = 1;
		}
	});
	errors += "\nPlease correct these errors and try again";
	if(haserrors){
		alert(errors);
	} else {
		alert('Everything is ok');
		$('checker').hide();
		$('sendtheform').show();
	}
}

function sendEmails(){
		var cur = opener.document.getElementById('emailList').value;
		var add = "";
		emails.list.each(function(s){
			if(typeof s.lName=='undefined'){ s.lName = ""; }
			add += s.fName + " " + s.lName + ", " + s.eMail + "\n";
		});
		opener.document.getElementById('emailList').value = cur + "\n" + add;
		opener.document.getElementById('emailCount').innerHTML = "you now have emails in your list...";
		alert('Emails added!');
}
	