jQuery.noConflict();

jQuery(function() {
	
	setAction();

	jQuery("ul#login-tabs").ready(function() {
		var path = window.location.hash;
		if (path) {
			jQuery("ul#login-tabs a").each(function() {
				var currentTab = jQuery(this).parent();
				if (path == jQuery(this).attr("href")) {
					jQuery(currentTab).addClass("selected");
				} else {

					jQuery(currentTab).removeClass("selected");
				}
			});
		 }
	});

	// Toggle the label "Användarnamn" to "Personnummer" if the corresponding radio button is checked
	jQuery("input#login-ms-anv").click(function(){
		jQuery("#loginform-minasidor label.text.login-ms-username span").html("Användarnamn:");
		jQuery("#login-ms-username").val("Användarnamn");
		jQuery("#loginform-minasidor label.text.login-ms-password span").html("Lösenord:");
		jQuery("#loginform-minasidor .forgot #ms-password").show();
		jQuery("#loginform-minasidor .forgot #ms-condition").hide();

		// Change type of input field
		jQuery("#login-ms-password").each(function(){
			var name = jQuery(this).attr('name');
			var size = jQuery(this).attr('size');
			var id = jQuery(this).attr('id');
		  	var html = '<input id="'+id+'" type="password" name="'+name+'" size="'+size+'" />';
	  		jQuery(this).after(html).remove();
			jQuery("#login-ms-password").val("******");
			jQuery("#login-ms-password").focus(function(){
				jQuery(this).attr("value","");
			});
		});
	});
	jQuery("input#login-ms-pers").click(function(){
		jQuery("#loginform-minasidor label.text.login-ms-username span").html("Personnummer:");
		jQuery("#login-ms-username").val("ååååmmddxxxx");
		jQuery("#loginform-minasidor label.text.login-ms-password span").html("Fakturanummer:");

		// Change type of input field
		jQuery("#login-ms-password").each(function(){
			var name = jQuery(this).attr('name');
			var size = jQuery(this).attr('size');
			var id = jQuery(this).attr('id');
		  	var html = '<input id="'+id+'" type="text" name="'+name+'" size="'+size+'" />';
	  		jQuery(this).after(html).remove();
			jQuery("#login-ms-password").val("OCR-nummer");
			jQuery("#login-ms-password").focus(function(){
				jQuery(this).attr("value","");
			});
		});
		jQuery(".forgot #ms-password").hide();
		jQuery(".forgot #ms-condition").show();
	});
	if(jQuery("#login-ms-pers")) {
		jQuery("#login-ms-username").focus(function(){
			jQuery(this).attr("value","");
		});
		jQuery("#login-ms-password").focus(function(){
			jQuery(this).attr("value","");
		});
	}

	// hover login button for for browsers that don't understand input:hover in CSS				
 	jQuery("#login .login-button input").each(function() {
 		 jQuery(this).hover(
			function() {
				jQuery(this).css({ backgroundPosition: "right bottom" })
			},
			function() {
				jQuery(this).css({ backgroundPosition: "right top" })
			}
 		 );
 	});
	
	// Show "read more" links if the number of services exceeds 5
	jQuery("#services>li>ul").each(function() {
		if (jQuery("li", this).length > 5) {
			jQuery(this).parent().append('<a class="show-all">Visa alla »</a>');
		}
		// Show service info in tooltip
		jQuery("li>a", this).mouseover(function(){
			var services = jQuery(this).parent();
			jQuery("div.tooltip", services).show();
		})
		.mouseout(function(){
			var services = jQuery(this).parent();
			jQuery("div.tooltip", services).hide();
		})
		.click(function(){
			if(jQuery(this).parent().parent().parent().attr("id") != "quicklinks") {
				return false;
			};
		});
	});
	
	// Slide to show all services
	jQuery("#services a.show-all").click(function(){
		var serviceList = jQuery(this).parents("li");
		jQuery(".show-all", serviceList).hide();
		jQuery(".secondary", serviceList).slideToggle();
		return false;
	});

	// ul#login-tabs items work as tabs for each div directly under div#login-panes
	jQuery("ul#login-tabs").tabs("div#login-panes > div.pane");
		
	// Click on a tab makes the tab "current"
	jQuery("ul#login-tabs a").click(function() {
		var currentTabText = jQuery(this).html();
		jQuery("ul#login-tabs a").each(function() {
			var currentTab = jQuery(this).parent();
			if (currentTabText == jQuery(this).html()) {
				jQuery(currentTab).addClass("selected");
			} else {

				jQuery(currentTab).removeClass("selected");
			}
		});
	});

	// ul#login-tabs items work as tabs for each div directly under div#login-panes
	jQuery("div#intro-tabs > ul").tabs("div#intro-panes > div.pane");

	// Click on a tab makes the tab "current"
	jQuery("div#intro-tabs ul a").click(function() {
		var currentTabText = jQuery(this).html();
		jQuery("div#intro-tabs ul a").each(function() {
			var currentTab = jQuery(this).parent();
			if (currentTabText == jQuery(this).html()) {
				jQuery(currentTab).addClass("selected");
			} else {
				jQuery(currentTab).removeClass("selected");
			}
		});
	});

//		jQuery("jquery selector").scrollable().navigator({ 
//
//		    navi:'div#intro-tabs>ul' 
//		});


			
	// Open movie overlays
	jQuery("a[rel]").overlay({
		absolute: true,
		left: '220px',
		top: '30%'
		// expose: { 
		// 	color: '#333', 
		//  	loadSpeed: 100, 
		//  	opacity: 0.7 
		// }
	});
	
	// Embed and start movie on click
	jQuery(".movie-overlay .wrapper a").click(function() {
		jQuery(this).parent().flashembed({src: jQuery(this).attr('href'), wmode: 'opaque'});
		return false;
	});

});


