var email_reg = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

$('input[type!="submit"]').live("focus", function(){
	$(this).addClass('input_focus');
})

$('input[type!=submit]').live("blur", function(){
	$(this).removeClass('input_focus');
})

$('a').live("click", function(){
 if($(this).attr("href").substr(0,1)=='#')
  return false;
});

$(document).live("click", function(){
 $('#new_verticalmenu').hover(
   function () {
    $('li ul', this).slideDown(100);
    }, 
   function () {
    $('li ul', this).slideUp(100);			
   }
  );

});


function waitId(id, type) {
 if(type==1)
  $('#'+id).html('<center> <img src="graphics/loading.gif" /> </center>');
 else
  $('#'+id).html('<center> <img src="graphics/loading2.gif" /> </center>');  
}

$('.close_notification').live("click",function(){
  closeNotification();
 return false;
});

function closeMessage() {
 $('#show_message').slideUp("slow");     
}
 
$('.close_message').live("click",function(){
 closeMessage();
 return false;    
}); 
    
/* Start of Notification function */    
    
function madKeyEvents() {
 $(document).keydown(function(event){
 var keycode = event.keyCode;
 if(keycode==27){
 closeNotification();
 }
 });
}

function createMessage(message, title, type) {
 madKeyEvents();
 $('#notification_frame').fadeIn(500);
 if(type=="alert")
  $('#notification_message').html('<div id="nofication_title">'+message+'</div>');
 else {
  if(trim(title)!="")
   $('#notification_message').html('<div id="nofication_title">'+title+'</div><div id="notification_content">'+message+"</div>");
  else
   $('#notification_message').html(message);   
 }
 var browser_height=$(window).height();
 var height=$('#notification_message').css('height');
 height=height.substr(0,height.indexOf('px'));
 if(height>browser_height) {
  var new_height=browser_height-100;
  $('#notification_message').css('height', new_height+'px'); 
  $('#notification_message').css('overflow-y','scroll'); 
  height=$('#notification_message').css('height');
  height=height.substr(0,height.indexOf('px'));
 }
 height_last=(browser_height-height)*0.20;
 $('#notification_panel').css('top',height_last+'px');
 $('#notification_actions').html('<a href="#" class="close_notification"> Close </a>');
 $('.close_notification').focus();  
}

function closeNotification() {
 $('#notification_frame').fadeOut(500); 
}

function writeToNotification(msg) {
 $('#notification_message').css("display", "none");
 $('#notification_message').slideDown("slow").html('<div id="nofication_title">'+msg+'</div>');
}

/* End of NOTIFICATION FUNCTIONS */


/* Basic functions */

function trim(str) {
 while (str.substring(0,1) == ' ') {
  str = str.substring(1, str.length);
 }
 while(str.substring(str.length-1, str.length) == ' ') {
  str = str.substring(0,str.length-1);
 }
 return str;
} 

function isValidImage(image){
 var i = image.lastIndexOf(".");
 ext = image.substr(i+1).toLowerCase();
 if(ext=="jpeg" || ext=="jpg" || ext=="png" || ext=="gif")
  return true;  
 return false;
}
 
function getExtension(file){
  var i = file.lastIndexOf(".");
  ext = file.substr(i+1).toLowerCase();
  return ext;
}

/* End of Basic functions */

/* start of ajax form submission */

$('.submit').live("click", function(){
 var id=$(this).attr("formid");
 var extra=$('#'+id).attr("class");
 extra=extra.split(",");
 var loading=extra[0];
 var result=extra[1];
 var report_type=extra[2];
 var must=$(this).attr("must");
 var url=$("#"+id).attr("action");
 var method=$("#"+id).attr("method");
 var str = '';
 var elem = document.getElementById(id).elements;
 if(formValidation(must, report_type, result)) {
  waitId("loading", 1);
  for(var i = 0; i < elem.length; i++) {
   if(elem[i].type=="checkbox" || elem[i].type=="radio") {
     if(elem[i].checked) {
      str += elem[i].name + "=";
      str += elem[i].value + "&";     	
     }	
    }
   else {
    str += elem[i].name + "=";
    str += elem[i].value + "&";
    }  
   }
   waitId(loading, 1); 
   $.post(url, str, function(data){
    $('#'+result).html(data);
    $("#"+loading).fadeOut(1000);
   });
   $('#'+loading).slideUp("slow");
  }

 return false;
});

