﻿jQuery.jqURL = {

	url: // returns a string
	function(args) {
		args =
			jQuery.extend({
				win: window
			},
			args);
		return args.win.location.href;
	},

	loc:
	function(urlstr, args) {
		args =
			jQuery.extend({
				win: window,
				w: 500,
				h: 500,
				wintype: '_top'
			},
			args);

		if (!args.t) {
			args.t = screen.height / 2 - args.h / 2;
		}
		if (!args.l) {
			args.l = screen.width / 2 - args.w / 2;
		}
		if (args['wintype'] == '_top') {
			args.win.location.href = urlstr;
		}
		else {
			open(
			urlstr,
			args['wintype'],
			'width=' + args.w + ',height=' + args.h + ',top=' + args.t + ',left=' + args.l + ',scrollbars,resizable'
			);

		}
		return;
	},

	qs:
	function(args) {
		args = jQuery.extend({
			ret: 'string',
			win: window
		},
		args);

		if (args['ret'] == 'string') {
			return jQuery.jqURL.url({ win: args.win }).split('?')[1];
		}

		else if (args['ret'] == 'object') {

			var qsobj = {};
			var thisqs = jQuery.jqURL.url({ win: args.win }).split('?')[1];

			if (thisqs) {
				var pairs = thisqs.split('&');
				for (i = 0; i < pairs.length; i++) {
					var pair = pairs[i].split('=');
					qsobj[pair[0]] = pair[1];
				}
			}
			return qsobj;
		}
	},

	strip:
	function(args) {
		args = jQuery.extend({
			keys: '',
			win: window
		},
			args);

		if (jQuery.jqURL.url().indexOf('?') == -1) { // no query string found
			return jQuery.jqURL.url({ win: args.win });
		}
		// if no keys passed in, just return url with no querystring
		else if (!args.keys) {
			return jQuery.jqURL.url({ win: args.win }).split('?')[0];
		}
		else { //return stripped url

			var qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });  // object with key/value pairs		
			var counter = 0;
			var url = jQuery.jqURL.url({ win: args.win }).split('?')[0] + '?';
			var amp = '';

			for (var key in qsobj) {
				if (args.keys.indexOf(key) == -1) {
					// pass test, add this key/value to string
					amp = (counter) ? '&' : '';
					url = url + amp + key + '=' + qsobj[key];
					counter++;
				}
			}
			return url;
		}
	},

	get:
	function(key, args) {
		args = jQuery.extend({
			win: window
		}, args);

		qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });
		return qsobj[key];
	},

	set:
	function(hash, args) {
		args = jQuery.extend({
			win: window
		}, args);

		// get current querystring
		var qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });

		// add/set values from hash
		for (var i in hash) {
			qsobj[i] = hash[i];
		}

		var qstring = '';
		var counter = 0;
		var amp = '';

		// turn qsobj into string
		for (var k in qsobj) {
			amp = (counter) ? '&' : '';
			qstring = qstring + amp + k + '=' + qsobj[k];
			counter++;
		}
		return jQuery.jqURL.strip({ win: args.win }) + '?' + qstring;
	}

};

function attachTextAreaEvents(textAreaSelector, nMaxLen) {

	var textArea = $(textAreaSelector);

	var handler = function(args) {
		var charLeft = countCharsLeft(textArea[0], nMaxLen, charsLeft[0]);
		if (charLeft == 0)
			return false;
	}
	textArea.keypress(handler);
	textArea.keyup(handler);
	textArea.click(handler);

}

function countCharsLeft(obj, nMaxLen, labelObj) {
	var charsLeft = nMaxLen - obj.value.length;
	if (charsLeft < 0)
		charsLeft = 0;

	return charsLeft;
}