// Webmail login scripts
//-----------------------------------------------------------------------

// store domain in a cookie.
document.cookie = "thedomain=pne.telia.com; path=/";
var counter = 0;
function login(){
if(document.forms[0].name.value=="suspended"){
goTo('index2.html');
} else {
goTo('metaFrame.html');
}
}

// If the checkbox widget exists to request an ssl connection and
// if it is checked, then true is returned.
function isSSLConnectionRequested() {
if( window.document.loginInfo.ssl ) {
return window.document.loginInfo.ssl[0].checked;
} else {
return false;
}
}

// If the hidded field exists to indicate that an ssl connection is
// required for this domain, then true is returned.
function isSSLConnectionsRequired() {
if( window.document.loginInfo.sslrequired ) {
return true;
} else {
return false;
}
}

function removeUsernameFromUrl(url) {
var username = /(.*)u=[^&]*([&]|$)(.*)/;
var result = url.match(username);
if( result != null ) {
url = result[1] + result[3];
}
return url;
}

function removeTokenFromUrl(url) {
var username = /(.*)t=[^&]*([&]|$)(.*)/;
var result = url.match(username);
if( result != null ) {
url = result[1] + result[3];
}
return url;
}

function removeDomainFromUrl(url) {
var username = /(.*)d=[^&]*([&]|$)(.*)/;
var result = url.match(username);
if( result != null ) {
url = result[1] + result[3];
}
return url;
}

