
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function EnsureNumeric()
{
  var key = window.event.keyCode; 

  if (key <48 || key >57) 
    window.event.returnValue = false; 
}

function showMsg(msg)
{
    $('notice').innerHTML = '<strong>' + msg + '</strong>';
    new Effect.Appear('notice');
    new Effect.Fade('notice', { duration:10 });
}

function IsValidRegisteration()
{
    return isRequiredValid($('username').value, 'usernamemsg') &&
    isValidEmail($('email').value, 'emailmsg') &&
    doFieldsMatch('email','emailconfirm','emailconfirmmsg') &&
    isValidEmail($('emailconfirm').value, 'emailconfirmmsg') &&
    doFieldsMatch('email','emailconfirm','emailconfirmmsg') &&
    isRequiredValid($('password').value, 'passwordmsg') &&
    doFieldsMatch('password','passwordconfirm','passwordconfirmmsg') &&
    isRequiredValid($('passwordconfirm').value, 'passwordconfirmmsg') &&
    doFieldsMatch('password','passwordconfirm','passwordconfirmmsg');
}

function doFieldsMatch(fieldname1, fieldname2, divname)
{
    if($(fieldname1).value.trim().length == 0)
        return false;
    if($(fieldname2).value.trim().length == 0)
        return false;
    
    if($(fieldname1).value == $(fieldname2).value)
        return Valid(divname);
    else   
        return InValid(divname, "* Fields dont match");
}

function isValidEmail(str, divname) {
   if((str.indexOf(".") > 2) && (str.indexOf("@") > 0))
        return Valid(divname);
   else
        return InValid(divname, '* Not valid email format');
}

function isRequiredValid(str, divname)
{
    if(str.trim().length == 0)
        return InValid(divname, '* Field is required');
    else
        return Valid(divname);
}

function Valid(divname)
{
    $(divname).style.display = 'none';
    $(divname).innerHTML  = '';
    return true;
}

function InValid(divname, msg)
{
    $(divname).style.display = 'block';
    $(divname).innerHTML  = msg;
    $(divname).style.color  = 'red';
    return false;
}

function showElement(name){
	document.getElementById(name).style.display='block'
}
function hideElement(name){
	document.getElementById(name).style.display='none'
}
function foodtypechanged(typevalue)
{
	new Ajax.Request('./restaurants/foodoptions', {
	  method: 'GET',
	  crossSite: true,
	  parameters: 'id='+typevalue,
	  onSuccess: function(transport) {
		  var options = eval('(' + transport.responseText + ')');
		  enableAllOptions();
		enableOptions(options[0].attributes);
	  },
	  onFailure: function(transport) {
		//things to do when we encounter a failure
	  }
	});
}

function enableAllOptions()
{
	document.getElementById('shisha').disabled = false;
	document.getElementById('halal').disabled = false;
	document.getElementById('dancing').disabled = false;
	document.getElementById('alcohol').disabled = false;
	document.getElementById('grocery').disabled = false;
}
function enableOptions(options)
{
	if(options.shisha == 0)
		document.getElementById('shisha').disabled = true;
	if(options.halal == 0)
		document.getElementById('halal').disabled = true;
	if(options.dancing == 0)
	document.getElementById('dancing').disabled = true;
	if(options.alcohol == 0)
		document.getElementById('alcohol').disabled = true;
	if(options.grocery == 0)
		document.getElementById('grocery').disabled = true;
}

function showForm(divname)
{
   document.getElementById('stof').style.display = 'none';
   document.getElementById('stop').style.display = 'none';
   document.getElementById('stoc').style.display = 'none';
   document.getElementById(divname).style.display = 'block';
   return false;
}
	
window.onload=function(){
Nifty("div#notice","big");Custom.init();
}