var loadScript = function( basepath, ImgURL )
{

	this.basepath = basepath;
	this.ImgURL = ImgURL;



	loadScript.prototype.scriptaculous = function( which )
	{
		var add = '';
		if ( which ) { add = '?' + which; }
		document.write( '<script type="text/javascript" src="' + this.basepath + 'scriptaculous/scriptaculous.js' + add + '"></script>' );
	};

	loadScript.prototype.custom = function( customs )
	{
		for ( i = 0; i < customs.length; i++ ) {
			document.write( '<script type="text/javascript" src="' +  this.basepath + customs[i] + '.js"></script>' );
		}
	};

	loadScript.prototype.loading = function()
	{
		var div = document.createElement('div');
			div.id = 'custom_loading';
			div.style.display = 'none';
			div.innerHTML = '<img src="'+this.ImgURL+'site_img/admin/loading.gif" alt="" /> Loading...';
		$$('body').appendChild(div);
		//$$('body').first().appendChild(div);
		delete div;
	};
	
	loadScript.prototype.validator = function()
	{
		document.write( '<script type="text/javascript" src="' + this.basepath + 'validator.js"></script>' );
	};


};

/*
http://dev.zeraweb.com/functors-in-javascript
*/

function splitSerialize(position)
{
	var return_position = new Array();

	position = position.split("&");

	for(i = 0, i_return = 0; i < position.length; i++)
	{
		position_id = position[i].split("=");

		if(position_id[1] && position_id[1] != 'undefined')
		{
			return_position[i] = position_id[1];
		}
	}

	return return_position;
}


function sessionTimeDown(min, sec)
{
	if(sec == 0)
	{
		min = min - 1;
		sec = 59;
	}
	else {
		sec = sec - 1;
	}

	if(sec == 0 && min == 0) {
		//alert('Сесията Ви изтече!');
	}
	else {
		$('session_counter').innerHTML = 'Auto log out after: ' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec);
		setTimeout("sessionTimeDown(" + min + "," + sec + ");", 1000);
	}
}

function loopSelected(elem_id, validate)
{
	var selectedArray = '';
	var selObj = document.getElementById(elem_id);
	var i;
	var count = 0;
	var warning =0;
	for (i=0; i<selObj.options.length; i++) {
		
		if (selObj.options[i].selected) {
		
			if(validate==true) {
				
				if(warning == 1) {					
					return -1;
					break;
				}				
				if(selObj.options[i].value == '-1') {
					warning = 1;
				}
				
			} else {
				if(warning == 1) {
					alert('You can\'t select "none" with other choises');
					break;
				}
					
				if(selObj.options[i].value == '-1') {
					warning = 1;
				}

				selectedArray += selObj.options[i].value + ',';
				
			}

		}
	}

	return selectedArray;

}

function verifyNumeric(which)
{
	var oldvalue = [''];

	if (isNaN(which.value))
	{
		if (oldvalue[which])
		{
			which.value = oldvalue[which];
		}
		else
		{
			which.value = '';
			oldvalue[which] = '';
		}
	}
	else
	{
		oldvalue[which] = which.value;
	}
}

function checkEmail(name) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($F(name))) {
		return true;
	}
	return false
}

function validateImageExtension(fld) {
	if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i.test(fld.value)) {
		alert("Invalid image file type.");
		fld.value = '';
		fld.focus();
		return false;
	}
	return true;
}