// Settings for the vertical rotation.
var newsVisualVertical = 4; //Number of news to be displayed
var intervalloVert = 2500; // time > 2500
var numNewsVert;
var larghezzaDivVert = 210; //width div
var altezzaDivVert = 90; //height div
var margineDivVert = 5; //margin between div

var newsVisualOrizzontal = 4;   //Number of news to be displayed
var intervalloOriz = 2000; // time > 1500
var numNewsOrizzontal;
var larghezzaDivOriz = 150; // width div
var altezzaDivOriz = 118; // height div
var margineDivOriz = 5; // margin between div


$(document).ready(function()
{
  //-------------------------- DOCUMENT FUNCTIONS ------------------------
  // ------------------------- Page layouts ------------------------------
  
  //dropdown menu functions
  
  var about_position = $("#mainNav_about_btn").position();
  var health_postion = $("#mainNav_health_btn").position();
  var investor_position = $("#mainNav_Investor_btn").position();
    
  $("#mainNavSub_about_btn").css({"top":about_position.top+35,"left":about_position.left});
  $("#mainNavSub_health_btn").css({"top":health_postion.top+35,"left":health_postion.left});
  $("#mainNavSub_Investor_btn").css({"top":investor_position.top+35,"left":investor_position.left});
  
  $("#mainNavSub_about_btn").hide();
  $("#mainNavSub_health_btn").hide();
  $("#mainNavSub_Investor_btn").hide();
  
  //hover time function
  $("#hovVal").val(0);
  
  //-------------------------------------------
  $("#mainNav_about_btn").mouseover(function()
  {
    $("#mainNavSub_about_btn").show();
    $("#hovVal").val(1);
    $("#mainNavSub_about_btn").mouseover(function()
    {
      $("#hovVal").val(1);
    });
  });
  
  $("#mainNav_about_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  $("#mainNavSub_about_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  
  //-------------------------------------------
  $("#mainNav_health_btn").mouseover(function()
  {
    $("#mainNavSub_health_btn").show();
    $("#hovVal").val(1);
    $("#mainNavSub_health_btn").mouseover(function()
    {
      $("#hovVal").val(1);
    });
  });
  
  $("#mainNav_health_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  $("#mainNavSub_health_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  
  //-------------------------------------------
  $("#mainNav_Investor_btn").mouseover(function()
  {
    $("#mainNavSub_Investor_btn").show();
    $("#hovVal").val(1);
    $("#mainNavSub_Investor_btn").mouseover(function()
    {
      $("#hovVal").val(1);
    });
  });
  
  $("#mainNav_Investor_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  $("#mainNavSub_Investor_btn").mouseout(function()
  {
    var hovVal = $("#hovVal").val();
    hovVal = hovVal - 1;
    $("#hovVal").val(hovVal);
  });
  
  setTimeout("hideSub()",1);
  //scroller function

    numNewsVert = $("#jqnewsVert").children().length;
    numNewsOrizzontal = $("#jqnewsOriz").children().length;
    if (numNewsVert > 0) {
        jqnewsVertical();
    }
    if (numNewsOrizzontal > 0) {
        jqnewsOrizzontal();
    }
  var windowWidth = $(window).width();


  //--------------------- BUTTON REDIRECTION ON CLICK & BUTTON FUNCTIONS-------------------
  
  $("#mainNav_home_btn").click(function(){
    window.location = "index.php?action=home";
  });
  $("#mainNav_about_btn").click(function(){
    window.location = "index.php?action=about-us";
  });
  $("#mainNav_health_btn").click(function(){
    window.location = "index.php?action=healthcare";
  });
  $("#mainNav_Investor_btn").click(function(){
    window.location = "index.php?action=invest-relate";
  });
  $("#mainNav_news_btn").click(function(){
    window.location = "index.php?action=news";
  });
  $("#mainNav_contact_btn").click(function(){
    window.location = "index.php?action=contact";
  });
  
  $("#about_overview").click(function(){
    window.location = "index.php?action=about-us&function=overview";
  });
  $("#about_strategy").click(function(){
    window.location = "index.php?action=about-us&function=strategy";
  });
  $("#about_directors").click(function(){
    window.location = "index.php?action=about-us&function=directors";
  });
  $("#about_respons").click(function(){
    window.location = "index.php?action=about-us&function=directors-responsibilities";
  });
  $("#about_cgs").click(function(){
    window.location = "index.php?action=about-us&function=cgs";
  });
  
  $("#healthcare_dares").click(function(){
    window.location = "index.php?action=healthcare&function=dar-es-salaam";
  });
  $("#healthcare_maputo").click(function(){
    window.location = "index.php?action=healthcare&function=maputo";
  });
  $("#healthcare_harare").click(function(){
    window.location = "index.php?action=healthcare&function=harare";
  });
  $("#healthcare_aviation").click(function(){
    window.location = "index.php?action=healthcare&function=aviation";
  });
  
  $("#investor_kcd").click(function(){
    window.location = "index.php?action=invest-relate&function=kcd";
  });
  $("#investor_ra").click(function(){
    window.location = "index.php?action=invest-relate&function=report-accounts";
  });
  $("#investor_ss").click(function(){
    window.location = "index.php?action=invest-relate&function=shareholder-services";
  });
  $("#investor_fc").click(function(){
    window.location = "index.php?action=invest-relate&function=financial-calendar";
  });
  $("#investor_sa").click(function(){
    window.location = "index.php?action=invest-relate&function=shareholder-analysis";
  });
  $("#investor_cr").click(function(){
    window.location = "index.php?action=invest-relate&function=company-registration";
  });
  $("#investor_advisors").click(function(){
    window.location = "index.php?action=invest-relate&function=advisers";
  });
  $("#investor_rule26").click(function(){
    window.location = "index.php?action=invest-relate&function=ami-rule-26";
  });
  $("#investor_contact").click(function(){
    window.location = "index.php?action=invest-relate&function=contact";
  });
  
  
  
  
  
  
  
  
  
  
  
  
});





// -------------------------- FUNCTIONS --------------------------

function showGal(dir,img)
{
  if(img < 10){img = "0"+img;}
  var windowWidth = $(window).width();
  var windowHeight = $(window).height();
  
  var html = "<div class='gal' onclick='$(\".gal\").hide();' style='width:"+windowWidth+"px;height:"+windowHeight+"px; position:fixed; top:0px; left:0px; background-image: url(images/gal_bg.png); z-index:9999999;'>";
  html += "<table width='"+windowWidth+"' align='center'>";
  html += "<tr><td height='"+windowHeight+"' valign='midle' align='center'>";
  html += "<img src='"+dir+"images/gal"+img+".jpg' height='"+(windowHeight-100)+"' style='border:5px solid #FFFFFF' onclick='$(\".gal\").hide();'>";
  html += "</tr></td></table>";
  html += "</div>";
  
 $("body").append(html);
}

// ------------------------ NEWS SCROLLER FUNCTIONS -----------------------

function jqnewsVertical() {
    if (newsVisualVertical > numNewsVert) {
        newsVisualVertical = numNewsVert;
    }

    for (var i = newsVisualVertical; i < numNewsVert; i++) {
        $($("#jqnewsVert").children()[i]).css("opacity", "0");
    }

    var gestInter = setInterval(jqNewsRotateVertical, intervalloVert);

    $("#jqnewsVert").mouseover(function() { clearInterval(gestInter) });
    $("#jqnewsVert").mouseout(function() { gestInter = setInterval(jqNewsRotateVertical, intervalloVert); });
}

function jqNewsRotateVertical() {
    $($("#jqnewsVert").children()[0]).animate({ opacity: 0 }, 1000, "linear", function() {
        $($("#jqnewsVert").children()[0]).animate({ marginTop: -altezzaDivVert }, 1000, "linear", function() {
        $($("#jqnewsVert").children()[0]).css("margin", margineDivVert);
        $("#jqnewsVert").append($($("#jqnewsVert").children()[0]));
        $($("#jqnewsVert").children()[newsVisualVertical - 1]).animate({ opacity: 1 }, 1000);
        });
    });
}

function jqnewsOrizzontal() {
    if (newsVisualOrizzontal > numNewsOrizzontal) {
        newsVisualOrizzontal = numNewsOrizzontal;
    }

    for (var i = newsVisualOrizzontal; i < numNewsOrizzontal; i++) {
        $($("#jqnewsOriz").children()[i]).css("opacity", "0");
    }

    var gestInter = setInterval(jqNewsRotateOrizzontal, intervalloOriz);

    $("#jqnewsOriz").mouseover(function() { clearInterval(gestInter) });
    $("#jqnewsOriz").mouseout(function() { gestInter = setInterval(jqNewsRotateOrizzontal, intervalloOriz); });
}

function jqNewsRotateOrizzontal() {    
    $($("#jqnewsOriz").children()[0]).animate({ opacity: 0 }, 1000, "linear", function() {
        $($("#jqnewsOriz").children()[0]).animate({ marginLeft: -larghezzaDivOriz }, 1000, "linear", function() {
            $($("#jqnewsOriz").children()[0]).css("margin", margineDivOriz);
            $("#jqnewsOriz").append($($("#jqnewsOriz").children()[0]));
            $($("#jqnewsOriz").children()[(newsVisualOrizzontal - 1)]).animate({ opacity: 1 }, 1000);
        });
    });
}

function hideSub()
{
  var hov = $("#hovVal").val();
  if(hov <= 0)
  {
    $("#mainNavSub_about_btn").hide();
    $("#mainNavSub_health_btn").hide();
    $("#mainNavSub_Investor_btn").hide();
  }
  setTimeout("hideSub()",1);
}

//--------------------------- VALIDATION FUNCTIONS -----------------------

function validateForm()
{
	if(IfText($("#name")) == false || IfEmail($("#email")) == false || IfPhoneNum($("#number")) == false || IfAddress($("#address")) == false || IfText($("#subject")) == false || IfComments($("#comments")) == false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function IfPhoneNum(num)
{
	if (num.val() == "")
	{
		alert("Field may not be empty");
		num.focus();
		return false;
	}
	else
	{
		
		var notval = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?!@#$%^&*()_-=|\\`~;:'{}[]\" ";
		var val  = 0;
		
		for (var i = 0; i<num.val().length;i++)
		{
			var numChar = num.val().charAt(i);
			for(var k = 0;k<notval.length;k++)
			{
				var notvalchar = notval.charAt(k);
				if (numChar.indexOf(notvalchar) == 0)
				{
					
					val = 1;
				}
			}
		}
		
		if (val == 1)
		{
			alert("Only numbers an + may be used for this Field");
			num.focus()
			return false;
		} else 
		{
			if(num.val().length < 10)
			{
				alert("Contact numbers must be 10 digets or more.");
				num.focus();
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function IfAddress(addr)
{
	if (addr.val() == "")
	{
		alert("Field may not be empty");
		addr.focus();
		return false;
	}
	else
	{
		var notval = "/+<>?!@#$%^&*()_-=|\\`~;:'{}[]\"";
		var val  = 0;
		
		for (var i = 0; i<addr.val().length;i++)
		{
			var addrChar = addr.val().charAt(i);
			for(var k = 0;k<notval.length;k++)
			{
				var notvalchar = notval.charAt(k);
				if (addrChar.indexOf(notvalchar) == 0)
				{
					
					val = 1;
				}
			}
		}
		
		if (val == 1)
		{
			alert("Only A-Z, a-z,0-9 allowed");
			addr.focus();
			return false;
		} else 
		{
				return true;
		}
	}
}

function IfEmail(Email)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(Email.val()) == false) 
	{
		alert('Invalid Email Address');
		Email.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function IfComments(comments)
{
	if(comments.val() == "")
	{
		alert("Field may not be empty");
		comments.focus();
		return false
	}
	else
	{
		return true;
	}
}

function IfText(text)
{
	if (text.val() == "")
	{
		alert("Field may not be empty");
		text.focus();
		return false;
	}
	else
	{
		var notval = "1234567890,./+<>?!@#$%^&*()_-=|\\`~;:'{}[]\"";
		var val  = 0;
		
		for (var i = 0; i<text.val().length;i++)
		{
			var textChar = text.val().charAt(i);
			for(var k = 0;k<notval.length;k++)
			{
				var notvalchar = notval.charAt(k);
				if (textChar.indexOf(notvalchar) == 0)
				{
					val = 1;
				}
			}
		}
		
		if (val == 1)
		{
			alert("Only A-Z, a-z allowed");
			text.focus();
			return false;
		} else 
		{
				return true;
		}
	}
}











































