//----------------------------------------------------------------------------------Global variables
var legalPage	 ="legal_entry_page.html";
var legalVersion ="080102";
var thisPage	 = document.URL;
var welcomePage  = "welcome.asp";
var expiration 	 = new Date();
var alpha		 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var digit		 = "0123456789"
var aryUrl		 = document.URL.split("?");
var secured;
var unsecured;
var win;

var SPEntryPage  = "structured-products-entry-page.html";
var aryStructureProductsPages = new Array(8) 
aryStructureProductsPages[0]="alternative-assets.html"; 
aryStructureProductsPages[1]="structured-notes.html"; 
aryStructureProductsPages[2]="structured-product-solutions.html"; 
aryStructureProductsPages[3]="structured-product-suitability.html"; 
aryStructureProductsPages[4]="structured-products-announcement.html"; 
aryStructureProductsPages[5]="structured-products.html"; 
aryStructureProductsPages[6]="tailored-structured-solutions.html"; 
aryStructureProductsPages[7]="what-are-structured-products.html"; 

expiration.setTime(expiration.getTime() + 94608000000);

var strUrl = aryUrl[0];

strUrl = strUrl.substring(0,strUrl.lastIndexOf("/")+1);
strUrl = strUrl.substring(strUrl.indexOf("/")-1, strUrl.length);

//secured = "https://www.rbccees.com/";
secured = "https" + strUrl; 

//unsecured = "http://www.rbccees.com/";

unsecured = "http" + strUrl;

//------------------------------------------------------------------
// Enhance the String object with new trim(), ltrim(),
// and rtrim() methods, as in VB
//------------------------------------------------------------------
String.prototype.trim = function()
{ //Trim, as in VB
  return this.replace(/(^\s+)|(\s+$)/g, '');
}

String.prototype.ltrim = function()
{ //LTrim, as in VB
  return this.replace(/^\s+/g, '');
}

String.prototype.rtrim = function()
{ //RTrim, as in VB
  return this.replace(/\s+$/g, '');
}

//--- Change contact (called from contactus pages)
function cmdChange() {
	openPopupWindow("contactus_change.asp","Change",750,450,1);
}

//--- Select prodReq013 (called from contactus pages)
function cmdAdvisory() {
	if (document.frmContact.prodReq013.checked) {
		openPopupWindow("contactusAdvisorLoc.asp","Advisor",500,600,1);
	} else {
		document.frmContact.AdvisorMsg.value = "";
	}
}
function y2k(number) {return (number < 1000) ? number + 1900 : number;}

function isDate(year,month,day){
	month = month -1
	var test = new Date(year,month,day);
	if ((y2k(test.getYear()) == year ) && (month == test.getMonth()) && (day == test.getDate()))
		return true;	
	else
		return false;
}

