﻿function SetContentSize() {

	var ch = xClientHeight();
	var pageheight = xHeight("PageContent") + xHeight("TopContent") + xHeight("BottomContent");
	if (pageheight < ch) {
		pageheight = ch;
		xHeight("PageContent", ch - xHeight("TopContent") - xHeight("BottomContent") - 30);
	}

}

function MarkPrintArea() {
	if (document.getElementById("MainContent")) {
		//		Might want to do something here	
		//		document.getElementById("MainContent").style
	}
}

function UnMarkPrintArea() {
	if (document.getElementById("MainContent")) {
		//		Might want to do something here	
		//		document.getElementById("MainContent").style.border = '';
	}
}
function PrintDocument() {

	var pr = window.print ? true : false;

	var printAreas = new Array();
	var count = 0;

	if (document.getElementById("routePlannerMap")) {
		printAreas[count++] = document.getElementById("routePlannerMap");
		printAreas[count++] = document.getElementById("routePlannerInfo");
	}
	else if (document.getElementById("MiddleInnerContent")) {
		printAreas[count] = document.getElementById("MiddleInnerContent");
	}
	else if (document.getElementById("LeftContent1")) {
		printAreas[0] = document.getElementById("LeftContent1");
		//		printAreas[1] = document.getElementById("RightContent1");
	}
	else if (document.getElementById("LeftContent2")) {
		printAreas[0] = document.getElementById("LeftContent2");
		//printAreas[1] = document.getElementById("RightContent2");
	}
	else if (document.getElementById("PageContent")) {
		printAreas[0] = document.getElementById("PageContent");
	}
	/*
	else 
	{
	var pagedivs = document.getElementsByTagName("div");
	for(i=0;i<pagedivs.length;i++) {
	if (pagedivs[i].getAttribute("printID") != null) {
	if (pagedivs[i].getAttribute("printID")==id && pagedivs[i].style.display != "none")  { // print only specified id
	printAreas[count] = pagedivs[i];
	count++;
	}
	}
	}
	}*/

	if (printAreas.length == 0) { // Did not find anything to print.
		alert("Nothing to print!");
		return;
	}

	// printdocument header
	var sStart = "<html><head>";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/Base.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/Framework.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/Block.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/print.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/contextmenu.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Styles/editor.css\" >";
	sStart += "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://staging.dev.virtualearth.net/mapcontrol/v6.2/css/bin/6.2.2008082210001.41/en/mapcontrol.css\" >";

	sStart += "</head><body class=\"Print\"><img src=\"/images/clear.gif\"><br><br><div style=\"width: auto;\">\n";
	sStart += '<p><img src="/images/icons/print_preemlogo.gif" border="0" /></p><br><br>';
	// printdocument footer
	var sEnd = "</div></body></html>";



	// set printwindow size;
	var wWidth = 720;
	var wHeight = (screen.height > 0) ? screen.height - 200 : 500;
	var wTop = 50;
	var wLeft = (screen.width > 0) ? (screen.width - wWidth) / 2 : 100;


	var browser = window.open('', 'printWin', 'top=' + wTop + ',left=' + wLeft + ',width=' + wWidth + ',height=' + wHeight + ',menubar=yes,scrollbars=yes,toolbar=yes');
	browser.document.writeln(sStart);

	for (i = 0; i < printAreas.length; i++) {
		browser.document.writeln(printAreas[i].innerHTML + "\n");
	}

	browser.document.writeln(sEnd);

	browser.document.close();

	if (pr)
		browser.print();
}


function MenuItemOver(currentobj, index) {
	alert('over');
	currentobj.style.display = 'none';
	//	document.getElementById(showObject).style.visibility = 'visible';
}

function MenuItemOut(currentobj, index) {
	alert('out');
	currentobj.style.display = 'none';
	//	document.getElementById(showObject).style.visibility = 'visible';
}

function OpenPopupWindow(url, width, height) {
	var page = document.open(url, "PopUp", "height=" + height + ",width=" + width + ",menubar=no,resizable=no,scrollbars=auto,status=no,toolbar=no");
	page.focus();
}

function MoveNext(current, next) {
	var elem = document.getElementById(current);
	if (elem.value.length > 3) {
		var elem2 = document.getElementById(next);
		elem2.focus();
	}
}

// from prettycode.org
window.location.querystring = (function() {
	var collection = {};

	// Gets the query string, starts with '?'
	var querystring = window.location.search;

	// Empty if no query string
	if (!querystring) {
		return { toString: function() { return ""; } };
	}

	// Decode query string and remove '?'
	querystring = decodeURI(querystring.substring(1));

	// Load the key/values of the return collection
	var pairs = querystring.split("&");

	for (var i = 0; i < pairs.length; i++) {
		// Empty pair (e.g. ?key=val&&key2=val2)
		if (!pairs[i]) {
			continue;
		}

		// Don't use split("=") in case value has "=" in it
		var seperatorPosition = pairs[i].indexOf("=");

		if (seperatorPosition == -1) {
			collection[pairs[i]] = "";
		}
		else {
			collection[pairs[i].substring(0, seperatorPosition)]
                = pairs[i].substr(seperatorPosition + 1);
		}
	}

	// toString() returns the key/value pairs concatenated
	collection.toString = function() {
		return "?" + querystring;
	};

	return collection;
})();

