/****************************************************
	Initialize page-specific variables
****************************************************/

if (is_in_player && browser_name != "Boxster Mac Media Browser") {
	var plyrObj = top.parent.window.external;
} else if (browser_name != "Boxster Mac Media Browser"){
	document.write("<object id=\"IERPCtl\" width=0 height=0 classid=\"CLSID:FDC7A535-4070-4B92-A0EA-D9994BCC0DC5\"></object>");
	var plyrObj = document.getElementById("IERPCtl");
}
var is_windows = navigator.appVersion.indexOf('Windows') != -1;
var is_msie = navigator.appVersion.indexOf('MSIE') != -1;

/****************************************************
	Sign In/Sign out functionality
	Read the cookie, figure out if the user is signed in or signed out
	and display the correct link.
****************************************************/

function showSignOut( status ) {
	/* Do not display signin/signout links for unsupported browsers/players */

	var pBrowserCookie = getUserCookieValue("RNPlayer");
	var signout = "http://" + guideURL + "/signin.rnx?signout=true";
	var signin = "http://" + guideURL + "/signin.rnx?signin=true";

	if ( !is_in_player && pBrowserCookie != null) {
		var pCookie = pBrowserCookie;
	} else if (plyrObj != null){
		var pCookie = plyrObj.GetPlayerCookie("RNPlayer");
	} else{
		var pCookie;
	}

	/* change the links for safari signin/signout links */
	if ( show_safari_signin ) {
		signout = "javascript:openPopup('http://" + guideURL  + "/?page=safari-signout','Help','467','300');";
		signout += "spTracking.sendLinkEvent( { eVar17: 'Logout', prop18: 'E:Logout' }, { type: 'custom', linkName: 'Navigation', events: 'event5' } );";
		signin = "javascript:openPopup('http://" + guideURL  + "/?page=safari','Help','467','300');";
		signin += "spTracking.sendLinkEvent( { eVar17: 'Login', prop18: 'E:Login' }, { type: 'custom', linkName: 'Navigation', events: 'event4' } );";
	}
	if ( status && browser_name == "Boxster Mac Media Browser") {
		document.getElementById("signOutLink").innerHTML = "&nbsp;|&nbsp;&nbsp;<a href=\"" + signout + "\" class=\"highlight\">Sign Out</a>";
		return;
	} else if ( status && pCookie.length > 0 && pCookie != "NODATA" && browser_name != "Boxster Mac Media Browser") {
		document.getElementById("signOutLink").innerHTML = "&nbsp;|&nbsp;&nbsp;<a href=\"" + signout + "\" class=\"highlight\">Sign Out</a>";
		return;
	} else {
		document.getElementById("signOutLink").innerHTML = "&nbsp;|&nbsp;&nbsp;<a href=\"" + signin + "\" class=\"highlight\">Sign In</a>";
		return;
	}
}

function showSignOut2 ( status, divider ) {
	/* Do not display signin/signout links for unsupported browsers/players */
	if ( !show_uds ) {
		return;
	}
	var pBrowserCookie = getUserCookieValue("RNPlayer");
	var signout  = "http://" + guideURL + "/signin.rnx?signout=true";
	var signin = "http://" + guideURL + "/signin.rnx?signin=true";

	if ( !is_in_player && pBrowserCookie != null) {
		var pCookie = pBrowserCookie;
	} else if (plyrObj != null){
		var pCookie = plyrObj.GetPlayerCookie("RNPlayer");
	} else{
		var pCookie;
	}

  /* change the links for safari signin/signout links */
	if ( show_safari_signin ) {
		signout = "javascript:openPopup('http://" + guideURL  + "/?page=safari-signout','Help','467','300');";
		signout += "spTracking.sendLinkEvent( { eVar17: 'Logout', prop18: 'E:Logout' }, { type: 'custom', linkName: 'Navigation', events: 'event5' } );";
		signin = "javascript:openPopup('http://" + guideURL  + "/?page=safari','Help','467','300');";
		signin += "spTracking.sendLinkEvent( { eVar17: 'Login', prop18: 'E:Login' }, { type: 'custom', linkName: 'Navigation', events: 'event4' } );";
	}
	if ( status && browser_name == "Boxster Mac Media Browser") {
		document.getElementById("signOutLink").innerHTML = ((divider)?"&nbsp;|&nbsp;&nbsp;":"")+"<a href=\"" + signout + "\" class=\"highlight\">Sign Out</a>";
		return;
	} else if ( status && pCookie.length > 0 && pCookie != "NODATA" && browser_name != "Boxster Mac Media Browser") {
		document.getElementById("signOutLink").innerHTML = ((divider)?"&nbsp;|&nbsp;&nbsp;":"")+"<a href=\"" + signout + "\" class=\"highlight\">Sign Out</a>";
		return;
	} else {
		document.getElementById("signOutLink").innerHTML = ((divider)?"&nbsp;|&nbsp;&nbsp;":"")+"<a href=\"" + signin + "\" class=\"highlight\">Sign In</a>";
		return;
	}
}