function onSilverlightError(sender, args) {
	var appSource = "";
	if (sender != null && sender != 0) {
		appSource = sender.getHost().Source;
	}

	var errorType = args.ErrorType;
	var iErrorCode = args.ErrorCode;

	if (errorType == "ImageError" || errorType == "MediaError") {
		return;
	}

	var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

	errMsg += "Code: " + iErrorCode + "    \n";
	errMsg += "Category: " + errorType + "       \n";
	errMsg += "Message: " + args.ErrorMessage + "     \n";

	if (errorType == "ParserError") {
		errMsg += "File: " + args.xamlFile + "     \n";
		errMsg += "Line: " + args.lineNumber + "     \n";
		errMsg += "Position: " + args.charPosition + "     \n";
	}
	else if (errorType == "RuntimeError") {
		if (args.lineNumber != 0) {
			errMsg += "Line: " + args.lineNumber + "     \n";
			errMsg += "Position: " + args.charPosition + "     \n";
		}
		errMsg += "MethodName: " + args.methodName + "     \n";
	}

	throw new Error(errMsg);
}

function changePopup(obj) {
	$('.dhtml').hide();
	$('.' + obj).show();
	$('.' + obj + ' .form_cont').show();
	$('.' + obj + ' .success').hide();
}

function changeRemindPopUp(obj, place, email) {
	$('.dhtml').hide();
	$('.' + obj).show();
	$('#remind_email').val(email);
	$('#remind_place').val(place);
	$('.' + obj + ' .form_cont').show();
	$('.' + obj + ' .success').hide();
}

function submitForm(form) {
	$('.' + form + ' .form_cont').hide();
	$('.' + form + ' .success').show();
}