//--- form validation (called from contactus pages)
function validation() {
	
	textareaMaxlength = 250;
	
	if (document.frmContact.Title.value == "- not selected -") {
		alert("Please select your title.");
		document.frmContact.Title.focus();
		return false;
	}
	if (document.frmContact.FirstName.value.length == 0) {
		alert("Please enter your first name.");
		document.frmContact.FirstName.focus();
		return false;
	}
	if (document.frmContact.LastName.value.length == 0) {
		alert("Please enter your last name.");
		document.frmContact.LastName.focus();
		return false;
	}
	
	if (((document.frmContact.MonthOB.value == "")||(document.frmContact.DayOB.value == "")||(document.frmContact.YearOB.value == ""))&&((document.frmContact.MonthOB.value != "")||(document.frmContact.DayOB.value != "")||(document.frmContact.YearOB.value != ""))) {
		alert("Please provide a valid Date of Birth.");
		document.frmContact.MonthOB.focus();
		return false;
	}
	
	if ((!isDate(document.frmContact.YearOB.value,document.frmContact.MonthOB.value,document.frmContact.DayOB.value))&&((document.frmContact.MonthOB.value != ""))) {
		alert ("Date of Birth is invalid");
		document.frmContact.MonthOB.focus();
		return false;
	}

	if (document.frmContact.Email.value.length == 0) {
		alert("Please enter your Email address.");
		document.frmContact.Email.focus();
		return false;
	} else {
		if (!check_email(document.frmContact.Email)) {
			document.frmContact.Email.focus();
			return false; 	
		}
	}
	if (document.frmContact.Country.value.length == 0) {
		alert("Please select country.");
		document.frmContact.Country.focus();
		return false;
	}
	if (document.frmContact.Nationality.value == 0) {
		alert("Please select your nationality.");
		document.frmContact.Nationality.focus();
		return false;
	}
	if ((!document.frmContact.cvemail.checked) && (!document.frmContact.cvphone.checked) && (!document.frmContact.cvfax.checked) && (!document.frmContact.cvregmail.checked)) {
			alert("Please pick at least one contact method.");
			document.frmContact.cvemail.focus();
			return false;
	}
	if(document.frmContact.message.value.length > textareaMaxlength) {
          alert('Your message must be 250 characters or less');
          document.frmContact.message.focus();
          return false;
    }	
	return true;	
}

//----------------------------------------------------------------------
//--- Heading functions replaced by include files March 29, 2004 - nr

//--- Check if the passing parameter is an alpha or not. Return true if yes, otherwise return false.
function is_alpha(parma) {
   if (alpha.indexOf(parma) == -1) {
      return false;
   } else {
      return true;
   }
}       

//--- Check if the passing parameter is a digit or not. Return true if yes, otherwise return false.
function is_digit(parmn) {
   if (digit.indexOf(parmn) == -1) {
      return false;
   } else {
      return true;
   }
}       

//-------------------------------------------------------------------
function openPopupWindow( pageToLoad, winName, width, height, center) {
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
        xposition = (screen.width - width) / 2;
        yposition = (screen.height - height) / 2;
    }
    args = "width=" + width + "," + "height=" + height + "," 
	+ "location=0,"   + "menubar=0," + "resizable=1," 
	+ "scrollbars=1," + "status=0,"  + "titlebar=0,"   
	+ "toolbar=0,"   + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only
	win = open( pageToLoad,winName,args );
}