/* Gets the value of the cookie given the cookie's name */
function getUserCookieValue( cookieName ) {
	var cookieArray = new Array();
	var cRE = new RegExp("(\;|^)[^;]*("+cookieName+")\=([^;]*)(;|$)");
	cookieArray = cRE.exec(document.cookie);
	if( cookieArray != null ) {
		return cookieArray[3];
	}
	return "";
}

/****************************************************
	Page Reload Functionality
****************************************************/

function RPOnLogin(){
	location.href = "http://superpass.real.com/?target=_rpsuperpass";
}

/****************************************************
	Reauth Functionality
****************************************************/

function doReauth(){
    if(plyrObj)
    {
	if( is_in_player && plyrObj.PlayerProperty("ReauthThisSession") != "true" ) {
	 	var pCookie = plyrObj.GetPlayerCookie("RNPlayer");
	 	var fCookie = plyrObj.GetPlayerCookie("RNFeatures");
	 	if( pCookie.length > 0 && pCookie != "NODATA" && fCookie.length > 0 && fCookie != "NODATA" ) {
	 		// User has player cookies, try to re-authenticate
	 		document.getElementById("reauth_loader").src= "http://" + guideURL + "/?page=reauth" + "&pck=" + escape(pCookie) + "&feat=" + escape(fCookie) + "&randid="+(new Date()).valueOf();
	 	} else {
			plyrObj.PlayerProperty("reauth_test") = "complete";
		}
	}
    }
}

/****************************************************
	Message Center Functionality
****************************************************/

// Opens the correct player window for either viewing messages or personalizing messages.
function OpenMessageCenter(WhatMsgArea) {
	if (WhatMsgArea == "NewMessage") {
		result = parent.window.external.HandleAction("command:MessageCenter(RNMsgView)");
	}
}

// Gets the number of unviewed messages and displays that number
function getMessageCenter( customizeURL, imageURL ) {
	if( !is_in_player || isViper() ) {
		return;
	}

	var UnviewedMsgCount = 0;
	var strMessage = "";
	var strImage = "";

	try {
		UnviewedMsgCount = parseInt(plyrObj.PlayerProperty('UNVIEWEDMESSAGESCOUNT'));
	} catch(e) {}

	if ( UnviewedMsgCount == 0 ) {
		strImage = "<span style='vertical-align: top;'><img src='"+imageURL+"' width='16' height='16' border='0' alt='View Messages'></span>";
		strMessage = "&nbsp;&nbsp;";
	} else {
		strImage = "<a href='javascript:OpenMessageCenter(\"NewMessage\");' ><span style='vertical-align: top;'><img src='"+imageURL+"' width='16' height='16' border='0' alt='View Messages'></span></a>";
		strMessage = "&nbsp;&nbsp;<a href='javascript:OpenMessageCenter(\"NewMessage\");' >" + UnviewedMsgCount + " message" + ((UnviewedMsgCount == 1)?"":"s") + "</a> : ";
	}

	strMessage += "<a href='"+customizeURL+"'>Alerts</a>";

	document.write(strImage + strMessage);
}

// Gets the number of unviewed messages and displays that number
function getMessageCenter2( customizeURL, imageURL, showImage, divider, messageText ) {
	if( !is_in_player || isViper() ) {
		return;
	}

	var UnviewedMsgCount = 0;
	var strMessage = "";
	var strImage = "";

	try {
		UnviewedMsgCount = parseInt(plyrObj.PlayerProperty('UNVIEWEDMESSAGESCOUNT'));
	} catch(e) {}

	if ( UnviewedMsgCount == 0 ) {
		strImage = "<img src='"+imageURL+"' width='16' height='16' border='0' alt='View Messages' align='top'>";
		strMessage = "&nbsp;&nbsp;<a href='"+customizeURL+"' class='bold'>"+messageText+"</a>";
	} else {
		strImage = "<a href='javascript:OpenMessageCenter(\"NewMessage\");' class='bold'><img src='"+imageURL+"' width='16' height='16' border='0' alt='View Messages' align='top'></a>";
		strMessage = "<a href='javascript:OpenMessageCenter(\"NewMessage\");' class='bold'>" + UnviewedMsgCount + " message" + ((UnviewedMsgCount == 1)?"":"s") + "</a>";
	}

	if (divider) {
		strMessage += "&nbsp;&nbsp;|&nbsp;"
	}

	if (showImage) {
		document.write(strImage + strMessage);
	} else {
		document.write(strMessage);
	}
}

