// JavaScript Document
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

$(document).ready(function(){

	//Open the contact popup
	$(".popupContact").click(function(){
		var sh = $(document).height();
		var sw = $(document).width();
		var mlv = (sw-815)/2;
		//alert(mlv);

		// Reset the contact form
		$("#contact-us").css( "display", "block" );
		$("#success_div").css( "display", "none" );
		$("#error_div").css( "display", "none" );

		$("div.overlay").css("width",sw+"px");
		$("div.overlay").css("height",sh+"px");
		$("div#contact").css("left",mlv+"px");

		var mtop = getScrollXY()[1] + 20;
		$("div#contact").css("top",mtop+"px");
		$("div.overlay").css("filter","alpha(opacity=80)");
		$("div.overlay").fadeIn("fast");
		$("div#contact").fadeIn(600);								  

	});

	//Set up Thankyou Popup
	function thankyoupopup(status){
		var sh = $(document).height();
		var sw = $(document).width();
		var mlv = (sw-815)/2;

		$("div.overlay").css("width",sw+"px");
		$("div.overlay").css("height",sh+"px");
		$("div#thankyou-popup").css("left",mlv+"px");

		var mtop = getScrollXY()[1] + 20;
		$("div#thankyou-popup").css("top",mtop+"px");
		$("div.overlay").css("filter","alpha(opacity=80)");
		$("div.overlay").fadeIn("fast");
		$("div#thankyou-popup").fadeIn(600);
		
		if(status == "success"){
			$("div#thankyou-popup-success_div").css( "display", "block" );
			$("div#thankyou-popup-error_div").css( "display", "none" );
		}else{
			$("div#thankyou-popup-success_div").css( "display", "none" );
			$("div#thankyou-popup-error_div").css( "display", "block" );
		}

	}
	
	//Thankyou popup close button click
	$("img.thankyou-popup-closeLb").click(function(){	
		$("div.overlay").fadeOut("slow");	
		$("div#thankyou-popup").fadeOut("fast");
		$("div#thankyou-popup-success_div").fadeOut("fast");
		$("div#thankyou-popup-error_div").fadeOut("fast");
		
		// Clear the input fields
		$("#txtbx_firstname").val( "" );
		$("#txtbx_lastname").val( "" );
		$("#txtbx_companyname").val( "" );
		$("#txtbx_country").val( "" );
		$("#txtbx_phonenumber").val( "" );
		$("#txtbx_emailaddress").val( "" );
	});
	
	//Close any popup when clicking on the overlay
	$("div.overlay").click(function(){

		$("div.overlay").fadeOut("slow");	
		$("div#contact").fadeOut("fast");
		$("div#news-popup").fadeOut("fast");
		$("div#bio-popup").fadeOut("fast");
		$("#success_div").fadeOut("fast");
		$("#error_div").fadeOut("fast");
		$("div#thankyou-popup").fadeOut("fast");
		$("div#thankyou-popup-success_div").fadeOut("fast");
		$("div#thankyou-popup-error_div").fadeOut("fast");
		$("div#news-1").css("display","none");
		$("div#news-2").css("display","none");
		$("div#profile-1").css("display","none");
		$("div#profile-2").css("display","none");
		$("div#profile-3").css("display","none");
		$("div#profile-4").css("display","none");
		$("div#profile-5").css("display","none");
		$("div#profile-6").css("display","none");
		$("div#profile-7").css("display","none");
		$("div#profile-8").css("display","none");
		$("div#profile-9").css("display","none");
		$("div#profile-10").css("display","none");
		$("div#profile-11").css("display","none");
		$("div#profile-12").css("display","none");

		// Clear the input fields
		$("input[type=text]").val("");

		// Remove error shading on all inputs
		$('.required-input').focus(function(){
    	    $(this).removeClass('required')
    	});
	});


	//Process a schedule form submission from the home page
	$("#schedule_form").submit( function(e) {
        // hijack form submission for front end validation and ajax post
		e.preventDefault();
		// instantiate error flag
		error = false;

        // validation, sets error flag
		$('.required-input').each(function(){
            if($(this).val() == ""){
                $(this).addClass("required");
                error = true;
            }
        })

		// If there are no errors, send the message
		if ( error != true ) {
			$.post( "contact/_schedule.php", { firstname: $("#firstname").val(), lastname: $("#lastname").val(), company: $("#companyname").val(), country: $("#country").val(), phone: $("#phonenumber").val(), email: $("#emailaddress").val() }, function( results ) {
				if ( results.success == true ) {
					thankyoupopup('success');
				}
				else {
					thankyoupopup('fail');
				}
				// Clear the input fields
				$("input[type=text]").val("");
			}, "json" );
		}

	});
	
	// Process a contact form submission in contact us page
	$("#contact_form").submit( function(e) {
        // hijack form submission for front end validation and ajax post
		e.preventDefault();
		// instantiate error flag
		error = false;

        // validation, sets error flag
		$('.required-input').each(function(){
            if($(this).val() == ""){
                $(this).addClass("required");
                error = true;
            }
        })

		// If there are no errors, send the message
		if ( error != true ) {
			thankyoupopup("success");
			$.post( "contact/_contact.php", { firstname: $("#firstname").val(), lastname: $("#lastname").val(), company: $("#company").val(), address: $("#address").val(), city: $("#city").val(), state: $("#state").val(), country: $("#country").val(), phone: $("#phone").val(), natureofenquiry: $('#natureofenquiry_input').val(), email: $("#email").val(), application: $('#application_input').val(), projectsize: $("#projectsize").val(), projecttimetable: $('#projecttimetable_input').val(), projectbudget: $('#projectbudget_input').val(), projectdescription: $('#projectdescription').val(), commentorquestion: $('#commentorquestion').val(), zipcode: $('#zipcode').val() }, function( results ) {
				if ( results.success == true ) {
					thankyoupopup('success');
				}
				else {
					thankyoupopup('fail');
				}
				// Clear the input fields
				$("input[type=text]").val("");
			}, "json" );
		}

	});


	//News popup
	$(".popupNews").click(function(){

		var sh = $(document).height();
		var sw = $(document).width();
		var mlv = (sw-815)/2;
		
		$("div.overlay").css("width",sw+"px");
		$("div.overlay").css("height",sh+"px");
		$("div#news-popup").css("left",mlv+"px");
		var mtop = getScrollXY()[1] + 20;
		$("div#news-popup").css("top",mtop+"px");
		
		$("div.overlay").css("filter","alpha(opacity=80)");
		$("div.overlay").fadeIn("fast");
		$("div#news-popup").fadeIn(600);
		if(newsitem != 0){
			$("div#news-"+newsitem).css("display","block");
			$("div#news-"+newsitem).fadeIn(600);
			$("div#news-"+newsitem+" div.news-article-content").scrollTop(0);
		}

	});

	//News popup close button click
	$("img.closeNewsLb").click(function(){	
		$("div.overlay").fadeOut("slow");	
		$("div#news-popup").fadeOut("fast");
		if(newsitem != 0){
			$("div#news-"+newsitem).fadeOut("fast");
			$("div#news-"+newsitem).css("display","none");
		}
	});
	
	//Bio Popup
	$(".popupBio").click(function(){

		var sh = $(document).height()+30;
		var sw = $(document).width();
		var mlv = (sw-815)/2;
		
		$("div.overlay").css("width",sw+"px");
		$("div.overlay").css("height",sh+"px");
		$("div#bio-popup").css("left",mlv+"px");
		var mtop = getScrollXY()[1] + 20;
		$("div#bio-popup").css("top",mtop+"px");
		
		$("div.overlay").css("filter","alpha(opacity=80)");
		$("div.overlay").fadeIn("fast");
		$("div#bio-popup").fadeIn(600);
		if(profileitem != 0){
			$("div#profile-"+profileitem).css("display","block");
			$("div#profile-"+profileitem).fadeIn(600);
			$("div#profile-"+profileitem+" div.profile-description").scrollTop(0);
		}
		

	});

	//Bio popup close button click
	$("img.closeBioLb").click(function(){	
		$("div.overlay").fadeOut("slow");	
		$("div#bio-popup").fadeOut("fast");
		if(profileitem != 0){
			$("div#profile-"+profileitem).fadeOut("fast");
			$("div#profile-"+profileitem).css("display","none");
		}
	});
	
	//Clear any highlighted input field when user tries to enter value into it
	$('.required-input').focus(function(){
	    $(this).removeClass('required')
	});
	
});