﻿var _Validators = new Array();


$(document).ready(function () {
	InitializeValidators();
	InitializeHoverActions();
	//DOM Updates after AJAX calls may orphan the inputs associated with Validators.
	// We need to reInitialize our validators after each request is completed.
	//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitializeValidators);
});

//Takes any image with the Hover action class applied to it and adds a hover action
//to replace the image with the appriopriately name counterpart (originalname + '_hover')
function InitializeHoverActions() {
	$('.HoverAction').each(function(i, e) {
		var jq = $(this);
		jq.mouseover(function() {
			var src = jq.attr('src');
			var imageType = src.substring(src.lastIndexOf('.'), src.length);
			src = src.replace(imageType, '');
			jq.attr('src', src + '_hover' + imageType);
		});
		jq.mouseout(function() {
			var src = jq.attr('src');
			jq.attr('src', src.replace('_hover', ''));
		});
	});
}

// This function creates custom Validator objects from all DOM elements 
// marked with a "Validate[type]" Css class name.
function InitializeValidators() {
	// Lets start by releasing all the existing validators. This will only happen on
	// return from an AJAX call.  On the initial page load, _Validators will be empty.
	$.each(_Validators, function(item) { item = null });
	_Validators = new Array();

	//Ok, lets start making Validators!
	$('.ValidateAreaCodeOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,3}-?[0-9]{0,3}?$')); });
	$('.ValidateSicOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,6}-?[0-9]{0,6}?$')); });
	$('.ValidateZipOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,5}[-?[0-9]{0,5}]?$')); });
	$('.ValidateRoute').each(function(input, element) { _Validators.push(new Validator(element, '^[cCrRbBhH]{1}[0-9]{0,3}?$')); });
	$('.ValidateText').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9a-zA-Z _-]*$')); });
	$('.ValidateNumber').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]*$')); });
	$('.ValidateTwoDecimalNumber').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]*(\\.)?[0-9]+$')); });
	$('.ValidateZip4').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{0,4}?$')); });
	$('.ValidateDecimal').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]*[.]{0,1}[0-9]*$')); });
	$('.ValidatePhone').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9.()-]*$')); });
	$('.ValidateEmail').each(function (input, element) { _Validators.push(new Validator(element, '^[0-9a-zA-Z@._-]*$', { blurExpression: '^([\\w\\-\\.]+)@((\\[\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|\\d{1,3})$' })); });
	$('.ValidateMultilineEmail').each(function (input, element) { _Validators.push(new Validator(element, '^[\\s0-9a-zA-Z@,;._-]*$', { blurExpression: '^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*([,;]\\s*\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)*', helperMessage: $(this).attr('id') + 'ErrorMessage' })); });
	$('.ValidateExtendedText').each(function(input, element) { _Validators.push(new Validator(element, '^[\\s0-9a-zA-Z .,;\[\\]:\'@#$%()&+?*_-]*$')); });
	$('.ValidateRestrictedText').each(function (input, element) { _Validators.push(new Validator(element, '^[A-Za-z \'-]+$')); });
}

// CLASSES
function Validator(input, expression, options) {
	this.keyExpression = expression;
	this.blurExpression = expression;
	this.oldValue = input.value;
	this.errorColor = '#f33';
	this.inputId = input.id;

	// override default options if provided.
	if (options) {
		if (options.errorColor)
			this.errorColor = options.errorColor;

		if (options.blurExpression)
			this.blurExpression = options.blurExpression;

		if (options.helperMessage)
			this.helperMessage = options.helperMessage;
	}

	input.onblur = function() { return validate(this, 'blur') };
	input.onkeyup = function() { return validate(this, 'keyup') };
}

//validates the input for the control against the correct regular expression
function validate(obj, event) {

	var validator;
	//look up the validator
	for (var i = 0; i < _Validators.length; i++) {
		if (_Validators[i].inputId == obj.id) {
			validator = _Validators[i];
			break;
		}
	}

	//abort if could not look up validator
	if (validator == null)
		return;

	//grab the expression
	var expression = event == 'blur' ? validator.blurExpression : validator.keyExpression;
	var myRegX = new RegExp(expression);
	if (validator.helperMessage)
		var helperMessageJq = $('#' + validator.helperMessage);
	//test it, return if true
	if (obj.value.length == 0 || myRegX.test(obj.value)) {
		validator.oldValue = obj.value;
		if (helperMessageJq)
			helperMessageJq.hide('normal');
		return true;
	}

	//invalid input, highlight the error and restor previous value
	$(obj).effect("highlight", { color: '#F33000' }, 1000);
	obj.value = validator.oldValue;
	if (helperMessageJq)
		helperMessageJq.show('normal');

	return false;
}

function EnablePopup(popup, callback) {
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = popup.height();
	var popupWidth = popup.width();

	$('#backgroundPopup').css({ "opacity": "0.7" }).fadeIn('slow');

	popup.css({
		"position": "absolute",
		"top": windowHeight / 2 - popupHeight / 2,
		"left": windowWidth / 2 - popupWidth / 2
	}).fadeIn("slow");

	$(".backgroundPopup").css({
		"height": windowHeight
	});

	if (callback) {
		callback();
	}
}

function DisablePopup(popup, callback) {
	$('#backgroundPopup').fadeOut('slow');
	popup.fadeOut('slow');

	if (callback) {
		callback();
	}
}

function viddlerGetMovie(movieName) {
	return GetMovie(movieName);
}

function GetMovie(movieName) { 
	var fullMovieName = movieName;
	if (navigator.appName.indexOf("Microsoft") != -1) {
		// is IE or another Microsoft product that does not yet exist 
		if (navigator.appVersion.indexOf("MSIE 8.0") != -1) {
			//ie 8 
			var mv = document[fullMovieName];
		}
		else if (navigator.appVersion.indexOf("MSIE 7.0") != -1) {
			//ie 7 
			var mv = document[fullMovieName];

		}
		else {
			//ie 6 
			var mv = window[fullMovieName];
		}
	}
	else {
		//another browser like Firefox, Chrome, or Safari 
		var mv = document[fullMovieName];
	}

	return mv;
}

function StopMovie(movieName) {
	var mv = viddlerGetMovie(movieName);
	mv.stopMovie();
}
