// JavaScript Document
$(document).ready(function() {	
	
	$("#marketing2009Input").fileUpload({
		'uploader': '/javascript/plugins/uploadify/uploader.swf',
		'script': '/javascript/plugins/uploadify/uploader.php',
		'cancelImg': '/javascript/plugins/uploadify/cancel.png',
		'folder': '/files/marketing2009',
		'multi': true,
		'auto': true,
		'sizeLimit': 52428800,
		'buttonText': 'New Files',
		'onAllComplete': function() {
		  location.href = "index.cfm";
		}
	});
	
	$("#marketing2010Input").fileUpload({
		'uploader': '/javascript/plugins/uploadify/uploader.swf',
		'script': '/javascript/plugins/uploadify/uploader.php',
		'cancelImg': '/javascript/plugins/uploadify/cancel.png',
		'folder': '/files/marketing2010',
		'multi': true,
		'auto': true,
		'sizeLimit': 52428800,
		'buttonText': 'New Files',
		'onAllComplete': function() {
		  location.href = "index.cfm";
		}
	});
	
	$("#salesInput").fileUpload({
		'uploader': '/javascript/plugins/uploadify/uploader.swf',
		'script': '/javascript/plugins/uploadify/uploader.php',
		'cancelImg': '/javascript/plugins/uploadify/cancel.png',
		'folder': '/files/sales',
		'multi': true,
		'auto': true,
		'sizeLimit': 52428800,
		'buttonText': 'New Files',
		'onAllComplete': function() {
		  location.href = "index.cfm";
		}
	});
	
	$.validator.addMethod("passFormat",function(value,element){
			return this.optional(element) || /^.*(?=.{7,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/.test(value);
		},
		"* Passwords must contain at least 1 number, 1 lower case, and 1 upper case letter."
	);		
	
	$('#clinicForm').validate({
		rules: {
			password: "required passFormat",
			email1: "required",
			password2: {
				equalTo: "#password"
			},
			email2: {
				equalTo: "#email1"
			}
		}
	});
	
	$('#indexForm').validate({
		rules: {
			userID: "required",
			password: "required"			
			}
	});
	
	$('#staffForm').validate({
		rules: {
			password: "required passFormat",
			email1: "required",		
			password2: {
				equalTo: "#password"
			},
			email2: {
				equalTo: "#email1"
			},
			catDog: {
				required: function(element) {
       			return  $("input[name='willAns']:checked").val() == "1";
				}			
    		},
			jobLen: {				
				required: function(element) {
       			return  $("input[name='willAns']:checked").val() == "1";
				}			
    		},
			vetSerLen: {				
				required: function(element) {
       			return  $("input[name='willAns']:checked").val() == "1";
				}			
    		} 
		}
				
	});
	//replace code below with better sol
	$("input[name='willAns']:nth(0)").click(function() {
		str = $("input[name='willAns']:checked").val();		
		if(str == "1"){	
			$("#catDog").valid();
			$("#jobLen").valid();
			$("#vetSerLen").valid();
		}
	});
	
	$("input[name='willAns']:nth(1)").click(function() {	
		
		str = $("input[name='willAns']:checked").val();
		if(str == "2"){			
			$('#catDog').removeClass('required');
			$('#jobLen').removeClass('required');
			$('#vetSerLen').removeClass('required');	
			$('#catDog').removeClass('error');
			$('#jobLen').removeClass('error');
			$('#vetSerLen').removeClass('error');
			$("label[for='catDog']").css("display","none");	
			$("label[for='jobLen']").css("display","none");	
			$("label[for='vetSerLen']").css("display","none");				
			//$("#jobLen").rules("remove", "required");
		}
	});
	
/*	jQuery( jQuery("input[name='willAns']") ).change( function()
	{
	  if ( jQuery("input[name='willAns']:checked" ).val() == "1" )
	  {
		jQuery("#jobLen").rules("add", "required");
		
	  }
	  else
	  {
		jQuery("#jobLen").rules("remove", "required");
		
	  }
	});*/	
	
	/*$("#willAns").click(function() {
		$("#jobLen").rules("remove");
	});*/
	
	$("input.zipcode").mask("99999");
	
	$("input.ssn").mask("999-99-9999");

	$('#changePasswordForm').validate({
		rules: {
			tempPass: "required",
			password: "required passFormat",
			tempPass: {
				equalTo: "#tempPass2"
			},
			password2: {
				equalTo: "#password"
			}
		}
	});

	$('#emailPasswordForm').validate();

	// AJAX calls	
	$('#email1').click(function(event){
		var emailAddr = $(this).val();
	
		$('.emailChk').show('slow', checkEmail(emailAddr));
	});	 

	$('a[id^=group]').click(function(event){
		var groupNum = $(this).attr('id').substring(5,6);
		
		$('#views').show('slow', showView(groupNum));
	});	 
		
	$('#srPic').hover(
		function () {$(this).attr("src", "/images/staffRewardsLinkHover.jpg");},
		function () {$(this).attr("src", "/images/staffRewardsLink.jpg");}
	)
});

function checkEmail(emailAddr){
	$.get('AJAXCheck.cfm?emailCheck=' + emailAddr,{},function(data){
		$('.emailChk').html(data);
	});
	return false
}

function showView(groupNum){
	$.get('showViews.cfm?groupNum=' + groupNum,{},function(data){
		$('a[id^=group]').css('font-weight', 'normal'),
		$('#group' + groupNum).css('font-weight', 'bold'),
		$('#views').html(data);
	}),
	$.get('showUsers.cfm?groupNum=' + groupNum,{},function(data){
		$('#users').html(data);
	});
	return false
}

function checkFlashVersion(){
	var version = getFlashVersion().split(',').shift();
	
	if(version < 10){
		/*alert("Your Flash Player is lower than version 10.  You will need to download and install the Flash Player for the Batch Entry to work. ");*/
		$(document).ready(function() {
		$('div.flashDetect').append('<p style="font-size:16px;font-weight:bold;color:##cc0000;">Your Flash Player is not up to date. You will need to install the Flash Player version 10 before proceeding. Please submit an Incident Request to obtain Flash Version 10.</p>');});
	}
	else{
	  /*alert("10 or higher");*/
	}
}

function getFlashVersion(){
	// ie
	try {
		try {
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try { axo.AllowScriptAccess = 'always'; }
			catch(e) { return '6,0,0'; }
		}
		catch(e) {}
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  	}
	// other browsers
	catch(e) {
		try {
			if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
			return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
	  	}
	}
	catch(e) {}
	}
	return '0,0,0';
}

