function loginpro(success) {
	if(success) {
		Element.update('loginform','You are now logged in. Please wait.');
		window.location.href = 'index.php';
	} else {
	  Element.update('warning','Invalid login or password!');
	  Effect.Appear('warning');
		 Effect.BlindUp('warning', { delay:6 } );
		 new Effect.Shake('loginform');
		 Element.update('submitb','<input type=\"submit\" class=\"tba\" value=\"login\" />');
	}

}
 

function loginf(thisf) {

	logn = thisf.login.value;
	logp = thisf.pwd.value;

	//alert(logn+" "+logp);
	
	Element.update('submitb','One moment please...');
	
	var opt = {
		// Use POST
		method: 'post',
		postBody: 'login='+logn+'&pwd='+logp,

		onSuccess: function(t) {
			//alert(t.responseText);
			if(t.responseText==1) {
					loginpro(true);
   } else {
	   	loginpro(false);
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.\n\nLogin not possible. Please contact the website administrator.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText+'\n\nLogin not possible. Please contact the website administrator.');
		}
	}
	
	new Ajax.Request('login.php', opt);
	return false;
  
}

function contactMail(thisf) {

	fname = thisf.name.value;
	femail = thisf.email.value;
	fphone = thisf.phone.value;
	fmessage = thisf.message.value;
	
	Element.update('warning','One moment please...');
	Effect.Appear('warning');
	
	var opt = {
	
		method: 'post',
		postBody: 'do=sendmail&name='+fname+'&email='+femail+'&phone='+fphone+'&messagef='+fmessage,
		
		onSuccess: function(t) {
			if(t.responseText==1) {
				Element.update('formdiv','<strong>Dear '+fname+',<br /><br />Thank you for your interest in Vesta Homes.<br /><br />We will be in contact with you regarding your request.</strong>');		
				Effect.Fade('warning',{duration:.1});
			} else if(t.responseText==2) {
				Element.update('warning','There was an error sending your e-mail. You have specified an incorrect e-mail address.');
				Effect.Highlight('warning');
			} else {
			  //alert(t.responseText);
				Element.update('warning','There was an error sending your e-mail. We apologize for the inconvenience.');
				Effect.Highlight('warning');
			}
		},
		
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.\n\nMail sending not possible. Please contact the website administrator.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText+'\n\nMail sending not possible. Please contact the website administrator.');
		}
	
	}
	
	new Ajax.Request('index.php',opt);
	return false;

}


// UPLOADING

   var counter = 0;
    
   function addField(origFieldID,oid,otype)
   {
    counter++;
    var newField = document.getElementById(origFieldID).cloneNode(true);
    //alert(newField.name);
    newField.id = origFieldID + counter;
    //newField.name = 'userfile[]';
    //alert(newField.nodeType);
	   // alert(counter);   
    newField.style.display = '';
    var insertHere = document.getElementById(origFieldID);
    insertHere.parentNode.insertBefore(newField,insertHere);
	  	formload(counter,oid,otype);
   }


		function UploadDone(idupl,imgurl) {
			$('UploadStatus'+idupl).style.color='#006600';
			Element.update('UploadStatus'+idupl, 'Uploading #'+idupl+' finished - photo saved');
			Element.update('UploadPreview'+idupl,'<img src="'+imgurl+'">');
		
		}
		
		function UploadStart(id) {
			//alert("zacinam odosielat");
			$('UploadForm'+id).submit();
			UploadForm(id);
			AddUploadForm();
		} 
		
		function AddUploadForm(oid,otype) {
			addField('UploadFileDiv',oid,otype);
		}
		
		function UploadForm(id) {

			$('UploadStatus'+id).innerHTML='Uploading, please wait...';
			$('UploadStatus'+id).style.color='#CC0033';
			Element.update('UploadPreview'+id,'<br><br>Loading..<br><img src="img/spinner.gif">');
			Effect.Fade('UploadForm'+id,{from:1,to:0.5});
			$('UploadFile'+id).disabled='disabled';
			return true;

		}
		
		function formload(id,oid,otype) {

		var formular = "<div class='UploadPreview' id='UploadPreview"+id+"'></div><div style='float:right; text-align:right; width:450px;'><form class='UploadForm' id='UploadForm"+id+"' name='UploadForm"+id+"' action='uploader.php?do=add_photo&amp;type="+otype+"&amp;oid="+oid+"&amp;idupl="+id+"' enctype='multipart/form-data' target='UploadTarget"+id+"' method='post'><small>Caption:</small> <input type='text' name='caption' size='30' class='tba' /><br /><br /><input type='file' name='UploadFile"+id+"' onchange='UploadStart("+id+");' id='UploadFile"+id+"'><iframe id='UploadTarget"+id+"' name='UploadTarget"+id+"' src='' style='width:0px; height:0px; border:none;'></iframe></form><div class='uploadStatus' id='UploadStatus"+id+"'>Select photo #"+id+"</div> </div> <div style='clear:both;'></div>";
		
		Element.update("UploadFileDiv"+id, formular);
		
		} 
		
		
// DELETING


function delEntry(id,table) {
	
	switch(table) {
	
		case "communities": var wh = "Community"; var el = 'com';	 break;
		case "designs":  	var wh = "Design";    var el = 'des';	 break;
		case "files": 		var wh = "File";	  var el = 'fil';	 break;
		case "infos":  		var wh = "Info";		var el = 'inf'; break;
	
	}

	if(confirm("Are you sure you want to delete this "+wh+"?")) {

		//Element.update("warning","Please wait...");
		//Effect.Appear("warning");
		
		var handlerFunc = function(t) {
				
				alert(''+wh+' deleted succesfully.');
				
				//Element.update("warning",""+wh+" deleted successfully.");
				Element.remove(el+"_"+id);      
		}
		var errFunc = function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
		
		new Ajax.Request('index.php', {parameters:'do=delete&table='+table+'&id='+id, onSuccess:handlerFunc, onFailure:errFunc});
	
	} else {
	
		return false;
	
	}

}		
