function DoAjax(url) {
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);	
	
	xhr_object.open("GET", '/_ajax/'+url, false);
	xhr_object.send(null);	

	if(xhr_object.readyState == 4){
		var res = xhr_object.responseText;
		//alert(res);
		return res;
	}
	else
		return "<div class=\"red_box\">This Operation cannot be performed for the moment</div>";
}
function GetId(id) {
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
		return document.all[id];
	else if(document.layers)
		return document.layers[id];
	else
		return null;
}
function GetSel(id){
	return GetId(id).options[GetId(id).selectedIndex].value;
}
function PutAlert(content,type){
	//alert(id);
	GetId('alert_span').className = type;
	GetId('alert_span').innerHTML = content;
}
function CheckInput(id,type){
	err = '';
	if(type == 'sel')
		var_input = GetSel(id);
	else if(type == 'check')
		var_input = GetId(id).checked;
	else{
		var_input = GetId(id).value;
	}
	
	if(type == 'textarea'){
		if(var_input == ""){
			GetId(id).className = 'textArea_error';
			PutAlert('Must be filled!','error');
			return(1);	
		}
		else{
			GetId(id).className = 'textArea_good';
			return(0);	
		}
	}
	else if(var_input == ""){
		GetId(id).className = 'text error';
		PutAlert('Must be filled!','error');
		return(1);	
	}
	else if(type == 'alphanum'){
		var alnum = /^[-!?,.0-9a-zA-Z\s/&]+$/;
		if(alnum.test(var_input) == true){
			GetId(id).className = 'text good';
			return(0);
		}
		else {
			GetId(id).className = 'text error';
			PutAlert('Must be alphanumeric (aA-zZ 0-9) or (?!.,)!','error');
			return(1);
		}
	}
	else if(type == 'email'){
		var alnum = new RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$","g");
  		if(alnum.test(var_input) == true){
			GetId(id).className = 'text good';
			return(0);
		}
		else {
			GetId(id).className = 'text error';
			PutAlert('Invalid email format!','error');
			return(1);
		}
	}
	else if(type == 'numeric'){
		var alnum = /^[0-9&]+$/;
		if(alnum.test(var_input) == true){
			GetId(id).className = 'text good';
			return(0);
		}
		else {
			GetId(id).className = 'text error';
			PutAlert('Must be in numbers!','error');
			return(1);
		}
	}
	else if(type == 'conf_password'){
		var real_pass = GetId('register_password').value;
		if(var_input == real_pass){
			GetId(id).className = 'text good';
			return(0);
		}
		else {
			GetId(id).className = 'text error';
			PutAlert('Password did not match!','error');
			return(1);
		}
	}
	else {
		GetId(id).className = 'text good';
		return(0);
	}
}
function DeleteItem(type,id) {
	if(window.confirm('Are you sure you want to delete this item?')) {		
		var res = DoAjax('delete.php?type='+type+'&id='+id);
		GetId('id_'+id).style.display = 'none';
	}
}
function DeletePhoto(type,id) {
	if(window.confirm('Are you sure you want to delete this item?')) {		
		var res = DoAjax('delete_photo.php?type='+type+'&id='+id);
		if(res == 'OK')
			GetId('id_'+id).style.display = 'none';
		else
			alert('A primary photo can\'t be deleted!');
	}
}
function DeleteOnePic(id) {
	GetId(id+'_preview').src = '/gfx/no_pic/no_pic_75.jpg';
	GetId(id).value = '';
}
