
/* Enabling support for new HTML5 tags for IE6, IE7 and IE8 */
if(navigator.appName == 'Microsoft Internet Explorer' ){
	if( ( navigator.userAgent.indexOf('MSIE 6.0') >= 0 ) || ( navigator.userAgent.indexOf('MSIE 7.0') >= 0 ) || ( navigator.userAgent.indexOf('MSIE 8.0') >= 0 ) ){
		document.createElement('header')
		document.createElement('nav')
		document.createElement('section')
		document.createElement('aside')
		document.createElement('article')
		document.createElement('footer')
		
		document.createElement('hgroup')
		document.createElement('figure')
		document.createElement('figcaption')
	}
}

/* GLOBAL VARIABLES */

var	$thepopUpBox 				= ''
var $overlay					= ''
var animationSpeed				= 500
var doc							= null;
var userId						= getCookie('IGReport');

/* GLOBAL VARIABLES */

;(function($){
	$(function(){

		/************* CODE FOR INPUT EFFECTS  ********/
		$('input:text').focus(function(){
			if(this.value==this.defaultValue){this.value=''}
		})
		$('input:text').blur(function(){
			if(!this.value){this.value=this.defaultValue;}
		})
		$('input:password').focus(function(){
			if(this.value==this.defaultValue){this.value=''}
		})
		$('input:password').blur(function(){
			if(!this.value){this.value=this.defaultValue;}
		})
		$('textarea').focus(function(){
			if(this.value==this.defaultValue){this.value=''}
		})
		$('textarea').blur(function(){
			if(!this.value){this.value=this.defaultValue;}
		})
		/************* END CODE FOR INPUT EFFECTS  ********/
		
		/* CONDITIONAL CODE FOR IE */
		$('head').append('<!--[if IE 6]><script type="text/javascript" src="scripts/pngfix.js"></script><![endif]-->');
		
		if (window.PIE) {
			$('.gradient, .rounded, .shadow').each(function() {
				PIE.attach(this);
			});
		}
		/* END CONDITIONAL CODE FOR IE */
		
		/* BEGIN CODE FOR FOCUS ON SEARCH BOX */
		$('div.search-box input[type=text]').focus(function(){
			$(this).parents('div.search-box').addClass('focus')
		})
		$('div.search-box input[type=text]').blur(function(){
			$(this).parents('div.search-box').removeClass('focus')
		})
		/* END CODE FOR FOCUS ON SEARCH BOX */
		
		/* PopUp box show hide Begin */
		jQuery('body').append('<div id="overlay_layer"></div>')// Creat the overlay_layer div
		$overlay = $('#overlay_layer')
		$overlay.animate({opacity:'hide'}, 0)// Initialy hiding the mask
		
		$('#popup-box').css({'left' : 0}).animate({opacity:'hide'}, 0)// Initialy hiding the popup box	
		
		$('a.download-btn').click(function(e){
			e.preventDefault()
			doc = this;
			if(userId){
				doDownload();
			} else {
				showAndHidepopupBox('#popup-box')
			}
		})// END DOWNLOAD BTN CLICK
		
		$('#download-submit').click(function(e){
			e.preventDefault();
			
			var err = false;
			var message = 'The following errors were found in the form:';
			if(isEmpty($('#name').val())){
				err = true;
				message += '\nPlease supply a name. ';
			}
			
			if(isEmpty($('#company').val())){
				err = true;
				message += '\nPlease supply the name of the company with which you are affiliated. ';
			}
			
			if(isEmpty($('#position').val())){
				err = true;
				message += '\nPlease supply the position you hold in your company. ';
			}
			
			if(isEmpty($('#email').val())){
				err = true;
				message += '\nPlease supply a valid e-mail address. ';
			} else {
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;;
				if(reg.test($('#email').val()) == false) {
					err = true;
					message += '\nThe e-mail address is invalid.';
				}
			}
			
			if(isEmpty($('#phone').val())){
				err = true;
				message += '\nPlease supply a telephone number';
			}
			
			if(err){
				alert(message);
				return;
			}
			
			$.ajax({
				type: "POST",
				dataType: "json",
				url: '/reports/index.php?act=register',
				data: ({name: $('#name').val(), company: $('#company').val(), position: $('#position').val(), email: $('#email').val(), phone: $('#phone').val()}),
				success: function(data){
					setCookie('IGReport',data['userId'],365);
					userId = data['userId'];
					doDownload();
					$overlay.animate( {opacity:'hide'}, animationSpeed );
					$thepopUpBox.animate( {opacity:'hide'}, animationSpeed );
				}
			});
		})
		
	})// END OF DOCUMENT READY FUNCTION
	

})(jQuery)