function formValidation(ids, type, result) {

 if(type=="notification")
  closeNotification();
 var checkers=ids.split(",");
 var error_msg="";
 for(var i=0, count=0;i<checkers.length; i=i+2) {
  $('#message_'+checkers[i]).remove();
  $("#"+checkers[i]).after("<span id='message_"+checkers[i]+"'> </span>");
  var limit_fire=0;
  if(checkers[i+1].substr(0,4)=="text" || checkers[i+1].substr(0,6)=="number") {
   if(checkers[i+1].substr(0,4)=="text")
    var subtract=4; 
   else if(checkers[i+1].substr(0,6)=="number")
    var subtract=6;
   if(checkers[i+1].length>subtract) {
  	var limit=checkers[i+1].substr(subtract,checkers[i+1].length-subtract);
    if($("#"+checkers[i]).val().length<limit)
     limit_fire=limit;
    checkers[i+1]=checkers[i+1].substr(0,subtract);
   }
  } 
  switch(checkers[i+1]) {
   case 'email':
    if($('#'+checkers[i]).val().search(email_reg)==-1) {
      if(count==0) {
       var start=checkers[i];
       count=1;
      }   
	 if(type=="single")    
      $('#message_'+checkers[i]).html("<span class='single_error'> Enter valid email </span>");
     else if(type=="group" || type=="notification")
        error_msg += "<div id='error_group_line'> Enter valid email address </div>";
    }
   	break;
	case 'number':
     if(isNaN($('#'+checkers[i]).val()) || limit_fire!=0 || trim($('#'+checkers[i]).val())=="") {
       if(count==0) {
        var start=checkers[i];
        count=1;
       }
       if(type=="single") {   
        if(trim($('#'+checkers[i]).val())=="")
         $("#message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" can't be empty </span> "); 
		else if(limit_fire==0)
         $("#message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" must have only digits </span>"); 
        else
         $("#message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" must have atleast "+limit_fire+" digits </span>"); 
       }
       else if(type=="group" || type=="notification") {
        if(trim($('#'+checkers[i]).val())=="")
         error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" must have only digits </div>";            
        else if(limit_fire==0)
         error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" must have only digits </div>";       	
        else
         error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" must have atleast "+limit_fire+" digits </div>";       	
       }
      }      
    break;
   	case 'text':  case 'select':
     if(trim($("#"+checkers[i]).val())=="" || limit_fire!=0) {
       if(count==0) {
        var start=checkers[i];
        count=1;
       }
       if(type=="single") {
        if(limit_fire==0)
         $("#message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" can't be empty </span>");
        else
         $("#message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" must have atleast "+limit_fire+" characters </span>");               	
       }
       else if(type=="group" || type=="notification") {
        if(limit_fire==0)
         error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" can't be empty </div>";       	
        else
         error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" must have atleast "+limit_fire+" characters </div>";       	
       }

      }      
    break;
	case 'password':
     if($("#"+checkers[i]).val().length<6) {
       if(count==0) {
        var start=checkers[i];
        count=1;
       }
       if(type=="single")   
        $("#message_"+checkers[i]).html("<span class='single_error'> Password must have atleast 6 characters </span>"); 
       else if(type=="group" || type=="notification")
        error_msg += "<div id='error_group_line'>"+"Password must have atleast 6 characters";
      }      
    break;
   	case 'radio':  case 'checkbox': 
      $("#extra_message_"+checkers[i]).html('');
     if($("#"+checkers[i]+":checked").length==0 || $("#"+checkers[i]+":checked").length=="undefined") {
       if(count==0) {
        var start=checkers[i];
        count=1;
       }
       if(type=="single")   
        $("#extra_message_"+checkers[i]).html("<span class='single_error'> "+$("#"+checkers[i]).attr("title")+" can't be empty </span>");
       else if(type=="group" || type=="notification")
        error_msg += "<div id='error_group_line'>"+$("#"+checkers[i]).attr('title')+" can't be empty </div>";
      }      
    break;
   } 
 }
 if(count==1) {
  if(type=="single") {
   $('html, body').animate({ scrollTop: $("#"+start).offset().top-10 }, 2000);
   $('#'+start).focus();
   }
  else if(type=="group") {
   $('html, body').animate({ scrollTop: $("#"+result).offset().top-10 }, 2000);
   $('#'+result).html(error_msg);
   $('#'+start).focus();
   }
  else if(type=="notification")
   createMessage(error_msg, 'Error !', " ");
  return false;
 }
 else
  return true;
} 
/* End of Validation function */ 

/* vertical menu drop down */


