function copyToRelationship() {
	$('birth_first_name1').value = $('birth_first_name').value;
	$('birth_middle_names1').value = $('birth_middle_names').value;
	$('birth_last_name1').value = $('birth_last_name').value;
	$('current_first_name1').value = $('current_first_name').value;
	$('current_middle_names1').value = $('current_middle_names').value;
	$('current_last_name1').value = $('current_last_name').value;
	
	$('birth_date1[month]').value = $('birth_date[month]').value;
	$('birth_date1[day]').value = $('birth_date[day]').value;
	$('birth_date1[year]').value = $('birth_date[year]').value;
}

function hider()
    {
      if (document.getElementById('relationshipdiv').style.visibility != "hidden") {
        document.getElementById('relationshipdiv').style.visibility = "hidden";
      }
      else {
        document.getElementById('relationshipdiv').style.visibility = "visible";
      }

    }

function toggleDisplay(id, showType){
	if(!showType) {
		showType = 'block';
	}
	
	var obj = $(id);
	if( (obj.style.display == 'none') || ( (obj.style.display == '') && (obj.offsetWidth == 0) ) ) {
		obj.style.display = showType;
	} else {
		obj.style.display = 'none';
	}
}
 
 
function toggleBonusDisplay() {

	copyToRelationship();	

	toggleDisplay('birthNameCompatibility');	
	toggleDisplay('birthNameCompatibilityMessage');
	toggleDisplay('currentNameCompatibility');	
	toggleDisplay('currentNameCompatibilityMessage');
	toggleDisplay('birthDateCompatibility');	
	toggleDisplay('birthDateCompatibilityMessage');
	
}

function initialToggle(relStatus) {

	toggleDisplay('birthNameCompatibility');
	toggleDisplay('currentNameCompatibility');
	toggleDisplay('birthDateCompatibility');

	if(relStatus) {
		$('optionalnumerology_compatibility_reading').checked = true;
		toggleBonusDisplay();
	}
	
}

function submitFreebieNumerologySuccess(transport) {
	   
	if(transport.responseJSON) {
    	
    	if(transport.responseJSON.result) {
    		    		
    		// We have success, fill up the custom rows and submit the form
			$('custom row_id').value = transport.responseJSON.visitor.id;
			$('meta_required').value += ',custom row_id,custom coupon';
    		$('custom coupon').value = transport.responseJSON.visitor.id + '-' + transport.responseJSON.visitor.coupon;
    		
    		$('custom lifepath').value = transport.responseJSON.aweber.lifepath;
    		$('custom expression').value = transport.responseJSON.aweber.expression;
    		$('custom soulurge').value = transport.responseJSON.aweber.soulurge;
    		$('custom monthname').value = transport.responseJSON.aweber.monthname;
    		
    		$('freebie_numerology').action = 'http://www.aweber.com/scripts/addlead.pl';
    		$('freebie_numerology').submit();
    		
    	} else {
    		
    		// form validation failed
    		var errorMsg = '<h1 class="tinyh1">Please Correct the following errors</h1><hr /><ul>';
    		
    		var myErrors = transport.responseJSON.errors;
    		
			for(key in myErrors) {
				errorObj = myErrors[key];
				
				for(errorCode in errorObj) {
					var elName = ucwords(key.replace(/_/g, ' '));
					errorMsg += "<li>" + elName + '<br /><span style="color: #888;">' + errorObj[errorCode] + "</span></li>";
				}
				
			}
			
			errorMsg += '</ul><p style="text-align: center;"><a href="javascript:TINY.box.hide()">close</a></p>';

			// Hello word :)
			TINY.box.show(errorMsg,0,0,0,1);
    		
    	}
    } else {
    	alert(transport.responseText);
    }
}

function submitFreebieNumerologyFailure(transport) {
	alert('something went wrong');
}

