jQuery(document).ready(function(){	
	//navigation
	jQuery("ul.dropdown li").hover(function(){
    
		jQuery(this).addClass("hover");
		jQuery('ul:first',this).css('visibility', 'visible');
    
    }, function(){
    
    	jQuery(this).removeClass("hover");
    	jQuery('ul:first',this).css('visibility', 'hidden');
    
    });
    
	jQuery("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
	
	
	//Contact Form
	jQuery("#contactfrm").submit(function(e){
		
		if(validateContactForm(this)){
			validateContactForm(this);
			return true;
		}
		return false;
	});
	
	
	
	//if mask is clicked
	jQuery('#mask').click(function () {
		jQuery(this).fadeOut();
		jQuery('.MultiBoxContainer').hide();
		jQuery('#iframepdf').removeAttr('src');
	});	
	
	//PDF
	jQuery('.pdf').click(function(e){
		jQuery('.MultiBoxContainer').css('display', 'block');
		
		
		jQuery('#iframepdf').attr('src', jQuery(this).attr('href'));
		
		
		
		//Get the screen height and width
		var maskHeight = jQuery(document).height()-5;
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		jQuery('#mask').fadeTo("slow",0.8);
		
		
		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		var scrollTop = jQuery(window).scrollTop();
		
		var boxTop = '403px';//Move the box to under the banner 
		//Set the popup window to center
		
		jQuery('.MultiBoxContainer').css('left', (winW-840)/2);
		
		
		e.preventDefault();
	});
	
	if(jQuery('.pdf').length) {
			//jQuery('#content').append('<div id="pdfBox">');
			jQuery('#boxes').append('<div class="MultiBoxContainer">');
			jQuery('.MultiBoxContainer').css({						  		
						  				'display':'none'
						  				});
			var boxIframe = jQuery('<iframe>').attr({
												'scrolling': 'no',
												'frameborder': '1',
												'id': 'multiBoxIframe',
												'name':'mulitBoxIframe',
												'src': 'javascript:void(0);',
												'width': '840',
												'height': '590'											
												})
												.css({
												'position': 'absolute',
												'top': '-20px',
												'left': '-20px',
												'width': '840px',
												'height': '590px',
												'visibility': 'hidden',
												'opacity': '1'
												});
												
			//jQuery('.PdfBoxContainer').append('<iframe scrolling="no" frameborder="1" id="multiBoxIframe" name="mulitBoxIframe" src="javascript:void(0);" style="position: absolute; top: -20px; left: -20px; width: 115%; height: 115%; visibility: hidden; opacity: 1;"></iframe>');
			jQuery('.MultiBoxContainer').append(boxIframe);
			
			
			var MultiBoxContentContainer = jQuery('<div>').attr('id','MultiBoxContentContainer')
															.css({
															'visibility':'visible',
															'opacity':'1',
															'width': '800px',
															'height':'550px'															
															});
															
			var MultiBoxContent = jQuery('<div>').addClass('MultiBoxContent');
			
			
			
			jQuery('.MultiBoxContainer').append(MultiBoxContent);
			jQuery('.MultiBoxContent').append(MultiBoxContentContainer);
			
			var iframePdf = jQuery('<iframe>').attr({
													'scrolling': 'auto',
													'height':'550',
													'frameborder': '0',
													'width': '800',													
													'id': 'iframepdf'
												});
			
			jQuery('#MultiBoxContentContainer').append(iframePdf);
			
			var pdfClose = jQuery('<div>').attr('id', 'pdfclose');
			jQuery('.MultiBoxContainer').append(pdfClose);
			
			
			//Click close button
			jQuery('#pdfclose').click(function() {				
				jQuery('#mask').fadeOut();
				jQuery('.MultiBoxContainer').hide();
				jQuery('#iframepdf').removeAttr('src');
			});
			
		}
	
});



//Check Validat
function IsValidEmail(e)
{
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    
    for(i=0; i < e.length ;i++)
    {
        if(ok.indexOf(e.charAt(i))<0)
        {    
            return false;
        }	
    } 
        
    if (!e.match(re) && e.match(re_two))
    {
        return true;		
    }
    else
    {
        return false;
    }    
}
function checkField(id, value, error) {
	if(value == "")	{
		jQuery('#' +id).addClass("error");
		jQuery('#' +id).html(error);		
		return false;
	} else {
		jQuery('#' +id).removeClass("error");
		jQuery('#' +id).html("");
	}	
	return true;
}
function checkEmailField(id, value, error) {
	if(value != "" && !IsValidEmail(value)) {
		jQuery('#' +id).addClass("error");
		jQuery('#' +id).html(error);
		return false;
	} else {
		jQuery('#' +id).removeClass("error");
		jQuery('#' +id).html("");
	}
	return true;
}
function validateContactForm(form){
	
	var isValid = true;
	isValid = isValid && checkField('lbl_name',form.ec_name.value ,"Please enter your name");	
	isValid = isValid && checkField('lbl_ec_email',form.ec_email.value ,"Please enter your email");		
	isValid = isValid && checkEmailField('lbl_ec_email',form.ec_email.value ,"Invalid email address");
	isValid = isValid && checkField('lbl_message',form.ec_message.value ,"Please enter your message");	
	isValid = isValid && checkField('lbl_code',form.verifiedImage.value ,"Please enter the code shown below");	
	return isValid;	
}
