/**
 * @fileoverview tdc.tdc
 */

/**
 * tdc.tdc object
 * @constructor
 */
tdc.tdc = function() {
	return {
		
		/**
		 * Changes the last breadcrumb entry to NOT have an anchor tag!
		 */
		fixLastBreadcrumb : function() {
			try {
				var children = tdc.element.getId("t_breadcrumb").getElementsByTagName("li");
				if (children.length > 0) {
					var atag = children[children.length-1].getElementsByTagName("a");
					if (atag.length > 0) {
						var childnodes = atag[0].childNodes;
						children[children.length-1].removeChild(atag[0]); // doesn't work in MSIE...
						for (var k = 0; k < childnodes.length; k++) {
							children[children.length-1].appendChild(childnodes[k]);
						}
					}
				}
			} catch(err) {}
		},

		movefourthlevel : function() {
		},

		/**
		 * Create the correct size popup for F5 design.
		 * @param {String} Url to open
		 * @param {String} Window size - optional
		 * @param {String} Window name - optional
		 * @param (Integer) Window height - optional
		 */
		popup : function(aUrl,aSize,aName,aHeight) {
			var width = 530;
			var height = 300;

			var size = 'normal';
			if (typeof(aSize) != 'undefined') size = aSize;

			var windowname = '';
			if (typeof(aName) != 'undefined') windowname = aName;

			if (size == 'large') {
				width = 770;
				height = 440;
			}

			if (typeof(aHeight) != 'undefined' && aHeight > 199 && aHeight < 576 ) height = aHeight;

			tdc.popup(aUrl,width,height,windowname);
		},

		/**
		 * Open NCOM chat popup
		 */
		openncomchat : function(){			
			tdc.popup('http://lg3.medianet.dk/solutions/tdc_012007/customer/index2.php?myurl=' + escape(document.location.toString()),490,468,'chat');
			return false;
		}
	}

}();



/**
 * tdc.tdc.dynmenu object
 * @constructor
 */
