function loadNote(id) {
	var url = HOME_DIR+'notes/view/'+id;
	/*
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
		$('note_loader').update(transport.responseText);
		new Dialog.Box('note_loader');
		$('note_loader').show();
	  }
	});
	*/
	noteLoader = Ext.getCmp('noteobject'+id);
	if ( noteLoader == null ) {
		noteLoader = new Dialog2(url,id);
	}
	noteLoader.show();
}
function showNewsletterForm() {
	if ( $('newsletter_form') != null ) {
		new Dialog.Box('newsletter_form');
		$('newsletter_form').show();
	}
};
function closeNewsletterForm() {
	if ( $('newsletter_form') != null ) {
		$('newsletter_form').hide();
	}
};
function showThongDiaryForm() {
	if ( $('thongdiary_form') != null ) {
		new Dialog.Box('thongdiary_form');
		$('thongdiary_form').show();
	}
};
function closeThongDiaryForm() {
	if ( $('thongdiary_form') != null ) {
		$('thongdiary_form').hide();
	}
};
function submitNewsletterSignup() {
	if ( signup.isValid() ) {
		signup.submit({
			url: HOME_DIR+'newsletter_registrations/add',
			success: function() {
				$('signup_form').update('<strong>Thank you for registering for our newsletter and special offers.</strong><br /><br /><a href="#" onclick="closeNewsletterForm();return false;"><img src="'+HOME_DIR+'images/close_btn.gif"></a>');
				$('signup_form').style.textAlign = 'center';
			},
			failure: function() {
				Ext.Msg.alert('Error!','Unable to submit information');
			}
		});
	}
};
function submitNewsletterOptout() {
	if ( optout.isValid() ) {
		optout.submit({
			url: HOME_DIR+'newsletter_registrations/remove',
			success: function(f,a) {
				$('optout_form').update('<br /><strong>Your email address has been removed from our mailing list.</strong><br /><br /><a href="#" onclick="closeNewsletterForm();return false;"><img src="'+HOME_DIR+'images/close_btn.gif"></a>');
				$('optout_form').style.textAlign = 'center';
			},
			failure: function(f,a) {
				if ( typeof(a.result.error) != 'undefined' ) {
					Ext.Msg.alert('Error!',a.result.error.reasons);
				}
				else {
					Ext.Msg.alert('Error!','Unable to submit information');
				}
			}
		});
	}
};
function submitThongDiary() {
	if ( thong.isValid() ) {
		thong.submit({
			url: HOME_DIR+'diaries/submit',
			params: {
				noxml: true
			},
			success: function() {
				$('thong_form').update('<strong>Thank you for submitting your story.</strong><br /><br /><a href="#" onclick="closeThongDiaryForm();return false;"><img src="'+HOME_DIR+'images/close_btn.gif"></a>');
				$('thong_form').style.textAlign = 'center';
			},
			failure: function() {
				Ext.Msg.alert('Error!','Unable to submit information');
			}
		});
	}
};
Event.observe(window,'load',function() {
	/*
	$('newsletter_form').style.width = '534px';
	$('newsletter_form').style.height = '564px';
	*/
	showNewsletterForm();
	closeNewsletterForm();
	/*
	$('thongdiary_form').style.width = '534px';
	$('thongdiary_form').style.height = '564px';
	*/
	showThongDiaryForm();
	closeThongDiaryForm();
});
var signup;
var thong;
Array.prototype.search = function(s,q){
  var len = this.length;
  for(var i=0; i<len; i++){
    if(this[i].constructor == Array){
      if(this[i].search(s,q)){
        return true;
        break;
      }
     } else {
       if(q){
         if(this[i].indexOf(s) != -1){
           return true;
           break;
         }
      } else {
        if(this[i]==s){
          return true;
          break;
        }
      }
    }
  }
  return false;
};
Ext.apply(Ext.form.VTypes, {
	usState: function(val, field) {
		if ( field.id == 'country' ) {
			if ( val == 'UNITED STATES' ) {
				if ( Ext.getCmp('state').value == '' ) {
					signup.markInvalid([{id:'state',msg:'You must select a state'}]);
				}
				else if ( ! StatesData.search(Ext.getCmp('state').value) ) {
					signup.markInvalid([{id:'state',msg:'You must select a state'}]);
				}
			}
			return true;
		}
		else {
			if ( Ext.getCmp('country').value == 'UNITED STATES' ) {
				if ( val == '' ) {
					return false;
				}
				else if ( ! StatesData.search(val) ) {
					return false;
				}
			}
			return true;
		}
	},
	usstateText: 'You must select a state'
});
Ext.onReady(function(){
    Ext.QuickTips.init();

	signup = new Ext.form.BasicForm('signupform');
	optout = new Ext.form.BasicForm('optoutform');
	var firstnameField = new Ext.form.TextField({
		name: 'firstname',
		id: 'firstname',
		allowBlank: false,
		minLength: 1,
		maxLength: 35,
		width: 200,
		blankText: 'First Name is required',
		applyTo: 'firstname',
		vtype:'alphanum'
	});
	var lastnameField = new Ext.form.TextField({
		name: 'lastname',
		id: 'lastname',
		allowBlank: false,
		minLength: 1,
		maxLength: 35,
		width: 200,
		blankText: 'Last Name is required',
		applyTo: 'lastname',
		vtype:'alphanum'
	});
	var genderCombo = new Ext.form.ComboBox({
		applyTo: 'gender',
		name: 'gender',
		id: 'gender',
		allowBlank: false,
		width: 200,
		triggerAction: 'all',
		mode: 'local',
		store: [ ['F','Female'], ['M','Male'] ],
		blankText: 'You must select a Gender'
	});
	var address1Field = new Ext.form.TextField({
		name: 'address1',
		id: 'address1',
		allowBlank: false,
		minLength: 3,
		maxLength: 100,
		width: 200,
		blankText: 'Address is required',
		applyTo: 'address1'
	});
	var address2Field = new Ext.form.TextField({
		name: 'address2',
		id: 'address2',
		maxLength: 100,
		width: 200,
		applyTo: 'address2'
	});
	var cityField = new Ext.form.TextField({
		name: 'city',
		id: 'city',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 200,
		blankText: 'City is required',
		applyTo: 'city'
	});
	var stateCombo = new Ext.form.ComboBox({
		name: 'state',
		id: 'state',
		applyTo: 'state',
		width: 200,
		forceSelection: false,
		triggerAction: 'all',
		vtype: 'usState',
		mode: 'local',
		store: StatesData
	});
	var countryCombo = new Ext.form.ComboBox({
		name: 'country',
		id: 'country',
		applyTo: 'country',
		value: 'UNITED STATES',
		width: 200,
		triggerAction: 'all',
		vtype: 'usState',
		mode: 'local',
		store: CountryData
	});
	var zipcodeField = new Ext.form.TextField({
		name: 'zipcode',
		id: 'zipcode',
		minLength: 3,
		maxLength: 35,
		width: 200,
		applyTo: 'zipcode',
		vtype:'alphanum'
	});
	var emailField = new Ext.form.TextField({
		name: 'email',
		id: 'email',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 200,
		vtype: 'email',
		blankText: 'Email is required',
		applyTo: 'email'
	});
	var optoutemailField = new Ext.form.TextField({
		name: 'optout_email',
		id: 'optout_email',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 200,
		vtype: 'email',
		blankText: 'Email is required',
		applyTo: 'optout_email'
	});
	signup.add(firstnameField, lastnameField, genderCombo, address1Field, address2Field, cityField, stateCombo, countryCombo, zipcodeField, emailField);
	signup.render();
	optout.add(optoutemailField);
	optout.render();

	thong = new Ext.form.BasicForm('thongform');
	var thong_firstnameField = new Ext.form.TextField({
		name: 'thong_firstname',
		id: 'thong_firstname',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 150,
		blankText: 'First Name is required',
		applyTo: 'thong_firstname',
		vtype:'alphanum'
	});
	var thong_lastnameField = new Ext.form.TextField({
		name: 'thong_lastname',
		id: 'thong_lastname',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 150,
		blankText: 'Last Name is required',
		applyTo: 'thong_lastname',
		vtype:'alphanum'
	});
	var thong_genderCombo = new Ext.form.ComboBox({
		applyTo: 'thong_gender',
		name: 'thong_gender',
		id: 'thong_gender',
		allowBlank: false,
		triggerAction: 'all',
		mode: 'local',
		store: [ ['F','Female'], ['M','Male'] ],
		blankText: 'You must select a Gender'
	});
	var thong_emailField = new Ext.form.TextField({
		name: 'thong_email',
		id: 'thong_email',
		allowBlank: false,
		minLength: 3,
		maxLength: 35,
		width: 150,
		vtype: 'email',
		blankText: 'Email is required',
		applyTo: 'thong_email'
	});
	var thong_storyField = new Ext.form.TextArea({
		name: 'thong_story',
		id: 'thong_story',
		minLength: 3,
		width: 300,
		height: 150,
		allowBlank: false,
		applyTo: 'thong_story'
	});
	thong.add(thong_firstnameField, thong_lastnameField, thong_genderCombo, thong_emailField, thong_storyField);
	thong.render();
});