/****************************************************
	Player and Toolbar Download Functionality
****************************************************/

function showDownloadPlayer( tab, url, imageURL ){
	if ( has_player != "true" ) {
		document.write("<a href=\"javascript:navigateToTab('"+tab+"','"+url+"');\" ><img src='"+imageURL+"' alt='' width='13' height='13' border='0' align='absmiddle'></a>&nbsp;&nbsp; <a href=\"javascript:navigateToTab('"+tab+"','"+url+"');\" class='download' >Download RealPlayer</a> &nbsp;&nbsp;|&nbsp;&nbsp;");
	}
}
function showDownloadToolbar( tab, url, imageURL ){
	if (!isToolbarInstalled() && is_windows && is_msie) {
		document.write("<a href=\"javascript:navigateToTab('"+tab+"','"+url+"');\" ><img src='"+imageURL+"' alt='' width='13' height='13' border='0' align='absmiddle'></a>&nbsp;&nbsp; <a href=\"javascript:navigateToTab('"+tab+"','"+url+"');\" class='download' >Download RealToolbar</a> &nbsp;&nbsp;|&nbsp;&nbsp;");
	}
}

function isToolbarInstalled(){
	r1barActiveXname = "BarControl.TBarControl";
	var r1bar = null;
	try {
	    r1bar = new ActiveXObject(r1barActiveXname);
	} catch(e){
	    // failed
	}
	if( r1bar != null && r1bar.isToolbarRegistered != 0){ //is toolbar installed and registered.
	    return true;
	}
	return false;
}

/****************************************************
	EMAIL Validation for Friends page
****************************************************/

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail")
		    return false
		 }

 		 return true
	}

function ValidateSenderName(nameID){

	if ((nameID.value==null)||(nameID.value=="")){
		alert("Please Enter your Name")
		nameID.focus()
		return false
	}
  return true
}

function ValidateSenderEmail(emailID){

	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
    alert("Please Enter a valid Email");
    emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
 }

function ValidateFriends(emailID){
  if ((emailID.value==null)||(emailID.value=="")){
    return true;
  }
	if (echeck(emailID.value)==false){

    emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
 }
function ValidateFriendsNotEmpty(){
  if((document.getElementById('toemail1').value.length > 0) ||
      (document.getElementById('toemail2').value.length > 0) ||
      (document.getElementById('toemail3').value.length > 0) ||
      (document.getElementById('toemail4').value.length > 0) ||
      (document.getElementById('toemail5').value.length > 0)){
   return true;
  }
  alert("Please enter at least one Email for your friends.");
  document.getElementById('toemail1').focus();
  return false;
}

function ValidateForm(){
	var elem = document.getElementById('frmMain').elements;
  var valid = false;
  var validname = false;
  var validemail = false;
  var validfriend1 = false;
  var validfriend2 = false;
  var validfriend3 = false;
  var validfriend4 = false;
  var validfriend5 = false;

  for(var i = 0; i < elem.length; i++)
  {
    if (elem[i] == document.getElementById('fromname')){
      validname = ValidateSenderName(elem[i]);
      if(!validname) break;
    }
    if (elem[i] == document.getElementById('fromemail')){
      validemail = ValidateSenderEmail(elem[i]);
      if(!validemail) break;
    }
    if (elem[i] == document.getElementById('toemail1')){
      validfriend1 = ValidateFriends(elem[i]);
      if(!validfriend1) break;
    }
    if (elem[i] == document.getElementById('toemail2')){
      validfriend2 = ValidateFriends(elem[i]);
      if(!validfriend2) break;
    }
    if (elem[i] == document.getElementById('toemail3')){
      validfriend3 = ValidateFriends(elem[i]);
      if(!validfriend3) break;
    }
    if (elem[i] == document.getElementById('toemail4')){
      validfriend4 = ValidateFriends(elem[i]);
      if(!validfriend4) break;
    }
    if (elem[i] == document.getElementById('toemail5')){
      validfriend5 = ValidateFriends(elem[i]);
      if(!validfriend5) break;
    }
  }

  if (validname && validemail && validfriend1 && validfriend2 && validfriend3 && validfriend4 && validfriend5){
    if(ValidateFriendsNotEmpty()){
      document.getElementById('submitval').value = true;
      return true;
    }
    else{
    document.getElementById('submitval').value = false;
    return false;
    }
  }
  else{
    document.getElementById('submitval').value = false;
    return false;
  }
  return false;
}