tdc.tdc.dynmenu = function() {

	/**
	 * Holds the current dynmenu menu
	 * @type Object
	 */
	var menu = false;

	/**
	 * What CSS class to add to a current item
	 * @type String
	 */
	var classon = "t_current";

	/**
	 * What default class the dynmenu is added to
	 * @type String
	 */
	var div = "t_leftnav";

	/**
	 * Fourth level of navigation must be on the content area!
	 * Defaults to empty!
	 */
	var level4;

	/**
	 * Returns the correct node for a level
	 * @param {Boolean} aUL The top UL root
	 * @param {Boolean} aLevel What level should we insert at
	 * @param {Boolean} aIndent What indent are we current add
	 * @returns A node
	 * @author Henrik Gemal
	 */
	function getNode(aUL, aLevel, aIndent) {
		var d;
		if (aLevel > aIndent) {
			// find the last li
			var lis = aUL.getElementsByTagName("li");
			d = lis[lis.length-1];
		} else {
			// find the last ul with level == indent
			var uls = aUL.getElementsByTagName("ul");
			for (var i = uls.length-1; i >= 0; i--) {
				if (uls[i].className == "t_dm_level" + aLevel) {
					d = uls[i];
					break;
				}
			}
		}
		return d;
	}

	return {

		/**
		 * Initializes the dynmenu object
		 * @param {Object} aObj A dynmenu object
		 * @author Henrik Gemal
		 */
		init : function(aObj) {
			menu = aObj;
		},

		/**
		 * Change the default placeholder for the menu
		 * @param (String) aString The id of the placeholder
		 */
		placeholder : function(aDiv) {
			div = aDiv;
		},

		/**
		 * Builds the dynmenu
		 * @author Henrik Gemal
		 */
		build : function() {
			// make sure we have an dynmenu object
			if (typeof(menu) == "object" && menu && menu.length > 0) {
				var nav = tdc.element.getId(div);
				if (nav) {
					var ele;
					var navul = nav.getElementsByTagName("ul");
					// if the UL isn't there we have to create it first
					if (!navul || !navul.length) {
						ele = document.createElement("ul");
						nav.appendChild(ele);
						navul = nav.getElementsByTagName("ul");
					}

					var indent = 1;
					var firstIndent = indent;
					// insert the links
					for (var i = 0; i < menu.length; i++) {

						// make sure we have a object
						if (typeof(menu[i]) != "object") {
							break;
						}

						// Set first indent value
						if (i == 0 && typeof(menu[i][4]) != "undefined") {
							firstIndent = menu[i][4];
						}

						// create the li with the link
						ele = document.createElement("li");
						if (typeof(menu[i][1]) != "undefined" && menu[i][1] == "") {
							ele.className = "t_leftnav_subheader";
						}


						// should the the menuitem be active
						if (typeof(menu[i][5]) == "boolean" && menu[i][5]) {
							// need to set both since IE doesn't support class
							ele.className = classon;
						}
						ele.appendChild(tdc.dynmenu.createLink(menu[i]));

						// do we have to indent it?
						if (typeof(menu[i][4]) == "number" && menu[i][4] > firstIndent && menu[i][4] < 5) {
							// where should we add the item
							var cul = getNode(navul[0], menu[i][4], indent);
							// if we have to indent add the ul
							if (menu[i][4] > indent) {
								var eleul = document.createElement("ul");
								eleul.className = "t_dm_level" + menu[i][4];
								eleul.style.display = "none";
								cul = cul.appendChild(eleul);
							}
							cul.appendChild(ele);
						} else {
							navul[0].appendChild(ele);
						}
						// set the indent level
						if (typeof(menu[i][4]) != "undefined") {
							indent = menu[i][4];
						} else {
							indent = 1;
						}
					}

					var activeLine = new Array();
					var tree = navul[0].getElementsByTagName("li");
					var staticMenuPresent = false;

					for (var j = 0; j < tree.length; j++) {
						if (tree[j].id) {
							staticMenuPresent = true;
						}
						if (tree[j].className == classon) {
							activeLine[activeLine.length] = tree[j];
							var childNode = tree[j].getElementsByTagName("ul");
							if (typeof(childNode[0]) == "object") {
								childNode[0].style.display = "block";
							}
							var parent = tree[j].parentNode;
							while (parent.id != div) {
								if (parent.tagName == "ul" || parent.tagName == "UL") {
									parent.style.display = "block";
								}
								if (parent.tagName == "li" || parent.tagName == "LI") {
									activeLine[activeLine.length] = parent;
								}
								parent = parent.parentNode;
							}
						}
					}

					var atag;
					var li;
					var ittModifier = (staticMenuPresent) ? activeLine.length-6 : 1;
					for (i = (activeLine.length-1); i >= (firstIndent - ittModifier); i--) {
						atag = activeLine[i].getElementsByTagName("a");
						if (typeof(atag[0]) == "object") {
							li = document.createElement("li");
							li.appendChild(atag[0].cloneNode(true));
							tdc.element.getId("t_breadcrumb").appendChild(li);
						}
					}
				}
			}
		}
	}
}();

/**
 * tdc.tdc.sso object
 * @constructor
 */