//String.prototype.encHtml = function() {
//	return (this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'));
//};
function limitChars(textid, limit)
{
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit)
	{
		//$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		//$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}



$(function(){
	$('#user_comment').keyup(function() {
		limitChars('user_comment', 1000);
	})
});
$(function() {
	$('#show_comment').keyup(function() {
		limitChars('show_comment', 1000);
	})
});
$(function() {
	$('#question').keyup(function() {
		limitChars('question', 1000);
	})
});



$(document).ready(function() {

	//	$("#new_video_btn").click(function() {
	//		$("#new_video_page").show();
	//	});
	//	$(".close").click(function() {
	//		$(".dhtml").hide();
	//	});

	$("#QnADropDown").change(function() {
		var selection = $("#QnADropDown > option:selected").attr("value");
		window.location = "QnA?cat=" + selection;
	});

	$("#DailyTipsDropdown").change(function() {
		var selection = $("#DailyTipsDropdown > option:selected").attr("value");
		window.location = "DailyTips?cat=" + selection;
	});

	//question submit form etc
	$(function() {
		$("#remind_validation").hide();
		$("#remind_validation_email").hide();
		$("#remind_server_error").hide();
		$("label").css("color", "black");
		$("#remind_submit").click(function(event) {

			// validate and process form here
			$("#remind_validation").hide();
			$("#remind_validation_email").hide();
			$("#remind_server_error").hide();
			$("label").css("color", "black");
			var valid = true;
			var name = $("input#remind_name").val();
			if (name == "") {
				$("#remind_validation").show();
				$("label#remind_name_label").css("color", "red");
				valid = false;
			}
			var place = $("input#remind_place").val();

			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("input#remind_email").val();
			if (email == "") {
				$("#remind_validation").show();
				$("label#remind_email_label").css("color", "red");
				valid = false;
			}
			else if (!pattern.test(email)) {
				$("#remind_validation_email").show();
				$("label#remind_email_label").css("color", "red");
				valid = false;
			}
			if (!valid)
				return false;

			var dataString = 'place=' + place + '&name=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email);
			//alert (dataString);return false;

			event.preventDefault();
			$.ajax({
				type: "POST",
				url: "RoadShowMap",
				data: dataString,
				success: function() {
					submitForm("remind");
					setTimeout(
						$(".remind").fadeOut(2500, function() {
							$("input#remind_name").attr("value", "");
							$("input#remind_email").attr("value", "");
						}), 4000);
				},
				error: function() {
					$("#remind_server_error").show();
				}
			});
			return false;
		});
	});


	//question submit form etc
	$(function() {
		$("#question_validation").hide();
		$("#question_validation_email").hide();
		$("#question_server_error").hide();
		$("label").css("color", "black");
		$("#question_submit").click(function(event) {

			// validate and process form here
			$("#question_validation").hide();
			$("#question_validation_email").hide();
			$("#question_server_error").hide();
			$("label").css("color", "black");
			var valid = true;
			var name = $("input#question_name").val();
			if (name == "") {
				$("#question_validation").show();
				$("label#question_name_label").css("color", "red");
				valid = false;
			}
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("input#question_email").val();
			if (email == "") {
				$("#question_validation").show();
				$("label#question_email_label").css("color", "red");
				valid = false;
			}
			else if (!pattern.test(email)) {
				$("#question_validation_email").show();
				$("label#question_email_label").css("color", "red");
				valid = false;
			}
			var question = $("#question").val();
			if (question == "") {
				$("#question_validation").show();
				$("label#question_label").css("color", "red");
				valid = false;
			}
			if (!valid)
				return false;

			var dataString = 'name=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email) + '&question=' + encodeURIComponent(question);
			//alert (dataString);return false;

			event.preventDefault();
			$.ajax({
				type: "POST",
				url: "QnA",
				data: dataString,
				success: function() {
					submitForm("advice");
					setTimeout(
						$(".advice").fadeOut(2500, function() {
							$("input#question_name").attr("value", "");
							$("input#question_email").attr("value", "");
							$("#question").attr("value", "");
						}), 4000);
				},
				error: function() {
					$("#question_server_error").show();
				}
			});
			return false;
		});
	});

	//comments submit form etc
	$(function() {
		$("#show_comment_validation").hide();
		$("#show_comment_validation_email").hide();
		$("#show_server_error").hide();
		$("label").css("color", "black");
		$("#show_comment_submit").click(function(event) {

			// validate and process form here
			$("#show_comment_validation").hide();
			$("#show_comment_validation_email").hide();
			$("#show_server_error").hide();
			$("label").css("color", "black");
			var valid = true;
			var name = $("input#show_comment_name").val();
			if (name == "") {
				$("#show_comment_validation").show();
				$("label#show_comment_name_label").css("color", "red");
				valid = false;
			}
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("input#show_comment_email").val();
			if (email == "") {
				$("#show_comment_validation").show();
				$("label#show_comment_email_label").css("color", "red");
				valid = false;
			}
			else if (!pattern.test(email)) {
				$("#show_comment_validation_email").show();
				$("label#show_comment_email_label").css("color", "red");
				valid = false;
			}
			var comment = $("#show_comment").val();
			if (comment == "") {
				$("#show_comment_validation").show();
				$("label#show_comment_label").css("color", "red");
				valid = false;
			}
			if (!valid)
				return false;

			var dataString = 'show_name=' + encodeURIComponent(name) + '&show_email=' + encodeURIComponent(email) + '&show_comment=' + encodeURIComponent(comment) + '&show_id=' + $.jqURL.get("show_id");
			//alert (dataString);return false;

			event.preventDefault();
			$.ajax({
				type: "POST",
				url: "Shows",
				data: dataString,
				success: function() {
					submitForm("show_comment_page");
					setTimeout(
						$(".show_comment_page").fadeOut(2500, function() {
							$("input#show_comment_name").attr("value", "");
							$("input#show_comment_email").attr("value", "");
							$("#show_comment").attr("value", "");
						}), 4000);
					//ShowCommentPageClick(1);
				},
				error: function() {
					$("#show_server_error").show();
				}
			});

			return false;
		});
	});

	//comments submit form etc for user videos
	$(function() {
		$("#user_comment_validation").hide();
		$("#user_comment_validation_email").hide();
		$("#user_server_error").hide();
		$("label").css("color", "black");
		$("#user_comment_submit").click(function(event) {

			// validate and process form here
			$("#user_comment_validation").hide();
			$("#user_comment_validation_email").hide();
			$("#user_server_error").hide();
			$("label").css("color", "black");
			var valid = true;
			var name = $("input#user_comment_name").val();
			if (name == "") {
				$("#user_comment_validation").show();
				$("label#user_comment_name_label").css("color", "red");
				valid = false;
			}
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("input#user_comment_email").val();
			if (email == "") {
				$("#user_comment_validation").show();
				$("label#user_comment_email_label").css("color", "red");
				valid = false;
			}
			else if (!pattern.test(email)) {
				$("#user_comment_validation_email").show();
				$("label#user_comment_email_label").css("color", "red");
				valid = false;
			}
			var comment = $("#user_comment").val();
			if (comment == "") {
				$("#user_comment_validation").show();
				$("label#user_comment_label").css("color", "red");
				valid = false;
			}
			if (!valid)
				return false;

			var dataString = 'user_name=' + encodeURIComponent(name) + '&user_email=' + encodeURIComponent(email) + '&user_comment=' + encodeURIComponent(comment) + '&video_id=' + $.jqURL.get("video_id");
			//alert (dataString);return false;

			event.preventDefault();
			$.ajax({
				type: "POST",
				url: "Videos",
				data: dataString,
				success: function() {
					submitForm("video_comment_page");
					//VideoCommentPageClick(1);
					setTimeout(
						$(".video_comment_page").fadeOut(2500, function() {
							$("input#user_comment_name").attr("value", "");
							$("input#user_comment_email").attr("value", "");
							$("#user_comment").attr("value", "");
						}), 4000);
				},
				error: function() {
					$("#user_server_error").show();
				}
			});
			return false;
		});
	});

	//new video submit form etc
	$(function() {
		$("#video_validation").hide();
		$("#video_validation_email").hide();
		$("#video_server_error").hide()
		$("label").css("color", "black");
		$("#new_video_submit").click(function(event) {

			// validate and process form here
			$("#video_validation").hide();
			$("#video_validation_email").hide();
			$("#video_server_error").hide()
			$("label").css("color", "black");
			var valid = true;

			var name = $("input#video_name").val();
			if (name == "") {
				$("#video_validation").show();
				$("label#video_name_label").css("color", "red");
				valid = false;
			}

			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var email = $("input#video_email").val();
			if (email == "") {
				$("#video_validation").show();
				$("label#video_email_label").css("color", "red");
				valid = false;
			}
			else if (!pattern.test(email)) {
				$("#video_validation_email").show();
				$("label#video_email_label").css("color", "red");
				valid = false;
			}

			var title = $("input#video_title").val();
			if (title == "") {
				$("#video_validation").show();
				$("label#video_title_label").css("color", "red");
				valid = false;
			}

			var place = $("input#video_place").val();
			if (place == "") {
				$("#video_validation").show();
				$("label#video_place_label").css("color", "red");
				valid = false;
			}

			var video_url = $("#video_url").val();
			if (video_url == "") {
				$("#video_validation").show();
				$("label#video_url_label").css("color", "red");
				valid = false;
			}

			if (!valid)
				return false;

			var dataString = 'video_name=' + encodeURIComponent(name) + '&video_email=' + encodeURIComponent(email) + '&video_title=' + encodeURIComponent(title) + '&video_place=' + encodeURIComponent(place) + '&video_url=' + encodeURIComponent(video_url);
			//alert (dataString);return false;
			var url = (window.location.pathname).replace("/Home.aspx", "/Home.aspx/SubmitVideo").replace("/Home.aspx#", "/Home.aspx/SubmitVideo")
			//var url = "/Win7Map/Home.aspx/SubmitVideo"; //window.location.pathname + "SubmitVideo";
			event.preventDefault();
			$.ajax({
				type: "POST",
				url: url,
				data: dataString,
				success: function() {
					submitForm("upload");
					setTimeout(
						$(".upload").fadeOut(2500, function() {
							$("input#video_name").attr("value", "");
							$("input#video_email").attr("value", "");
							$("input#video_title").attr("value", "");
							$("input#video_place").attr("value", "");
							$("#video_url").attr("value", "");
						}), 4000);
				},
				error: function() {
					$("#video_server_error").show();
				}
			});
			return false;
		});
	});
});

