center = function( styles ) {
	return { 
		'left': Math.round( ( ( window.getScrollWidth()-parseFloat( styles['width'] ) ) / 2 ), 1 ), 
		'top': Math.round( ( ( window.getScrollHeight()-parseFloat( styles['height'] ) ) / 2 ), 1 )
	};
}
/*
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash = new Array();
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
*/
foggy = function() {
	return new Element( 'div', { 'id': 'foggy' } ).setStyles({
		'position': 'absolute',
		'left': 0,
		'top': 0,
		'width': window.getScrollWidth(),
		'height': window.getScrollHeight(),
		'background-color': '#333333',
		'opacity': 1,
		'z-index': 999
	}).inject( document.body );
}

function radio_value ( form, name ) {
	eval("var radio = document." + form + "." + name);
	for( i=0; i < radio.length; i++) {
		if( radio[i].checked )
			return radio[i].value;
	}
}

ajaxalert = function( msg ) {

	var msg = new Element( 'div' ).setStyles({
		'padding': '4px',
		'background-color': '#ff9703',
		'color': '#ffffff',
		'font-size': '12px',
		'margin-bottom': '10px',
		'margin-top': '10px'
	}).setHTML( msg ).injectTop( $( document.forms[0] ) );
	del = function() { msg.remove(); }.delay(5000);
	
}

function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

Element.extend({
	'insertAtCursor': function( txt ) {
		if( document.selection ) {
			this.focus();
			// get current selection
			var range = document.selection.createRange();
			// create a selection of the whole textarea
			var range_all = document.body.createTextRange();
			range_all.moveToElementText(this);
			// calculate selection start point by moving beginning of range_all to beginning of range
			//for( var sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start ++ ) {
			//	if( this.getTag() == 'textarea' ) range_all.moveStart('character', 1);
			//	else range_all.moveStart(
			
			//this.value = this.value.substring(0, sel_start) + txt + this.value.substring( sel_start, this.value.length );
			//sel.text = txt;
			//sel.caretPos.text = sel.caretPos.text.charAt( sel.caretPos.text.length -1 ) == ' ' ? txt + ' ' : txt;
			//sel = null;
		} else if ( this.selectionStart || this.selectionStart == '0' ) {
			var start = this.selectionStart;
			var end = this.selectionEnd;
			this.value = this.value.substring(0, this.selectionStart) + txt + this.value.substring( this.selectionEnd, this.value.length );
			this.setSelectionRange( (start+1), (end+1) );
		} else {
			this.value += txt;
		}
	}
});

//var GET = getUrlVars();

window.addEvent('domready', function(e) {

	$$('input.DatePicker').each( function(el){
		new DatePicker(el);
	});
	
	var num_postcards = 1;
	
	if( $('bottom') ) {
		$('add').setStyle( 'cursor', 'pointer' );
		$('add').addEvent('click', function(e) {
			// increase the pointer
			num_postcards++;
			$('num_postcards').value = num_postcards;
		
			// number
			new Element( 'span' ).setText( num_postcards + '.' ).injectBefore('bottom');
			// day to send
			new Element( 'label' ).setText('Day to Send').injectAfter( 
				new Element( 'input', { 'type': 'text', 'class': 'DatePicker', 'name': 'schedule_send_' + num_postcards } ).injectInside( 
					new Element( 'span' ).injectBefore('bottom') 
				)
			);
			// title
			new Element( 'label' ).setHTML('Card Title').injectAfter(
				new Element( 'input', { 'type': 'text', 'size': 30, 'name': 'title_' + num_postcards } ).injectInside(
					new Element( 'span' ).injectBefore('bottom')
				)
			);
			// library
			new Element( 'label' ).setHTML('Postcard').injectAfter(
				new Element( 'input', { 'type': 'text', 'class': 'LibPicker', 'name': 'postcard_' + num_postcards } ).injectInside(
					new Element( 'span', { 'class': 'nofloat' } ).injectBefore('bottom')
				)
			);

			// add the date picker
			$$('input.DatePicker').each( function(el){
				new DatePicker(el);
			});

		});
	}
	
});