tdc.tdc.sso = function() {
	return {
		
		
		//wrappers to be deleted in august 2007
		isLoggedIn : function() {
			return tdc.sso.isLoggedIn();
		},
		validateLogin : function(aForm){
			return tdc.sso.validateLogin(aForm);
		},
		
		/**
		 * shows the logout button in the topimage
		 * @author Kim Johannesen
		 */
		showLogoutButton : function() {
			if( tdc.cookie.get("res_tdc_dk") ){				
				var element = tdc.element.getId("t_content");
				if(element){
					var tmpProtocol = (location.protocol == "https:" ? "https" : "http") + "://sso.tdconline.dk/tdconline/tdclogout"; 
					$j('<form action="' + tmpProtocol + '" onsubmit="return tdc.tdc.sso.validateLogout();" id="t_logout_form"></form>').appendTo('#t_content');
					$j('<button class="t_button_gray" type="submit" id="t_sso_logoutbutton"><span>Log ud</span></button>').appendTo('#t_logout_form');
				}
			}
		},

		// Switch the password fields so the user can type his actual password
		switchPassword : function(aForm, aForce) {
			var one = aForm.form["usr_password_fake"];
			var two = aForm.form["usr_password"];

			// force the switch
			if (aForce) {
				one.style.display = "none";
				two.style.display = "block";
				two.focus();
			}

			if (two.value == "") {
				// Show/hide the real password field
				if (one.style.display == "" || one.style.display == "block") {
					one.style.display = "none";
					two.style.display = "block";
					two.focus();
				} else {
					one.style.display = "block";
					two.style.display = "none";
				}
			}
		},

		// Switch the password/digital signature
		switchDigital : function(aType) {
			var one = tdc.element.getId("tdcosso-userpass" + aType);
			var two = tdc.element.getId("tdcosso-digisig" + aType);
			if (!one || !two) {
				return;
			}
			if (one.style.display == "" || one.style.display == "block") {
				one.style.display = "none";
				two.style.display = "block";
			} else {
				one.style.display = "block";
				two.style.display = "none";
			}
		},

		// Draw the SSO login box
		showLogin : function(aType) {
			var tabindex;
			// Try to get the login name from the cookie
			var sr = tdc.sso.getUsername();

			var ssocertremember = tdc.cookie.get("SSOLOGINCERTREMEMBER");
			var linktg = (typeof(ssolinkstarget) != "undefined" && ssolinkstarget) ? ssolinkstarget : "top";

			aType = (aType ? aType : 0).toString();
			document.writeln('<div class="tdcosso" id="tdcosso-type' + aType + '">');
			document.writeln('<h3>TDC login</h3>');

			// Are we logged in?
			if (tdc.sso.isLoggedIn()) {
				document.writeln('<div class="tdcosso-logout">');
				document.writeln('<form action="' + (location.protocol == "https:" ? "https:" : "http:") + '//sso.tdconline.dk/tdconline/tdclogout" onsubmit="return tdc.tdc.sso.validateLogout()">')
				if (typeof(ssologouttarget) != "undefined" && ssologouttarget)
					document.writeln('<input type="hidden" name="tg" value="' + escape(ssologouttarget) + '">')
				else if (document.location.host == "selvbetjening.tdconline.dk" || document.location.host == "fastnetselvbetjening.tdconline.dk")
					document.writeln('<input type="hidden" name="tg" value="' + escape("http://kundeservice.tdconline.dk/selvbetjening/") + '">')
				if (aType == 3)
					document.writeln('<input class="tdcosso-submit" type="submit" id="type3logoutbtn" value="" title="Log ud af TDC Login" tabindex="3" />');
				else
					document.writeln('<input class="tdcosso-submit" type="submit" value="" title="Log ud af TDC Login" tabindex="3" />');
				if (sr)
					document.writeln('<p>Du er logget ind som:</p><p><strong>' + sr + '</strong></p><p>&Oslash;nsker du at logge ud?</p> ');
				if (aType == 3)
					document.writeln('<p>G&aring; til din <a href="http://tdconline.dk/mit/"><strong>personlige oversigt</strong></a></p>');
				document.writeln('</form>');
				document.writeln('</div>');
				document.writeln('<ul class="tdcosso-logout">');
				if (aType != 3)
					document.writeln('<li><a href="http://tdconline.dk/mit/" title="G&aring; til din personlige oversigt" target="_' + linktg + '">Personlig oversigt</a></li>');
				document.writeln('<li><a href="https://selvbetjening.tdconline.dk/eservice/ditLogin.do" title="Redigér dine indstillinger" target="_' + linktg + '">Indstillinger</a></li>')
				document.writeln('<li><a href="http://login.tdconline.dk/artikel.php?dogtag=tdco_login_om" title="L&aelig;s mere om TDC Login" target="_' + linktg + '">Om TDC Login</a></li>');

				document.writeln('</ul>');
			}
			// Is SSO down?
			else if (typeof(loginActive) != "undefined" && loginActive == 0)
			{
				document.writeln('<div class="tdcosso-disabled"><p>Der er i &oslash;jeblikket drifts- forstyrrelser p&aring; login til TDC. Vi arbejder p&aring; en l&oslash;sning.</p></div>');
			}
			// Is SSO disabled?
			else if (tdc.sso.isDisabled())
			{
				document.writeln('<div class="tdcosso-disabled"><p>Login er deaktiveret i din browser.</p><p><a href="http://tdconline.dk/testclient.php?case=old" target="_' + linktg + '">Hvorfor?</a></p></div>');
			}
			// Draw the login box
			else
			{
				if (aType == 3)
					document.writeln('<p>Log ind til <strong>mail, foto, selvbetjening</strong> m.m.</p>');
				document.writeln('<form action="https://access.tdc.dk/servlet/getAccessLogin" method="post" onsubmit="return tdc.tdc.sso.validateLogin(this)" autocomplete="off">')
				document.writeln('<input type="hidden" name="HiddenURI" value="' + (typeof(ssologinhiddenuri) == "undefined" || !ssologinhiddenuri ? "https://sso.tdconline.dk/redir/?http://sso.tdconline.dk/tdconline/tdclogin" : ssologinhiddenuri) + '" />')
				document.writeln('<input type="hidden" name="LOCALE" value="da_DK" />')
				document.writeln('<input type="hidden" name="AUTHMETHOD" value="UserPassword" />')
				document.writeln('<div class="tdcosso-userpass" id="tdcosso-userpass' + aType + '"' + (ssocertremember ? ' style="display:none"' : '') + '>');
				if (aType == 0)
					tabindex = 8;
				else if (aType == 1)
					tabindex = 4;
				else if (aType == 2)
					tabindex = 12;
				if (aType == 3)
					tabindex = 0;
				document.writeln('<input type="text" name="usr_name" title="Dit brugernavn til TDC Login" class="tdcosso-name" tabindex="'+(tabindex+1)+'" value="' + (sr ? sr : 'Brugernavn' ) + '" onfocus="tdc.tdc.sso.loginFocus(this, event);" onblur="tdc.tdc.sso.loginFocus(this, event);"' + '>')
				document.writeln('<input class="tdcosso-password-fake" type="text" value="Password" name="usr_password_fake" title="Dit password til TDC Login" tabindex="'+(tabindex+2)+'" onkeypress="tdc.tdc.sso.switchPassword(this, 1);" onfocus="tdc.tdc.sso.switchPassword(this);" />');
				document.writeln('<input class="tdcosso-password" type="password" value="" name="usr_password" title="Dit password til TDC Login" tabindex="9992" onblur="tdc.tdc.sso.switchPassword(this);" />');
				document.writeln('<div class="tdcosso-check">');
				document.writeln('<label><input type="checkbox" tabindex="'+(tabindex+4)+'" name="tdcosso-remember"' + (tdc.cookie.get("SSOLOGINUSER")?' checked="checked"':'')+'> Husk brugernavn</label>')
				document.writeln('</div>');
				document.writeln('</div>');
				document.writeln('<div class="tdcosso-digisig" id="tdcosso-digisig' + aType + '"' + (ssocertremember ? ' style="display:block"' : '') + '></div>');
				document.writeln('<input class="tdcosso-submit" type="submit" value="" title="Log ind i TDC Login" tabindex="'+(tabindex+3)+'" />');
				document.writeln('<div class="tdcosso-check-sig">');
				document.writeln('<input name="tdcosso-signature" class="tdcosso-signature" type="checkbox" tabindex="9995" onclick="tdc.tdc.sso.switchDigital(' + aType + ')"' + (ssocertremember ? ' checked="checked"' : '') + ' /> Log ind med <a href="http://privat.tdc.dk/artikel.php?dogtag=tdc_p_digi_pers_bestil" title="L&aelig;s mere om Digital Signatur" target="_' + linktg + '">Digital Signatur</a>');
				document.writeln('</div>');
				document.writeln('</form>');
				document.writeln('<ul>');
				document.writeln('<li><a href="' + (typeof(ssologinregister) == "undefined" || !ssologinregister ? "https://registrering.tdconline.dk/eservice/showtdclogin.do?popup=true" : ssologinregister) + '" title="Opret et nyt TDC Login" target="_' + linktg + '" onclick="return tdc.tdc.sso.cookieUpdate(this)">Ny bruger</a></li>')
				document.writeln('<li><a href="' + (typeof(ssologinnewpassword) == "undefined" || !ssologinnewpassword ? "http://tdc.dk/artikel.php?dogtag=tdco_login_sms" : ssologinnewpassword) + '" title="F&aring; hj&aelig;lp hvis du har glemt dit password til TDC Login" target="_' + linktg + '">Glemt password?</a></li>')
				document.writeln('<li><a href="http://login.tdconline.dk/artikel.php?dogtag=tdco_login_om" title="L&aelig;s mere om TDC Login" target="_' + linktg + '">Om TDC Login</a></li>');
				document.writeln('</ul>');
			}
			document.writeln('</div>');
		}
	}

}();