function submitFreebieNumerology() {
	
	// Copy AWeber Form Variables to Hidden Freebie Form
	$('current_first_name').value = $F('name');
	$('current_last_name').value = $F('custom lastname');
	$('birth_full_name').value = $F('custom birthname');
	$('birth_date[month]').value = $F('custom month');
	$('birth_date[day]').value = $F('custom day');
	$('birth_date[year]').value = $F('custom year');
	$('email').value = $F('from');
	
    //remember to put a word separator between elements of the camelcase action name, per the ZF manual:
    var myAjax = new Ajax.Request(baseUrl + '/freebie/process/freebie/numerology',
    	 {
    		method: 'post', 
    		parameters: $('freebie_numerology').serialize(true),
    		onSuccess: submitFreebieNumerologySuccess,
    		onFailure: submitFreebieNumerologyFailure
    	});
   
    return false;
}

function submitFreebieNumerologyReadingSuccess(transport) {
	   
	if(transport.responseJSON) {
    	
    	if(transport.responseJSON.result) {
    		    		
    		// We have success, fill up the custom rows and submit the form    		
    		$('custom rowid').value = transport.responseJSON.visitor.id;
    		$('custom coupon').value = transport.responseJSON.visitor.id + '-' + transport.responseJSON.visitor.coupon;
    		
    		$('custom lifepath').value = transport.responseJSON.aweber.lifepath;
    		$('custom expression').value = transport.responseJSON.aweber.expression;
    		$('custom soulurge').value = transport.responseJSON.aweber.soulurge;
    		$('custom monthname').value = transport.responseJSON.aweber.monthname;
    		
    		$('meta_required').value += ',custom rowid,custom coupon';
    		
    		$('freebie_numerologyreading').action = 'http://www.aweber.com/scripts/addlead.pl';
    		$('freebie_numerologyreading').submit();
    		
    	} else {
    		
    		// form validation failed
    		var errorMsg = '<h1>Please Correct the following errors</h1><hr /><ul>';
    		
    		var myErrors = transport.responseJSON.errors;
    		
			for(key in myErrors) {
				errorObj = myErrors[key];
				
				for(errorCode in errorObj) {
					var elName = ucwords(key.replace(/_/g, ' '));
					errorMsg += "<li>" + elName + '<br /><span style="color: #888;">' + errorObj[errorCode] + "</span></li>";
				}
				
			}
			
			errorMsg += '</ul><p style="text-align: center;"><a href="javascript:TINY.box.hide()">close</a></p>';

			// Hello word :)
			TINY.box.show(errorMsg,0,0,0,1);
    		
    	}
    } else {
    	alert(transport.responseText);
    }
}

function submitFreebieNumerologyReadingFailure(transport) {
	alert('something went wrong');
}

function submitFreebieNumerologyReading() {
	
	// Copy AWeber Form Variables to Hidden Freebie Form
	$('current_first_name').value = $F('name');
	$('current_last_name').value = $F('custom lastname');
	$('birth_full_name').value = $F('custom birthname');
	$('birth_date[month]').value = $F('custom month');
	$('birth_date[day]').value = $F('custom day');
	$('birth_date[year]').value = $F('custom year');
	$('email').value = $F('from');
	
    //remember to put a word separator between elements of the camelcase action name, per the ZF manual:
    var myAjax = new Ajax.Request(baseUrl + '/freebie/process/freebie/numerology-reading',
    	 {
    		method: 'post', 
    		parameters: $('freebie_numerologyreading').serialize(true),
    		onSuccess: submitFreebieNumerologyReadingSuccess,
    		onFailure: submitFreebieNumerologyReadingFailure
    	});
   
    return false;
}

function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
	function ucwords( str ) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	    // +   improved by: Waldo Malqui Silva
	    // +   bugfixed by: Onno Marsman
	    // *     example 1: ucwords('kevin van zonneveld');
	    // *     returns 1: 'Kevin Van Zonneveld'
	    // *     example 2: ucwords('HELLO WORLD');
	    // *     returns 2: 'HELLO WORLD'
	 
	    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
	}

	