//--- Get cookie value
function getCookieVal(offset) {  
	var endstr = document.cookie.indexOf (";", offset); 
	 
	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

//--- Get cookie
function getCookie(name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;
	
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

//--- get cookie with key
function getKeyCookie(name,key) {
	var strCookie = getCookie(name) + "&";
	var i = strCookie.indexOf(key, i) + key.length + 1;
	var j = strCookie.indexOf("&", i);

	if ((i == -1) || (j-i <= 0)) {
		return null;
	} else {	
		return strCookie.substr(i, j-i);
	}
}

//--- Set cookie
function setCookie(name, value, expires, path, domain, secure) {  // Set Cookie
  path = "/";
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

//--- delete cookie
function deleteCookie(name) {  
	var exp = new Date();  
	var cval = getCookie(name);
	
	exp.setTime (exp.getTime() - 1);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

//--- Popup legal page if legal version changed and the current page is not the legal page.
function showLegalPage() {

	if ((win==null)&& (getCookie("ckLegalPageVersion") != legalVersion)) {
	//if (thisPage.lastIndexOf("/")!=-1) {
	//	thisPage=thisPage.substr(thisPage.lastIndexOf("/")+1);
	//}
	// check if the legal version changed and the current page is not the legal page.
	//if (thisPage!=legalPage) {
		openPopupWindow(legalPage,'legal',550,450,1);
	//}
	}
	else 
	{
		showStructuredProductsEntryPage();
	}
}


//--- Show welcome page
function showWelcomePage(){

if ((getCookie("ckDspWelcomeBackPage") == null && getCookie("ckMktgID") != "0" && getCookie("ckMktgID") != null ) ||
    (getCookie("ckDspWelcomeBackPage") != null && getCookie("ckEmpID")  != null ) ||
    (getCookie("ckDspWelcomeBackPage") != null && getCookie("ASSOC")    != null )) 
{

		if (thisPage.lastIndexOf("/")!=-1) { 
			thisPage=thisPage.substr(thisPage.lastIndexOf("/")+1); 
		}
		if (thisPage!=welcomePage) { 
			openPopupWindow("welcome.asp","Welcome",650,480,1); 
		}
	} 	 
}

//--- Show Structured Products Entry Page
function showStructuredProductsEntryPage()
{
	if (getCookie("ckStructuredProducts") == null) 
	{
		if (thisPage.lastIndexOf("/")!=-1) { 
			thisPage=thisPage.substr(thisPage.lastIndexOf("/")+1); 
		}
		if (thisPage!=SPEntryPage) 
		{ 
			for (i = 0 ; i < aryStructureProductsPages.length; i++)
			{			
				if (aryStructureProductsPages[i] == thisPage)
				{
					openPopupWindow(SPEntryPage,'legal',550,450,1);
				}
			}
		}
	}
}
//---------------------------------------------------------------------
// validate the email address. It must contain an @ symbol and a period (.)
// procedures:
// 1. trim the value of the passing object - em.
// 2. declare variables :
//    emadr = the upper case value of em
// 3. Check for the following errors:
//      - user enter email address
//		- '@' symbol not included in the email address
//		- email address begin with a digit.
//		- email address begin with '@' symbol
//		- email address end with '@' symbol
//		- email address contains more than one '@' symbol
//		- '.' not included in the email address
//      - '.' cannot follow right after '@' symbol
//      - email address end with '.'
//      - email address not end with .com or .net or .ca or .org and
//---------------------------------------------------------------------
function check_email(em) {
   em.value = em.value.trim();
   var emadr = em.value.toUpperCase();

   var idx   = 0;     
   // check if @ exist
   if (emadr.length == 0) {
   	 alert("Please enter email address !");
   	 return false;
   } else if (emadr.indexOf("@") == -1) {
         alert("Invalid email address. Should include '@' symbol in your email address.");
         return false;
   } else if (is_digit(emadr.charAt(0))) {
         alert("Invalid email address. Email address never start with a digit.");      
         return false;
   } else if (emadr.charAt(0) == '@') {
         alert("Invalid email address. Should not start with '@' symbol.");      
         return false;
   } else if (emadr.charAt(emadr.length-1) == '@') {
         alert("Invalid email address. Should not end with '@' symbol.");      
         return false;         
   } else {  
       
   // split em into two part 
      var email_array = emadr.split("@");
      
      if (email_array[2] != null) {
          alert("Invalid email address. Should contains only one '@' symbol.");
          return false;
      } else {
     
          // check email_array[1] part...
          var em2 = email_array[1];
          if (em2.indexOf(".") == -1) {
               alert("Missing '.' at the second part of the emaill address."); 
               return false;
          } else if (em2.charAt(0) == ".") {
               alert("Invalid email address! '.' can't placed right after '@' symbol");
               return false;
          } else if (em2.charAt(em2.length-1) == ".") {
               alert("Email address cannot end with '.'");
               return false;
          } 
      }
   }
   return true;
}

/*var urlRedirect;

if (getCookie("ckFirstPage") == null) {

	//--- Set referer page cookies
	setCookie("ckReferer", document.referrer);
	if (getCookie("ckReferer1st") == null) {
		setCookie("ckReferer1st", getCookie("ckReferer"), expiration  ); 
	}
	
	//--- If Old Visitor Cookie exists, goto convert old visitor page.

	if ((getKeyCookie("RBCPBANKING","IDNO") != null) || (getCookie("ASSOC") != null) ||
	    (getCookie("ADVISOR")               != null) || (getCookie("LOC")   != null)) { 
	    urlRedirect = "ConvertOldVisitor.asp?url=" + document.URL;
	}

	//--- If known count cookie does not exist, create known count cookie and set it to 0.
	if (getCookie("ckNumVisits") == null) {
		setCookie("ckNumVisits", "0", expiration );
	}
	
	//--- If unknown count cookie does not exist, create unknown count cookie and set it to 0.
	if (getCookie("ckNumUnknownVisits") == null) {
		setCookie("ckNumUnknownVisits", "0", expiration );
	} 
	
	//--- Set known & unknown visitor count
	if (getCookie("ckVisitorID") != null) {
		if (getCookie("ckKnownCnt") == null) {
			intKnownCnt = parseInt(getCookie("ckNumVisits")) + 1;
			setCookie("ckNumVisits", intKnownCnt.toString(), expiration);
			setCookie("ckKnownCnt","done");
		}
	} else {
		if (getCookie("ckKnownCnt") == null) {
			intUnknownCnt = parseInt(getCookie("ckNumUnknownVisits")) + 1;
			setCookie("ckNumUnknownVisits", intUnknownCnt.toString(), expiration);		
			setCookie("ckKnownCnt","done");	
		}
	}
	
	if(urlRedirect == null) {
		setCookie("ckFirstPage","done");
		showWelcomePage();
	}

}*/

//*****************************************************************************************************************
/*if (getCookie("ckFirstPage")=="done") {
	showLegalPage();
}
else if (getCookie("ckStructuredProducts") == null)
{
	showStructuredProductsEntryPage();
}
else {
	location.href = urlRedirect;
}*/
//*****************************************************************************************************************

//-----------Function to build page footer --added by NR 15 May 2003--
function footer() {
	document.writeln("<hr size='1' width='770' align='left' noshade />");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='ftrtextlge'>");
			document.writeln("&nbsp;&nbsp;rbccees.com is operated by RBC.");
			document.writeln("</td>");
			document.writeln("<td align='right' class='ftrtext'>");
			document.writeln("<a href='cees-privacy.html' class='ftrtextlink'>");
			document.writeln("Privacy</a>");
			document.writeln("&nbsp;|&nbsp;<a href='cees-legal.html' class='ftrtextlink'>Legal Terms of Use</a>");
			//document.writeln("Legal Terms of Use</a>");
			document.writeln("&nbsp;|&nbsp;<a href='http://www.rbc.com/security/index.html' target='_blank' class='ftrtextlink'>Security</a>&nbsp;&nbsp;");
			//document.writeln("Security</a>&nbsp;&nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='ftrtext'>");
			document.writeln("&nbsp;&nbsp;&copy;&nbsp;RBC, 2007");
			document.writeln("</td>");
			document.writeln("<td align='right' class='ftrtext'>");
			document.writeln("Last modified: 28/02/2007 &nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
	document.writeln("<hr size='1' width='770' align='left' noshade />");
}
//-----------Function to build page footer for secure template ---------------------------

function footerSecure() {
	document.writeln("<hr size='1' width='770' align='left' noshade />");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='ftrtextlge'>");
			document.writeln("&nbsp;&nbsp;rbcprivatebanking.com is operated by Royal Bank of Canada.");
			document.writeln("</td>");
			document.writeln("<td align='right' class='ftrtext'>");
			document.writeln('<a href="' + unsecured + 'cees-privacy.html" class="ftrtextlink">');
			document.writeln("Privacy</a>");
			document.writeln('&nbsp;|&nbsp;<a href="' + unsecured + 'cees-legal.html" class="textlink2">Legal Terms of Use</a>');
			//document.writeln("Legal Terms of Use</a>");
			document.writeln("&nbsp;|&nbsp;<a href='http://www.rbc.com/security/index.html' target='_blank' class='ftrtextlink'>Security</a>&nbsp;&nbsp;");
			//document.writeln("Security</a>&nbsp;&nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='ftrtext'>");
			document.writeln("&nbsp;&nbsp;&copy;&nbsp;RBC, 2007");
			document.writeln("</td>");
			document.writeln("<td align='right' class='ftrtext'>");
			document.writeln("Last modified: 28/02/2008 &nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
	document.writeln("<hr size='1' width='770' align='left' noshade />");
}

function footerGmg() {
	document.writeln("<hr size='1' width='770' align='left' noshade />");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='footerlge'>");
			document.writeln("&nbsp;&nbsp;rbccees.com is operated by RBC.");
			document.writeln("</td>");
			document.writeln("<td align='right' class='footer'>");
			document.writeln("<a href='../cees-privacy.html' class='textlink2'>");
			document.writeln("Privacy</a>");
			document.writeln("&nbsp;|&nbsp;<a href='../cees-legal.html' class='textlink2'>Legal Terms of Use</a>");
			//document.writeln("Legal Terms of Use</a>");
			document.writeln("&nbsp;|&nbsp;<a href='http://www.rbc.com/security/index.html' target='_blank' class='textlink2'>Security</a>&nbsp;&nbsp;");
			//document.writeln("Security</a>&nbsp;&nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
		document.writeln("<table width='770' cellpadding='0' border='0'>");
			document.writeln("<tr>");
			document.writeln("<td align='left' class='footer'>");
			document.writeln("&nbsp;&nbsp;&copy;&nbsp;RBC, 2007");
			document.writeln("</td>");
			document.writeln("<td align='right' class='footer'>");
			document.writeln("Last modified: 28/02/2008 &nbsp;");
			document.writeln("</td>");
			document.writeln("</tr>");
		document.writeln("</table>");
	document.writeln("<hr size='1' width='770' align='left' noshade />");
}

// go function provided by RBC
// March, 2004

function go() {
	var curOption = document.theForm.region.options[document.theForm.region.selectedIndex];
	if (curOption.value) {
		if (curOption.value != "default") {
			window.location.href = curOption.value;
		}
	}
}

//added during OCE redesign - nr
//---Populate year selection upto 100 years from the current year
function fillyearselection(){
	var optionName
	var length
	var now = new Date();
	var CurrentYear
	
	CurrentYear = now.getYear();
	if (CurrentYear<1900) {CurrentYear+=1900};
	
	for (var i = (CurrentYear-100); i<CurrentYear;i++) {
		optionName = new Option (i,i)
		length = document.frmContact.YearOB.length;
		document.frmContact.YearOB.options[length] = optionName;
	}

		optionName = new Option (CurrentYear,CurrentYear)
		length = document.frmContact.YearOB.length;
		document.frmContact.YearOB.options[length] = optionName;

}

//added during OCE redesign - nr
//--- open window function for online banking demo

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//added during OCE redesign - nr
//--- open window for security notice

function openwindow()
{
window.open("https://www.rbcroyalbank.com/onlinebanking/bankingusertips/notices/notice05.html","RTB",'width=500,height=400,left=0,top=0,statusbar=yes,scrollbars=yes');
}

//added to handle maxlength of Textarea on 2007/02/02 - By
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}


function securedUrl(pageName)
    {
        strUrl = document.location.href
        strUrl = strUrl.substring(0,strUrl.lastIndexOf("/")+1);
        strUrl = strUrl.substring(strUrl.indexOf("/")-1, strUrl.length);
        return 'https' + strUrl + pageName;
    }
    
    function unSecuredUrl(pageName)
    {
        strUrl = document.location.href
        strUrl = strUrl.substring(0,strUrl.lastIndexOf("/")+1);
        strUrl = strUrl.substring(strUrl.indexOf("/")-1, strUrl.length);
        return 'http' + strUrl + pageName;
    }