/**
 * tdc.tdc.service object
 * @constructor
 * This object is duplicated in front.js so please update both!
 */
tdc.tdc.service = function() {

	// wrapper funktion
	function isKeyword(aStr) {
		return tdc.isKeyword(aStr);
	}
	
	return {

		/**
		 * Checks a search form
		 * @param {String} aId ID of search term form
		 * @returns
		 * @type Boolean
		 * @author Henrik Gemal
		 */
		checkForm : function(aId) {
			var searchword = tdc.element.getId(aId).value;
			ret = false;
			if (isKeyword(searchword)) {
				tdc.redir("http://kundeservice.tdc.dk/keyword.php?stdcdk&kid=" + searchword);
			} else if (searchword != "Søg på tdc" && searchword != "" && searchword != "Søg i Kundecenter") {
				ret = true;

				try {
					if (document.domain == 'erhverv.tdc.dk') {
						tdc.element.getId('t_pcid').value = '8';
					} else if (document.domain == 'om.tdc.dk') {
						tdc.element.getId('t_pcid').value = '9';
					}
					if (document.domain == 'privat.tdc.dk') {
						tdc.element.getId('t_pcid').value = '23';
					}
				} catch (error) {}
			}
			return ret;
		}

	}

}();

tdc.tdc.feedback = function() {

	return {
		toggleComment : function() {

			if (tdc.element.getId('opinion_ja').checked == true) {
				Instadia_sendEvent('0118m', 'FB3#' + yes_id + '#' + CScontext + '_ja');
			} else {
				Instadia_sendEvent('0118m', 'FB3#' + no_id + '#' + CScontext + '_nej');
			}
			//disable so that the user cannot toggle back and forth to keep generating sendevents
			tdc.element.getId('opinion_ja').disabled = true;
			tdc.element.getId('opinion_nej').disabled = true;
			tdc.element.getId('furtherCommentsLink').style.display = 'block';
		},

		moreComments : function(templateId) {
				tdc.element.getId('holdsInstadiaTemplate').style.display = 'block';
				Instadia_launchSurvey(templateId, 'holdsInstadiaTemplate', function() { tdc.tdc.feedback.afterSurveySubmit(); });
			return true;
		},

		afterSurveySubmit  : function() {
			tdc.element.getId('holdsInstadiaTemplate').style.display = 'none';
			tdc.element.getId('furtherCommentsLink').style.display = 'none';
			//say thank you if the user filled out the Instadia form
			tdc.element.getId('second').style.display = 'block';
		}
	}
}();

