// Alam group of Companies - JavaScript Document

//Function to get empty fileds
function isEmpty(itemid, message, location)
{
	var selected_item = $("#"+itemid).val();
	
	if(selected_item == "" || selected_item == null)
	{
		$("#"+location).html("<font color = 'red'>"+message+"</font>");
		
		$("#"+itemid).focus();
		
		return false;
	}
	else
	{
		$("#"+location).html("");
		return true;	
	}
}

//Check email address
function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

	return pattern.test(emailAddress);
}

//Function to send message
function sendMessage(param1, param2, param3, param4, location)
{
	var company = $("#"+param1).val();
	var prospective_customer = $("#"+param2).val();
	var email = $("#"+param3).val();
	var message = $("#"+param4).val();
	var url = "../alam_incls/process_message.php";
	
	if(isValidEmailAddress(email))
	{
	 	$("#"+location).html("<font color = '#CD9400'>Processing request &nbsp;. &nbsp;. &nbsp;. &nbsp;</font><img src=\"../images/loader.gif\" width=\"16\" height=\"11\" border=\"0\" align=\"middle\" />");
		
		$.post(url,
		{ companyname: company, customer: prospective_customer, customeremail: email, customermessage: message },
			function(data)
			{
				if(data == 1)
				{
					$("#"+location).html("<font color = 'green'>Message sent &nbsp; &nbsp;</font><img src=\"../images/sent.gif\" width=\"21\" height=\"26\" border=\"0\" align=\"middle\" />");
				}
				else
				{
					$("#"+location).html("<font color = 'red'>Message not sent &nbsp; &nbsp;</font><img src=\"../images/not_sent.gif\" width=\"21\" height=\"25\" border=\"0\" align=\"middle\" />");
				}
			}
		)

	} 
	else 
	{
	 	$("#"+location).html("<font color = 'red'>Please enter a valid email address.</font>");
		
		$("#"+param3).focus();
		
		return false;
	}
	
	return false;
}

//Function to send inquiry
function sendInquiry(param1, param2, param3, param4, param5, param6, location)
{
	var company = $("#"+param1).val();
	var prospective_customer = $("#"+param2).val();
	var email = $("#"+param3).val();
	var contact_address = $("#"+param4).val();
	var products_required = $("#"+param5).val();
	var time_period = $("#"+param6).val();
	var url = "../alam_incls/process_inquiry.php";
	
	if(isValidEmailAddress(email))
	{
	 	$("#"+location).html("<font color = '#CD9400'>Processing request &nbsp;. &nbsp;. &nbsp;. &nbsp;</font><img src=\"../images/loader.gif\" width=\"16\" height=\"11\" border=\"0\" align=\"middle\" />");
		
		$.post(url,
		{ companyid: company, customer: prospective_customer, customeremail: email, customer_contact_address: contact_address, customer_products_required: products_required, customer_time_period: time_period },
			function(data)
			{
				if(data == 1)
				{
					$("#"+location).html("<font color = 'green'>Purchase inquiry sent &nbsp; &nbsp;</font><img src=\"../images/sent.gif\" width=\"21\" height=\"26\" border=\"0\" align=\"middle\" />");
				}
				else
				{
					$("#"+location).html("<font color = 'red'>Purchase inquiry not sent &nbsp; &nbsp;</font><img src=\"../images/not_sent.gif\" width=\"21\" height=\"25\" border=\"0\" align=\"middle\" />");
				}
			}
		)

	} 
	else 
	{
	 	$("#"+location).html("<font color = 'red'>Please enter a valid email address.</font>");
		
		$("#"+param3).focus();
		
		return false;
	}
	
	return false;

}



/* SLIDESHOW */
function slideSwitch() {
    var $active = $('#slideshowmain DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshowmain DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshowmain DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});