$(function(){
	$('h3 a').remove(); // delete all 'top' links from the DOM
	
	
	//$('#loginform').hide().css({ 'height' : '220px', 'margin-bottom' : '15px' }); // this form is shown by default to support js off .css is to hold positioning
	/*
	$('#estore').show().click(function(){ // this img is display: none; by default to support js off
		$(this).hide();
		$('#loginform').fadeIn(500)
	});
	$('#close').click(function(){
		$('#loginform').hide();
		$('#estore').fadeIn(500);
	});
	*/
	
	
	$('#subnav a, #nav ul li ul a:not([rel]), #nav ul li > a[href=/services.asp#pos]').click(function(e){ // navigate to the correct div.info
		e.preventDefault();
		var sectionID = $(this).attr('class');
		window.location.href = sectionID;
	});
	
	$('a[rel=externallink], form[rel=externallink]').attr('target','_blank'); // open matching links in a new window
	
	hideDivDotInfo(); // hide all div.info divs
	
	$('#loginform').submit(function(){ // delete login and password text after submit
		$('#loginform input[type=text]').val('');
		$('#loginform input[type=password]').val('');
	});
	
	$("a[rel=emailModal]").click(function(e){
		e.preventDefault();
		var aspVar = $(this).attr('href'); // aspVar is the value for the hidden field
		aspVar = aspVar.replace(/[^\d]/g,'');
		var employeeName = $(this).attr('class');
		buildform(aspVar, employeeName);
	});
});

function hideDivDotInfo(){
	$('#main div.info').hide();
}

function updateSubNavAnchorBg(elId){
	$('#subnav a').css({ 'color' : '#111', 'font-weight' : 'normal' });
	$('#subnav a[href=' + elId + ']').css({ 'color' : '#900', 'font-weight' : 'bold' });
}

function buildform(variable, name){
	$('body').append("<div id='mask'></div>");
	
	// set up the mask
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
					
	$('#mask').css({
		'width': maskWidth,
		'height': maskHeight,
		'display': 'none',
		'background': '#000',
		'position': 'absolute',
		'left': '0',
		'top': '0',
		'opacity': '0.8',
		'z-index': '10'
		});

	$('#mask').show();
		
	// set up the modal/form box
	// append form markup to #mask
	$('body').append("<form id='frmContactModal' name='frmContactModal' method='post' action='contact.asp'><h2>Contact: <span id='nme'></span></h2><fieldset><input type='hidden' value='' name='selID' id='selID'><p>Fields marked with <img src='/images/star.jpg' width='11' height='10' alt='required'> are required.</p><p id='alert'></p><div><label for='selName'>Name: <img src='/images/star.jpg' width='11' height='10' alt='required'></label><input type='text' name='selName' id='selName' class='validate valLength'></div><div><label for='selPhone'>Phone:</label><input type='text' name='selPhone' id='selPhone' class='validate phone'></div><div><label for='selEmail'>Email: <img src='/images/star.jpg' width='11' height='10' alt='required'></label><input type='text' name='selEmail' id='selEmail' class='validate email'></div><div><label for='selSubject'>Subject: <img src='/images/star.jpg' width='11' height='10' alt='required'></label><input type='text' name='selSubject' id='selSubject' class='validate valLength'></div><label for='selBody' id='textarealabel'>Comment / Question: <img src='/images/star.jpg' width='11' height='10' alt='required'></label><span>250 characters remaining</span><textarea name='selBody' id='selBody' rows='8' cols='40' class='validate textarea dimtext'>Please enter 15 to 250 characters.</textarea><input type='submit' name='btnSubmit' id='submitBtn' value='Send Email'><input type='button' id='closeBtn' value='Close'></fieldset></form>");
	
	$('#selID').attr('value',variable); // make the hidden input value = the variable from the asp href
	$('#nme').html(name); // insert name in the h2
	
	var wHeight = $(window).height();
	var wWidth = $(window).width();
					
	$('#frmContactModal').css('top', (wHeight/2 - 260) + getScrollY()); // $('#contactform').height()/2
	$('#frmContactModal').css('left', wWidth/2 - 200);										
					
	$('#frmContactModal').fadeIn(500);
	
	$('#mask, #closeBtn').click(function(){
		$('#mask').remove();
		$('#frmContactModal').remove();
	});
	
	// validate
	$('#frmContactModal').submit(function(){
		$('.highlightinput').removeClass('highlightinput'); // remove highlight from all fields
		$('p#alert').html('');
		
		var fieldsToValidate = $('#frmContactModal .validate'); // collection of all fields to validate
		
		for(var i = 0; i < fieldsToValidate.length; i++){
			var el = fieldsToValidate[i];
			if($(el).hasClass('valLength') && ($(el).val().length < 2 || $(el).val().length > 50)){
				displayAlert($(el), 'Please enter more than 1 character and less than 50.');
				return false;
			}
			if($(el).hasClass('phone') && !hasTenDigits($(el).val())){
				displayAlert($(el), 'Please enter a 10 digit phone number.');
				return false;
			}
			if($(el).hasClass('email') && !/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test($(el).val())){
				displayAlert($(el), 'Please enter a valid email address.');
				return false;
			}
			if($(el).hasClass('email') && /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test($(el).val()) && $(el).val().length > 40){
				displayAlert($(el), 'Email addresses must be 40 characters or shorter.');
				return false;
			}
			if($(el).hasClass('textarea') && $(el).val().length < 15){
				displayAlert($(el), 'Please enter at least 15 characters.');
				return false;
			}
		}
	});
	$('#frmContactModal textarea').focus(function(){
		$(this).removeClass('dimtext').val('');
	});

	$('#frmContactModal textarea').keyup(function(){
		var textarea = $(this).val();
		var textareaLength = textarea.length;
		var limit = 250;
		if(textareaLength > limit){
			$('#frmContactModal span[id!=nme]').html('0 characters remaining!');
			$(this).val(textarea.substr(0, limit));
		}
		else{
			$('#frmContactModal span[id!=nme]').html((limit - textareaLength) + ' characters remaining');	
		}
	});

}

function hasTenDigits(phoneNumber){
	if(phoneNumber.length > 0){
		var digits = phoneNumber.replace(/[^\d]/g,'');
		if(digits.length == 10){
			var newNumber = digits.replace(/(\d{3})(\d{3})(\d+)/, '$1-$2-$3');
			$('#selPhone').attr('value',newNumber);
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return true;
	}
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function displayAlert(el, message){
	el.addClass('highlightinput');
	$('p#alert').html(message);
}

