﻿/// <reference name="MicrosoftAjax.js"/>
function WindowClosed(sender, eventArgs) {

	if (eventArgs._argument != null)
		if (eventArgs._argument.PostBack) {
			window.location = "MainPage.aspx";
	}
}

// Works only in IE, but since it only needs to work in 7 the
// mozilla way is not implemented.
window.onload = function() {

	if (navigator.appVersion.match("MSIE 7") == "MSIE 7") {
		if (lockedMenuID == null) { document.getElementById("subContainer").style.display = "none" };
	}
}

var activeMenu;
var mouseOnMenu;

var lockedMenuID;
var lockedMenuItemID;

function EnableSubMenu(id) {
	if (activeMenu != null)
		DisableActiveMenu(false);

	if (lockedMenuID != null) {
		var lockedMenu = document.getElementById(lockedMenuID);
		document.getElementById('menu').className = "Menu";
		lockedMenu.style.display = "none";
		lockedMenu.getElementsByTagName("UL")[0].className = "menuSub";
	}
		
	activeMenu = document.getElementById(id);

	document.getElementById('menu').className = "Menu_Selected";
	activeMenu.style.display = "inline";
	activeMenu.getElementsByTagName("UL")[0].className = "menuSub_active";

	if (navigator.appVersion.match("MSIE 7") == "MSIE 7") {
		document.getElementById("subContainer").style.display = "block";
	}
}

function DisableActiveMenu(delayDisable) {

	if (delayDisable)
	{ setTimeout('DisableActiveMenu(false);', 250); return; }

	if (!mouseOnMenu) {
		if (activeMenu != null) {
			activeMenu.style.display = 'none';
			activeMenu.getElementsByTagName("UL")[0].className = "menuSub";
			document.getElementById('menu').className = 'Menu';
		}
		if (lockedMenuID != null) {
			var lockedMenu = document.getElementById(lockedMenuID);
			document.getElementById('menu').className = "Menu_Selected";
			lockedMenu.style.display = "inline";
			lockedMenu.getElementsByTagName("UL")[0].className = "menuSub_active";
		} else {
			if (navigator.appVersion.match("MSIE 7") == "MSIE 7") {
				document.getElementById("subContainer").style.display = "none";
			} 
		}
	}


}

function LockSubMenuItem(subMenuID, subMenuItemID) {

	lockedMenuID = subMenuID;
	lockedMenuItemID = subMenuItemID

	$get(lockedMenuItemID).className = "menuSubItemSelected";
	
	EnableSubMenu(lockedMenuID);
}

/*	
Opens a radwindow based on the given variables.
If an existing name is given it will open a predefines window else a new one.
Returns the window reference
				
URL: page to open
width: width of the radwindow
height: height of the radwindow
windowName: windows name, use an existing name to open an existing window
any other name will open a new window
center: boolean, defines wether the center method should be called or not.
*/
function openRadWindow(URL, width, height, windowName, center) {
	//var windows = manager.get_windows();
	var oWnd = radopen(URL + "&width" + width + "&height=" + height, windowName);

	//moveTo modifies x, y. However IE7 places it on -2, -2 with values 0, 0
	if (center)
		oWnd.moveTo(2, 2);

	if (width > 0)
		oWnd.set_width(width);
	if (height > 0)
		oWnd.set_height(height);

	if (center)
		oWnd.center();

	//Make sure the window is in front.
	setTimeout(function() { oWnd.setActive(true); }, 0);

	return oWnd;
}

/*
Looks for the given window by name.
When found it looks wether the window is visible or not
and inverts this.
					
Note: the restoring of the position is not fully working yet.
so I added a try catch there.
*/
function ShowHideRadWindow(WindowName) {
	var manager = $find("<%=RadWindowManagerMain.ClientID %>");

	if (manager != null) {
		windows = manager.get_windows();
		if (windows != null) {
			var i = 0;
			for (i = 0; i < windows.length; i++) {
				if (windows[i]._name.match(WindowName) == WindowName) {

					if (windows[i].IsVisible()) {

						if (windows[i].style == null)
							windows[i].Hide();
						else
							windows[i].style.display = "none";
					} else {
						if (windows[i].style == null)
							windows[i].Show();
						else
							windows[i].style.display = "inline";
						windows[i].Center();
					}
				}
			}
		}
	}
}

function RegExMatch(address) {
	var regEx = "[0-9]{4}";
	var re = new RegExp(regEx);
	if (address.match(re)) {
		return true;
	} else {
		return false;
	}
}
function GetAddressInput(inputFieldName) {
	var address;
	address = document.getElementById(inputFieldName).value;

	var regExpressionControl;

	if (document.getElementById('masterPage_cphBody_regExpre') != null) {
		regExpressionControl = document.getElementById('masterPage_cphBody_regExpre');
	}
	else {
		regExpressionControl = document.getElementById('masterPage_cphBody_ActDirectlyControl_regExpre');
	}

	var result = RegExMatch(address);
	if (!result) {
		alert('Er is een ongeldige postcode ingevuld.');
	}
	else {
		if (address != null && address != "") {
			getLongLat(address);
		}
		else {
			alert('Er is een ongeldige postcode ingevuld.');
		}
	}
}


function getLongLat(address) {
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode({ 'address': address, 'region': 'NL' }, getLongLatCompleted);
}

function getLongLatCompleted(results, status) {

	if (status == google.maps.GeocoderStatus.OK) {
		var window = openRadWindow('LightBoxPage.aspx?title=Uw CarXpert&type=geolocator' +
					'&postalCode=' + document.getElementById('inputPostal').value +
					'&latitude=' + results[0].geometry.location.lat() +
					'&longitude=' + results[0].geometry.location.lng(), 900, 550, 'LightBox', true);
		window.set_title('Uw CarXpert');
	}
	else {
		alert('Er is geen geldige postcode ingevuld.');
	}
}