Element.addMethods({
	setWidth: function (element, width) {
		$(element).setStyle({width: width + 'px'});
		return Element.extend(element);
    },

	setHeight: function (element, height) {
		$(element).setStyle({height: height + 'px'});
		return Element.extend(element);
    },

	setTop: function (element, top) {
		$(element).setStyle({top: top + 'px'});
		return Element.extend(element);
    },

	getTop: function (element, top) {
		return parseInt($(element).getStyle('top'));
    },

	setLeft: function (element, left) {
		$(element).setStyle({left: left + 'px'});
		return Element.extend(element);
    },

	center: function (element) {
		element.setStyle({marginLeft: -(Math.round(element.getWidth()) / 2) + 'px'});
		return Element.extend(element);
    }
});

function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

	var windowWidth, windowHeight;

	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

	objPageSize = {page_width: pageWidth, page_height: pageHeight, window_width: windowWidth, window_height: windowHeight};

	return objPageSize;
}

function getPageScroll() {
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }

	return yScroll;
}

function validate_fields(fields, event) {
	fields.each(function(field) {
		if($F(field).blank()) {
			event.stop();
			$(field).addClassName('error');
		} else if($F(field) == $(field).title) {
			event.stop();
			$(field).addClassName('error');
		} else {
			$(field).removeClassName('error');
			return true;
		}
	});
}

function validate_email(fields, event) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	fields.each(function(field) {
		if($F(field).blank()) {
			event.stop();
			$(field).addClassName('error');
			$('all-required').setStyle({color: '#c60707'});
		} else if(!filter.test($F(field))) {
			event.stop();
			$(field).addClassName('error');
			$('all-required').setStyle({color: '#c60707'});
		} else {
			$(field).removeClassName('error');
			return true;
		}
	});
}

function validate_file(fields, event) {
	fields.each(function(field) {
		var file = $F(field);
		var ext = file.split('.').last();
		
		if($F(field).blank()) {
			event.stop();
			$(field).addClassName('error');
			$('all-required').setStyle({color: '#c60707'});
		} else if(ext != 'jpg' && ext != 'JPG') {
			event.stop();
			$(field).addClassName('error');
			$('alert-ext').setStyle({color: '#c60707'});
			$('all-required').setStyle({color: '#c60707'});
		} else {
			$(field).removeClassName('error');
			$('alert-ext').setStyle({color: '#BFBFBF'});
			return true;
		}
	});
}

document.observe('dom:loaded', function() {

	$$('.toggle').each(function (elm) {
		elm.observe('focus', function (event) {
			if (elm.value == elm.title) {
				elm.addClassName('focus').value = '';
			}
		}).observe('blur', function (event) {
			if (elm.value == '') {
				elm.removeClassName('focus').value = elm.title;
			}
		});
	});

	$$('.form-btn').each(function (elm) {
		elm.observe('mouseover', function (event) {
			elm.addClassName('hover');
		}).observe('mouseout', function (event) {
			elm.removeClassName('hover');
		});
	});

	$$('input.reset').each(function(elm) {
		elm.observe('click', function(event) {
			if(!confirm('Esti sigura ca vrei sa stergi datele din formular?')) {
				event.stop();
			} else {
				$$('input').invoke('removeClassName', 'error');
			}
		});
	});

});