function createCookie(name,value,days,domain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	
	if (domain) {
		path = "; path="+domain;
	} else {
		path = '';
	}
	document.cookie = name+"="+value+expires+path;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

window.addEvent('domready', function(){
	
	if ($('search_sword'))
	{
		var sword = $('search_sword').value;
		
		$('search_sword').addEvent('focus',function(){
			if ($('search_sword').value == sword)
			{
				$('search_sword').value = "";
			}
		});
		
		$('search_sword').addEvent('blur',function(){
			if ($('search_sword').value == "")
			{
				$('search_sword').value = sword;
			}
		});
	}

	$$('div.main_menu li').each(function(item) {
		if (item.getElements('ul').length>0)
		{
			item.addClass('sub');
			item.removeClass('reg');
		}	
	});

	$$('div.main_menu ul li ul').each(function(item) {
		var width = parseInt(item.getParent().offsetWidth);
		//if (width<100)
		//	width=100;
		item.setStyle('width', (width)+'px');
		item.getParent().getElement('a').setStyle('width', (width)+'px');
		item.getParent().getElement('a span.middle1').setStyle('padding-right', '0px');
	});

	$$('div.main_menu ul li ul li a span.middle1').each(function(item) {
		item.getParent().getElement('span.left1').setStyle('height', parseInt(item.offsetHeight)+'px');
		item.getParent().getElement('span.right1').setStyle('height', parseInt(item.offsetHeight)+'px');
	});	

	$$('input').each(function(item) {
		if (item.id!="search_sword" &&
		    (item.type=="text" ||
			 item.type=="password"))
		 {
		 		//clone the input item
			x = item.clone().addClass('special_input');
			
		 	if (item.id) {
				value = readCookie(item.id);
				if(value != null){
					x.value = value;	
				}
		 	}
					 	
				//set width in pixels based on the size only if explicit width is not already set
			if (typeof item.size != "undefined" && item.size>0)
				x.setStyle('width', (6*item.size)+'px');
			
				//generate three wrapper div elements
         	t = divWrap(divWrap(divWrap(x, "special_input_outer_2"), "special_input_outer_1"), "special_input_outer_0");  
			
				//switch original input box with the new one with wrapper div elements
			t.injectAfter(item);
			item.remove();
			
		 }
	});


	$$('textarea').each(function(item) {
			//clone the input item
		x = item.clone().addClass('special_textarea');
		
		$value = readCookie(item.id);
		if($value != null){
			x.value = $value;	
		}
		
			//generate three wrapper div elements
        t = divWrap(divWrap(divWrap(divWrap(divWrap(divWrap(divWrap(x, "special_textarea_inner"), "special_textarea_left_bottom"),  "special_textarea_left_top"),  "special_textarea_left_middle"),  "special_textarea_right_bottom"),  "special_textarea_right_top"),  "special_textarea_right_middle");  
		
		
			//switch original input box with the new one with wrapper div elements
		t.injectAfter(item);
		item.remove();		
		
       	    //insert left border
		y = new Element('div', {'class':'special_textarea_left_middle_border'});
		y.injectAfter(t.getElement('div.special_textarea_left_middle'));
            //manage the size of the left border!
		yh = parseInt(t.offsetHeight)-2*parseInt(y.getStyle('top'));
		yw = parseInt(t.offsetWidth)-2*parseInt(y.getStyle('left'));
		y.setStyle('height', yh+'px');
		y.setStyle('width', yw+'px');
		    
       	    //insert right border
		y = new Element('div', {'class':'special_textarea_right_middle_border'});
		y.injectAfter(t.getElement('div.special_textarea_left_middle'));
            //manage the size of the left border!
		yh = parseInt(t.offsetHeight)-2*parseInt(y.getStyle('top'));
		yw = parseInt(t.offsetWidth)-2*parseInt(y.getStyle('left'));
		y.setStyle('height', yh+'px');
		y.setStyle('width', yw+'px');


       	    //insert top border
		y = new Element('div', {'class':'special_textarea_top_middle'});
		y.injectAfter(t.getElement('div.special_textarea_left_middle'));
            //manage the size of the left border!
		yh = parseInt(t.offsetHeight)-2*parseInt(y.getStyle('top'));
		yw = parseInt(t.offsetWidth)-2*parseInt(y.getStyle('left'));
		y.setStyle('height', yh+'px');
		y.setStyle('width', yw+'px');
		    
       	    //insert bottom border
		y = new Element('div', {'class':'special_textarea_bottom_middle'});
		y.injectAfter(t.getElement('div.special_textarea_left_middle'));
            //manage the size of the left border!
		yh = parseInt(t.offsetHeight)-2*parseInt(y.getStyle('top'));
		yw = parseInt(t.offsetWidth)-2*parseInt(y.getStyle('left'));
		y.setStyle('height', yh+'px');
		y.setStyle('width', yw+'px');

	});	
	
	$(document.body).getElements('input[type=submit]').addEvent('click',function(event){
		
		$$('input').each(function(item) {
			if (item.id)
				createCookie(item.id,item.value,0,null);
		});
		
		$$('textarea').each(function(item) {
			if (item.id)
				createCookie(item.id,item.value,0,null);
		});
	});
});

function divWrap(el, divClass){  
       x = new Element('div', {'class':divClass});
	   el.inject(x);
	   return x; 
}