tdc.tdc.guide = function() {
	return {
		openFlashMovie : function(aId) {
		  var flashURL = '';
		  switch (aId)
		  {
		    case 1:
					flashURL = 'http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/01_tdc/guide01-tdcdk.swf';
					break;
		    case 2:
					flashURL = 'http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/02_bredbaand/guide02-bredbaand.swf';
					break;
		    case 3:
					flashURL = 'http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/03_fastnet/guide03-fastnet.swf';
					break;
		    case 4:
					flashURL = 'http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/04_netway/guide04-netway.swf';
					break;
		    case 5:
					flashURL = 'http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/05_bredbaandstelefoni/guide05-bredbaandstelefoni.swf';
					break;
			}


			if (flashURL != '') {
			  window.open(flashURL, "flashmovie", "width=577, height=396, left=150, top=100, resizable=0, scrollbars=0");
			}
    },


    playFlash : function (aId) {
    	if(tdc.browser.hasFlash() >= 8) {
		  	document.write('<div class="t_block t_block t_block_single t_block_border_b">');
			  document.write('<object type="application/x-shockwave-flash"  data="http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/bannere/banner_guide0'+aId+'.swf?jsfunc=tdc.tdc.guide.openFlashMovie('+aId+')" width="208" height="190">');
			  document.write('<param name="movie" value="http://download.tdc.dk/pub/tdconline/kundeservice/videoguides/bannere/banner_guide0'+aId+'.swf?jsfunc=tdc.tdc.guide.openFlashMovie('+aId+')" \/>');
			  document.write('<param name="allowScriptAccess" value="always" \/>');
			  document.write('<img src="http://i.c.dk/pics/6/8/7/40786/208x120.jpg" alt="" \/>');
			  document.write('</object>');
			  document.write('</div>');
			}
	  }
	}

}();