var Flash = {
	Hide: function() {
		$("object").each(function(i) {
			$(this).hide();
		});
	},
	Show: function() {
		$("object").each(function(i) {
			$(this).show();
		});
	}
};

var BlockUIWrapper = {

	Html: {
		OverlayContent: "div#OverlayContent",
		OverlayImage: "img#OverlayImage",
		BlockOverlay: "div.blockOverlay"
	},

	Initialize: function(hideAfterInSeconds) {

		var instance = this;
		$.blockUI({
			message: $(instance.Html.OverlayContent),
			css: {
				width: '699px',
				height: '279px',
				cursor: 'default',
				top: ($(window).height() - 279) / 2 + "px",
				left: ($(window).width() - 699) / 2 + "px"
			}
		});
		$(instance.Html.BlockOverlay).attr("title", "Klicka för att stänga").click(BlockUIWrapper.Unblock);
		Flash.Hide();

		setTimeout(function() {
			BlockUIWrapper.Unblock();
		}, hideAfterInSeconds);
	},

	Unblock: function() {
		$.unblockUI();
		Flash.Show();
	}
};

var RoutePlanner = {
	Preloader: {
		Show: function() {
			jQuery("img#routePlannerMapPreloader").show();
		},
		Hide: function() {
			jQuery("img#routePlannerMapPreloader").hide();
		}
	},
	GetTable: function() {
		return "<div class=\"YellowSeparator\"><table id=\"stationsTable\">";
	},
	GetTableHeader: function() {
		return "<tr><td class=\"StationListHeader header4\">Stationsnamn</td><td class=\"StationListHeader header4\">Ort</td><td class=\"StationListHeader header4\">Adress</td><td class=\"StationListHeader header4\">Typ</td></tr>";
	},
	GetTableFooter: function() {
		return "</table></div>";
	},
	GetName: function(name) {
		if (name.indexOf(",") != -1) {
			try {
				return name.split(",")[1];
			} catch (e) {
				return name;
			}
		}
		return name;
	},
	TypeToIcon: function(type) {
		try {
			return type.replace("å", "a").replace("ä", "a").replace("ö", "o");
		} catch (e) {
			return type;
		}
	},
	GetIcon: function(type) {
		type = type.toLowerCase();
		if (type == "") return "";

		var icon = this.TypeToIcon(type);
		return this.GetIconHtml(icon);
	},
	GetSaifaIcon: function(isConnected) {
		try {
			return isConnected.toLowerCase() == "true" ? this.GetIconHtml("saifa-ansluten") : "";
		} catch (e) {
			return false;
		}
	},
	GetIconHtml: function(icon) {
		return "<img src=\"/images/icons/" + icon + ".gif\" alt=\"" + icon + "\" title=\"" + icon + "\" class=\"TypeIcon\" />";
	},
	Capitalize: function(str) {
		try {
			return str.toLowerCase().replace(/(^|\s)([a-z])/g, function(m, p1, p2) { return p1 + p2.toUpperCase(); });
		} catch (e) {
			return str;
		}
	}
};

(function($) {	
	$.fn.disableSend = function() {
		var form = $(this),
			btn = form.find('button:first'),
			required = form.find('strong.required').parent().next(),
			radio = form.find('input[type=radio]'),
			rest = form.find("li:gt(3)").find('input');
			
			//start by disabling the button
			btn.addClass('disabled').attr("disabled", true);
		
			//If fields with required are empty then disable the button
			form.delegate('.required-field', 'keyup', function(){
				fieldRequired();
			});
			
			radio.bind('change', function(){
				feedbackRequired();
			});
			
			function fieldRequired(){
				var empty = false;
				$('.required-field').each(function() {	
					if ($(this).val() == '') {
						empty = true;
					}
				});
				
				if (empty) {
					btn.addClass('disabled').attr('disabled', true);
				} else {
				    btn.removeClass('disabled').attr('disabled', false);
				}
			}
			
			function feedbackRequired(){
				//checked - make fields below required 
			    if (radio.eq(0).is(':checked')) {
					rest.each(function(){
						if($(this).prev().find('strong').length == 0){
							$(this).prev().append('<strong class="required"> *</strong>');
							form.find('strong.required').parent().next().addClass('required-field');
							fieldRequired();
						}
					});
			    //unchecked - make sure fields below are not required
				}else{
					rest.each(function(){
						$(this).prev().find('strong').remove();
						$(this).removeClass('required-field');
					});
					fieldRequired();
			    }
			}
			
			
			//Trigger on start
			required.trigger('keyup');
			feedbackRequired();
	};
	$(document).ready(function() {
		$('fieldset.contact-form').disableSend();
	});
})(jQuery);