// If javascript is enabled, the form action url will be be overwritten
// to take the user to authentication. Otherwise the default action
// will send them back to this very same form.
//
// The url will make a secure connection if one is requested by the
// user, or the domain requires it.
//
// This function is called by the body tag and the ssl checkbox.
function setAction() {


var protocol = window.location.protocol;
var hostname = window.location.hostname;
var port = window.location.port;
var httpport = "80";
// If the httpport is not set, well use the current port.
if( httpport == "0" ) {
httpport = port;
}
var sslport = "443";
if( isSSLConnectionRequested() || isSSLConnectionsRequired() ) {
// If a secure connection is required or requested, we'll use one.
protocol = "https://"
port = sslport;
} else {
protocol = "http://"
port = httpport;
}



var authenticateUrl = "/cp/ps/Main/login/TISAuthenticate?fp=fail&d=pne.telia.com";




// Remove unecessary query parameters from the url. The username is
// supplied by the login form. The token will be created upon
// a successful login.
authenticateUrl = removeUsernameFromUrl(authenticateUrl);
authenticateUrl = removeTokenFromUrl(authenticateUrl);
authenticateUrl = removeDomainFromUrl(authenticateUrl);
// Enable session validation. This prevents users re-authenticating
// an expired or logged out session using a browser refresh.
authenticateUrl = authenticateUrl + "&v=1259843362367";
// Construct the complete login url.
//var baseUrl = protocol + hostname + ":" + port + authenticateUrl;

// solution for login via Mina Sidor
var baseUrl = "https://webmail.telia.com/cp/ps/Main/login/TISAuthenticate?d=pne.telia.com"


// This block of code commented out cos it's not being used. It
// only makes sense to dynamically modify the locale if we allow
// the user to choose a locale right on the login page. Since
// we've removed that feature, this code is no longer needed.
// It'll stay here until we confirm that we no longer want
// the locale dropdown on the login page.
/*******************************************************************
var args = getArgs();
var localeArgsString = getLocaleArgsString(args);
if( localeArgsString.length > 0 ) {
if (baseUrl.indexOf("?") > 0) {
localeArgsString = "&" + localeArgsString;
} else {
localeArgsString = "?" + localeArgsString;
}
}
var url = baseUrl + localeArgsString;
*********************************************************************/
var url = baseUrl;
window.document.loginInfo.action = url;
// Put the focus on the username field only when the user first logs in.
if( counter==0 )
{
counter=1;
//document.loginInfo.u.focus();
//document.loginInfo.u.select();
}
}
// This function calls a popup window containing the signup wizard.
// Most importantly it attaches localization information to the url.
function getWizard() {
var url = "SignUpWizard";
var args = getArgs();
var localeArgsString = getLocaleArgsString(args);
if( localeArgsString.length > 0 ) {
localeArgsString = "?" + localeArgsString;
}
url += localeArgsString;
popWin(url,'','500','400');
}
// This functions splits the url argument list apart and places it
// into an array.
function getArgs() {
var args = new Object();
args.name = new Array();
args.value = new Array();
var query = window.location.search.substring(1);
var pairs = query.split("&");
for( var i = 0; i < pairs.length; i++ ) {
var pos = pairs[i].indexOf("=");
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
args.name[i] = argname;
var value = pairs[i].substring(pos + 1);
args.value[i] = value;
args[argname] = value;
}
return args;
}
function getLocaleArgsString(args) {
var localeArgsString = "";
for( var i = 0; i < args.name.length; i++ ) {
if( args.name[i] == "l" ) {
localeArgsString += args.name[i] + "=" + args.value[i] + "&";
}
}
// Strip off trailing &
localeArgsString = localeArgsString.substring(0,localeArgsString.length - 1);
return localeArgsString;
}
// This function is called when a user chooses display the login page
// in a different langauge. This function modifies the locale query
// parameters of the form action url and submits the form to redraw
// the login form page.
function changeLocale() {
// Obtain value from language selection box.
var localeArray = window.document.loginInfo.localeString.options;
var localeIndex = window.document.loginInfo.localeString.selectedIndex;
// Locale in the format "language_COUNTRY". e.g. en_US, en_GB
var l = localeArray[localeIndex].value;
// construct url up to paramters
// var hostname = window.location.hostname;
// var port = window.location.port;
var url = "Login?";
// split up url parameters
var args = getArgs();
// blank out existing l parameter.
for( var i = 0; i < args.name.length; i++ ) {
if( args.name[i] == "l" ) {
args.value[i] = "";
args.name[i] = "";
}
//alert(args.name[i] + " = " + args.value[i]);
}
// url length before
var ulb = url.length;
// add parameters to url. Don't add blank parameters.
for( var i = 0; i < args.name.length; i++ ) {
if( args.name[i] != "" && args.name[i] != null) {
url += args.name[i] + "=" + args.value[i] + "&";
}
}
// Strip off trailing & if one was added.
if( url.length > ulb ) {
url = url.substring(0,url.length - 1);
}
// add l, c and v parameters
if( l ) {
url += "&l=" + l;
}
// place the url into the from action attribute.
window.document.loginInfo.action = url;
// perform the submit
window.document.loginInfo.submit();
}
function replace(str, oldstr, newstr)
{
if(!str)
return '';
var strLength = str.length;
if (strLength == 0) return '';
while(1)
{
var i = str.indexOf(oldstr);
if ((!i) && (oldstr != str.substring(0,oldstr.length)))
break;
if (i == -1)
break;
str = str.substring(0,i) + newstr + str.substring(i+oldstr.length,strLength);
}
return str;
}
var psSafeStr= '__RMS_AT_REP__';
//This function will replace any '@' with '+'
function makeLoginIdPSSafe()
{
var u= window.document.loginInfo.uVisible.value;
window.document.loginInfo.u.value= replace(u, '@', psSafeStr);
}
//This function will replace any '+' with '@'
function restorePSSafeLoginId()
{
var u= window.document.loginInfo.uVisible.value;
u= replace(u, psSafeStr, '@')
u= replace(u, psSafeStr.toLowerCase(), '@');
window.document.loginInfo.uVisible.value= u;
}

function doLoad()
{
var rdos = document.getElementsByName("rdoLoginType");
for(var i = 0; i < rdos.length; i++)
{
if(rdos[i].checked)
{
checkRdo(rdos[i]);
}
}
}

function checkRdo(obj)
{
var selectedValue = obj.value;
var usrLabel = document.getElementById("usrLabel");
var usrField = document.getElementById("usrField");
var txtUserName = 'Användarnamn eller e-postadress:';
var txtMobile = 'Mobilnummer:<br><span class="helpTxt">Exempel: 4035551234</span>';
if(selectedValue == 'mobileNum')
{
usrLabel.innerHTML=txtMobile;
usrField.name="mobileNumber";
usrField.value="";
}
else
{
usrLabel.innerHTML=txtUserName;
usrField.name="u";
usrField.value="";
}
setAction();
}