function isEmpty(str){
  str = str.replace(/^\s+/g, '').replace(/\s+$/g, '');
  if(str.length == 0){
      return true;
  } else {
      return false;
  }
}

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function doDownload(){
	$.ajax({
		type: "POST",
		dataType: "json",
		url: '/reports/index.php?act=download',
		data: ({userId: userId, document: doc.toString()}),
		success: function(data){
			if(data['download']){
				location.href = doc;
			}
		}
	})
}


// Begining function showpopupBox
function showAndHidepopupBox(popupBoxId){
	/* Begin  Pointers and Varriables */
	$thepopUpBox			= jQuery(popupBoxId)
	var $body		 		= jQuery('body')
	var	$window		 		= jQuery(window)
	var $page		 		= jQuery('#page')
	
	var thepopUpboxHeight	= $thepopUpBox.outerHeight()
	var thepopUpboxWidth	= $thepopUpBox.outerWidth()
	var bodyHeight			= $body.height()
	var bodyWidth			= $body.width()
	var windowHeight		= $window.height()
	var windowWidth			= $window.width()
	var pageouterWidth		= $page.outerWidth()
	var pageouterHeight		= $page.outerHeight()
	var overlayHeight		= $overlay.height()
	var overlayWidth		= $overlay.width()
	var verticalPosition	= 0 
	var horizontalPosition	= 0
	/* End Pointers and Varriables */
		
	if(pageouterHeight>windowHeight){
		overlayHeight=pageouterHeight+45
	}
	else{
		overlayHeight=windowHeight
	}
	if(bodyWidth>pageouterWidth){
		overlayWidth=bodyWidth
	}
	else{
		overlayWidth=pageouterWidth
	}
	if(windowHeight>thepopUpboxHeight){
		verticalPosition = $window.scrollTop() + Math.round( (windowHeight-thepopUpboxHeight)/2 )
	}
	else{
		verticalPosition = $window.scrollTop()
	}
	if(windowWidth>thepopUpboxWidth){
		horizontalPosition = $window.scrollLeft() + Math.round( (windowWidth-thepopUpboxWidth)/2 )
	}
	else{
		horizontalPosition = $window.scrollLeft()
	}

	$overlay.css({ 
		'height'	: overlayHeight,
		'width'		: overlayWidth,
		'position'	: 'absolute',
		'left'		: 0,
		'top'		: 0,
		'z-index'	: 95,
		'opacity'	: 0.8,
		'background': '#000'
	})//Set the properties of the overlay div
	
	$overlay.animate( {opacity:'show'}, animationSpeed )//show the mask
	
	$thepopUpBox
		.css({ 
			 'display'	: 'block',
			 'top'		: verticalPosition,
			 'left'		: horizontalPosition
		})
		.animate( {opacity:'show'}, animationSpeed )
		// popup box position at the center of the window (verticaly and horizontaly)
		
	$overlay.click(function(){
		jQuery(this).animate( {opacity:'hide'}, animationSpeed )
		$thepopUpBox.animate( {opacity:'hide'}, animationSpeed )
	})
} // Ending function showpopupBox

