function contactValidate(e){
	value = $('email').value;
	error = '';
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	if (apos < 1||dotpos-apos<2){
		error += '- The email provided is not valid' + '\n';
	}
	if(error){
		alert(error);
		Event.stop(e);
	}
}

function createValidate(e){
	value = $('email').value;
	error = '';
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	if (apos < 1||dotpos-apos<2){
		error += '- The email provided is not valid' + '\n';
	}
	if($('password').value != $('verify').value){
		error += '- Passwords do not match';
	}
	if(error){
		alert(error);
		Event.stop(e);
		return false;
	}
	return true;
}

function loginValidate(e){
	var error = '';
	if(!$F('login_email')){
		error += "You need to enter in your email\n";
	}
	if(!$F('login_password')){
		error += "You need to enter a password\n";
	}
	if(error){
		alert(error);
		Event.stop(e);
		return false;
	}
	return true;
}

function loadTabs(){
	searchTabs = new YAHOO.widget.TabView();
	searchTabs.on('click', tabChange);
		searchTabs.addTab( new YAHOO.widget.Tab({
			label:'Connections',
			//dataSrc:'users/functions.php?action=searchLast&last_name='+$F('search'),
			content:'Search Results',
			active:true
		}));
		searchTabs.addTab( new YAHOO.widget.Tab({
			label:'Groups',
			//dataSrc:'groups/functions.php?action=searchDesc&description='+$F('search'),
			content:'Search Results'
		}));
		searchTabs.addTab( new YAHOO.widget.Tab({
			label:'Contacts',
			//dataSrc:'contacts/functions.php?action=searchName&name='+$F('search'),
			content:'Search Results'
		}));
		searchTabs.addTab( new YAHOO.widget.Tab({
			label:'File Cabinet'
			//dataSrc:'objects/functions.php?action=searchName&name='+$F('search'),
		}));
	searchTabs.appendTo('tabs');
	checkTabs();
}

function changeIndex(){
	this.set('activeIndex', this.get('activeIndex'));
}
function tabChange(){
	var index = this.get('activeIndex');
	if(index == 0){
		//this.getTab(0).set('dataSrc', 'users/functions.php?action=searchLast&last_name='+$F('search'));
		new Ajax.Request('users/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	else if(index == 1){
		//this.getTab(1).set('dataSrc', 'groups/functions.php?action=searchDesc&description='+$F('search'));
		new Ajax.Request('groups/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	else if(index == 2){
		//this.getTab(2).set('dataSrc', 'contacts/functions.php?action=searchName&name='+$F('search'));
		new Ajax.Request('contacts/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	else if(index == 3){
		//this.getTab(2).set('dataSrc', 'contacts/functions.php?action=searchName&name='+$F('search'));
		new Ajax.Request('objects/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
}
function checkTabs(){
	var index = searchTabs.get('activeIndex');
	if(!index){
		index = 0;
	}
	searchTabs.set('activeIndex', 0);
	searchTabs.getTab(0).set('content', '');
	searchTabs.getTab(1).set('content', '');
	searchTabs.getTab(2).set('content', '');
	searchTabs.getTab(3).set('content', '');
	if(index == 0){
		new Ajax.Request('users/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	if(index == 1){
		new Ajax.Request('groups/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	if(index == 2){
		new Ajax.Request('contacts/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	if(index == 3){
		new Ajax.Request('objects/functions.php?action=search&search='+$F('search'), 
			{onComplete:function(t){searchTabs.getTab(index).set('content', t.responseText);}});
	}
	searchTabs.set('activeIndex', index);
}

function initCreate(){
	Event.observe('create', 'submit', createValidate)
}
function initSearch(){
	var loader = new YAHOO.util.YUILoader({
		require: ['tabview', 'connection'],
		loadOptional:true,
		onSuccess:function(){
			loadTabs();
		}
	});
	loader.insert();
}

function initChangePage(){
	var loader = new YAHOO.util.YUILoader({
		require: ['editor', 'container'],
		onSuccess:editor_init
	});
	loader.insert();
	YAHOO.util.Get.css('../css/blog.css');
}

function editor_init(){
	var state = 'off';
	var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event; 
	myEditor = new YAHOO.widget.Editor('content', {
    height: '300px',
    width: '600px',
    markup:'xhtml',
    dompath: true, //Turns on the bar at the bottom
    animate: true //Animates the opening, closing and moving of Editor windows
});
	myEditor._defaultToolbar.buttons[10].buttons.pop();
	 myEditor.on('toolbarLoaded', function() {
        var codeConfig = {
            type: 'push', label: 'Edit HTML Code', value: 'editcode'
        };
        YAHOO.log('Create the (editcode) Button', 'info', 'example');
        this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
        
        this.toolbar.on('editcodeClick', function() {
            var ta = this.get('element'),
                iframe = this.get('iframe').get('element');
            if (state == 'on') {
            	state = 'off';
                this.toolbar.set('disabled', false);
                YAHOO.log('Show the Editor', 'info', 'example');
                YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
                this.setEditorHTML(ta.value);
                if (!this.browser.ie) {
                    this._setDesignMode('on');
                }

                Dom.removeClass(iframe, 'editor-hidden');
                Dom.addClass(ta, 'editor-hidden');
                this.show();
                this._focusWindow();
            } else {
                state = 'on';
                this.cleanHTML();
                Dom.addClass(iframe, 'editor-hidden');
                Dom.removeClass(ta, 'editor-hidden');
                this.toolbar.set('disabled', true);
                this.toolbar.getButtonByValue('editcode').set('disabled', false);
                this.toolbar.selectButton('editcode');
                this.dompath.innerHTML = 'Editing HTML Code';
                this.hide();
            }
            return false;
        }, this, true);

        this.on('cleanHTML', function(ev) {
            YAHOO.log('cleanHTML callback fired..', 'info', 'example');
            this.get('element').value = ev.html;
        }, this, true);
        
        this.on('afterRender', function() {
            var wrapper = this.get('editor_wrapper');
            wrapper.appendChild(this.get('element'));
            this.setStyle('width', '100%');
            this.setStyle('height', '100%');
            this.setStyle('visibility', '');
            this.setStyle('top', '');
            this.setStyle('left', '');
            this.setStyle('position', '');

            this.addClass('editor-hidden');
        }, this, true);
    }, myEditor, true);
    myEditor.render();
	
	
}
