function checkEmail(strng) {
	var error = "";

	var emailFilter=/^.+@.+\..{2,6}$/;

	if (!(emailFilter.test(strng))) {
		return false;
	}

	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\] ']/

	if (strng.match(illegalChars)) {
		return false;
	}

	return true;
}

function formVal(thefields,emailfields) {
	thereturn = true;
	if (thefields!='') {
		fparts = thefields.split(",");
		for (i = 0; i < fparts.length; i++) {
			if (document.getElementById(fparts[i]).value=='') {
				document.getElementById(fparts[i]).style.background='#ffff00';
				thereturn = false;
			}else{
				document.getElementById(fparts[i]).style.background='#ffffff';
			}
		}
	}
	if (emailfields!='') {
		fparts = emailfields.split(",");
		for (i = 0; i < fparts.length; i++) {
			if (!checkEmail(document.getElementById(fparts[i]).value)) {
				document.getElementById(fparts[i]).style.background='#ffff00';
				thereturn = false;
			}else{
				document.getElementById(fparts[i]).style.background='#ffffff';
			}
		}
	}
	
	if (thereturn==false) { alert('You must complete all required fields'); }
	return thereturn;
}


function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 	} 
} 

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

window.onload = externalLinks;

jQuery(document).ready(function($) {

	$('#register, #envelopes').click( function(){
		$('#envelopes').fadeOut( function(){
			$('#sub-signup').fadeIn();
		});
		return false;
	});


    $('#subbox').animate({
        left: '+=425'
    }, 1000, function() {
        $('#popboxarea').fadeIn();
    });

    $('#submainbox').css('display', 'none');

    $('.popbox-img img').click(OpenImageClick);
    //$('.popbox-vid img').click(OpenVideoClick);//
    $('.popbox-vid-overlay img').click(OpenVideoClick);

    $('#popimg-close').click(function() {
        CloseActivePop(function() { $('#submainbox').css('display', 'none'); });
    });

    $('#popvid-close').click(function() {
        CloseActivePop(function() { $('#submainbox').css('display', 'none'); });
    });
});

	function OpenImageClick() {
	    var thisImg = $(this);
	    $('#submainbox').css('display', 'block');
		
		if ($('#popimg-main').attr('src') != thisImg.attr('src').replace("-small.jpg", ".jpg")) {
			CloseActivePop(function() {
				OpenPopImage(thisImg);
			});
		} else if (!$('#popimg').hasClass('popimg-active')) {
			CloseActivePop(function() {
				OpenPopImage(thisImg);
			});
		}
	}

	function OpenVideoClick() {
	    var thisImg = $(this);
	    $('#submainbox').css('display', 'block');
		
		//flv = thisImg.attr('src').replace("-small.jpg", ".flv");//
		flv = thisImg.attr('rel').replace("-small.jpg", ".flv");
		
		if (document.getElementById("videoplayer") && $('#videoplayer').children('param[name="FlashVars"]').attr('value').indexOf(flv)==-1) {
			CloseActivePop(function() {
				OpenPopVideo(thisImg);
			});
		} else if (!$('#popvid').hasClass('popvid-active')) {
			CloseActivePop(function() {
				OpenPopVideo(thisImg);
			});
		}
	}

	function CloseActivePop(callBack) {
		$('.popbox-img img').unbind();
		//$('.popbox-vid img').unbind();//
		$('.popbox-vid-overlay img').unbind();
		
		if ($('#popimg').hasClass('popimg-active')) {
			$('#popimg').animate(
				{ right: '-=410' },
				1000,
				function() {
					$('#popimg').removeClass('popimg-active');

					$('.popbox-img img').click(OpenImageClick);
					//$('.popbox-vid img').click(OpenVideoClick);//
					$('.popbox-vid-overlay img').click(OpenVideoClick);
					
					callBack();
				});
		} else if ($('#popvid').hasClass('popvid-active')) {
		    document.getElementById("videoplayer").SetVariable("player:jsStop", "");
			$('#popvid').animate(
				{ right: '-=543' },
				1000,
				function() {

				    document.getElementById('popvid-main').innerHTML = "";
					$('#popvid').removeClass('popvid-active');

					$('.popbox-img img').click(OpenImageClick);
					//$('.popbox-vid img').click(OpenVideoClick);//
					$('.popbox-vid-overlay img').click(OpenVideoClick);

					callBack();
				});
		} else {
			callBack();
		}
	}

	function OpenPopImage(obj) {
		$('.popbox-img img').unbind();
		//$('.popbox-vid img').unbind();//
		$('.popbox-vid-overlay img').unbind();
		
		$('#popimg').addClass('popimg-active');
		
		$('#popimg-main').attr('src', obj.attr('src').replace("-small.jpg", ".jpg"));
		$('#popimg-main').attr('alt', obj.attr('alt'));
		$('#popimg-main').attr('title', obj.attr('alt'));
		$('#popimg-caption').html(obj.attr('alt'));
		$('#popimg').css('display', 'block');
		$('#popimg').animate(
		{ right: '+=410' },
		1000,
		function() {
			$('.popbox-img img').click(OpenImageClick);
			//$('.popbox-vid img').click(OpenVideoClick);//
			$('.popbox-vid-overlay img').click(OpenVideoClick);
		});
	}

	function OpenPopVideo(obj) {
		$('.popbox-img img').unbind();
		//$('.popbox-vid img').unbind();//
		$('.popbox-vid-overlay img').unbind();

		$('#popvid').addClass('popvid-active');
		
		//flv = obj.attr('src').replace("-small.jpg", ".flv");//
		//img = obj.attr('src').replace("-small.jpg", ".jpg");//
		
		flv = obj.attr('rel').replace("-small.jpg", ".flv");
		img = obj.attr('rel').replace("-small.jpg", ".jpg");
		

		if (document.getElementById('popvid-main').innerHTML=='') {
		    flashObjStr  = "<object class='playerpreview' type='application/x-shockwave-flash' id='videoplayer' data='/flash/player_flv_maxi.swf' width='533' height='300'>"
			flashObjStr += "<param name='movie' value='/flash/player_flv_maxi.swf?d=" + new Date().getTime() + "' />"
			flashObjStr += "<param name='wmode' value='transparent' />"
			flashObjStr += "<param name='FlashVars' id='flashparam' value='flv=" + flv + "&startimage=" + img + "&showiconplay=1&showplayer=never&width=533&height=300&margin=0&playercolor=000000' />"
			flashObjStr += "</object>"
			document.getElementById('popvid-main').innerHTML = flashObjStr;
        } else {
            document.getElementById('flashparam').value="flv=" + flv + "&startimage=" + img + "&showiconplay=1&showplayer=never&width=533&height=300&margin=0&playercolor=000000";
			document.getElementById("videoplayer").SetVariable("player:jsUrl", flv);
			document.getElementById("videoplayer").SetVariable("player:jsStartImage", img);        }
		
		$('#popvid').css('display', 'block');
		$('#popvid').animate({
			right: '+=543'            }, 1000, function() {
            	
			$('.popbox-img img').click(OpenImageClick);
			//$('.popbox-vid img').click(OpenVideoClick);//
			$('.popbox-vid-overlay img').click(OpenVideoClick);
			
		});
	}
	
	function changeMap(newFilename)
	{
		document.getElementById("jobsMap").src="/images/jobs-map/"+newFilename;
	}


	function addCommas(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
