/* ----------------------------------------------
	common.js - included on all templates	
---------------------------------------------- */

// Gobal Variables
var strTopTab = "";
var host = document.location.host.toLowerCase();
var strUrl = document.location.href.toLowerCase();

// Browser Types
var ns = (!document.all && document.getElementById);
var ie = (document.all && document.getElementById);
var isNetscape6 = false;
if (navigator.userAgent.indexOf('Netscape6') != -1) isNetscape6 = true ;

function isNet4x() {
	if (document.getElementById)
		return false;
	return true;
}

// Site Type
function getSiteType() {

	if (host.indexOf('pacificlifeandannuity') != -1) return "PANDA";
	else if (host.indexOf('mutualfunds') != -1) return "MF";
	else if (host.indexOf('collegesavings') != -1) return "CS";
	return "ANN";
}

function fncGetServer(){
	if (host.indexOf("modeloffice") != -1) return "modeloffice.";
	else if(host.indexOf("test") != -1) return "test.";
	return "www.";
}

function fncGetSiteClass(){

	if(host.indexOf("myaccount") != -1) return "myaccount";
	else if(host.indexOf("mymodeloffice") != -1) return "myaccount"; // Panda my account modeloffice
	else if(strUrl.indexOf("http:") != -1) return "corporate";
	else return "broker";
}


function getCompanyName() {
	var strCoName = "";

	if (getSiteType() == "PANDA") {
		strCoName = "Pacific Life and Annuity";
	} else if (getSiteType() == "MF" || getSiteType() == "CS") {
		strCoName = "Pacific Life Funds";
	} else {
		strCoName = "Pacific Life Insurance Company";	
	}
	
	return strCoName;
}

// Creates fully qualified url (used in corp submenus from login page)
function getFullURL(fullURL, fullLink) {
	
	var protocol = document.location.protocol;

	if (fncGetSiteClass() != "myaccount") protocol = "http:";	

	var retAnchor = '<A CLASS="supportLinks" ID="SUPPORTLINKS" TARGET="main"' +
					' HREF="' + protocol + '//' + host + fullURL + '">' + fullLink + '</'+'A>';	
 	return retAnchor;
	
}

// Footers for plaBorders
function getPlaBorderFooter() {

	var today = new Date(); 
	var strCopyYear = today.getFullYear();
	var strLegalHref = "/registered/legal_home.xml"
	
	var retval = "<table id='tblFooter' width='730' border='0' align='center' cellpadding='0' cellspacing='0'>" + 
				 "<tr>" +
				 "<td bgcolor='FFCC66' height='1'><img src='/images/templates/spacer.gif' width='1' height='1'></td>" +
				 "</tr><tr>";					
	
	if (fncGetSiteClass() == "corporate" || fncGetSiteClass() == "myaccount")
		retval += "<td class='disclaimer' bgcolor='#FFFFFF'>Copyright " + strCopyYear + " &#169; " + getCompanyName();
	
	
	else 			
		retval += "<td class='gry10px' bgcolor='#FFFFFF'>For broker/dealer use only. Not for use with the public.";	
	
	if (fncGetSiteClass() == "myaccount" || fncGetSiteClass() == "corporate"){
	//LEGAL STUFF

		//PUBLIC
		if (getSiteType() == 'MF' || getSiteType() == 'CS') 
			strLegalHref = "/public/mutual_funds/legal_info_pub.xml";
		else if (getSiteType() == 'PANDA')
			strLegalHref = "/public/panda/legal_pub.xml";
		else
			strLegalHref = "/public/legal_public.xml";			
		
	 	//BROKER
	}else {
		if (getSiteType() == 'MF' || getSiteType() == 'CS') 
			strLegalHref = "/public/mutual_funds/legal_info_pub.xml";
		else if (getSiteType() == 'PANDA')
			strLegalHref = "/panda/registered/legal_home.xml";
	}		
			
	retval += "<div align='right' style='position:relative; top: -10px;'>" + 
			  "<a class='breadcrumbs' href='" + strLegalHref + "'>Legal/Privacy Information</a>";
			  
	if (fncGetSiteClass() != "corporate") {				  
		retval += "<SPAN CLASS='yellowPipe'> | </SPAN>" + 	
				  "<a target=main class='breadcrumbs' href='/public/software_center.xml'>Software Center</a>";
	}	  
				  
	retval += "</div>";
	
	
	retval += "</td></tr></table>"
	return retval;
}


// Search Form Validation
function checkVal(obj) {
	(obj.value == "")?alert("Please enter one or more search terms in the search box."):obj.form.submit();
}

// Montana site disclaimer window
var wndDisclaimer;

function fncDisclaimer(url) {	
	var features = "dependent=yes,height=250,width=525,resizable=yes,scrollbars=yes,toolbar=no";
	wndDisclaimer = window.open(url, 'disclaimer', features);
	wndDisclaimer.focus();
}


//General
function fncLine(strBgColor,strLineHeight,strLineWidth){
	var strLine = "";
	strLine = '<table border="0" cellpadding="0" cellspacing="0"><tr><td BGCOLOR="#' + strBgColor + '"><IMG BORDER="0" WIDTH="' + strLineWidth + '" HEIGHT="' + strLineHeight + '" SRC="/images/general/spacer.gif"><\/td><\/tr><\/table>';
	document.write(strLine);
}

//discover the whale contest
function fncDiscoverWhale(nmbr,align){
	var strWhaleImage = '<a href="/registered/promotions/discover_the_whale/how_to_play.xml">' + 
		'<img src="/images/promotions/discover_the_whale/beanie_whale_' + nmbr + '.jpg" width="70" height="70" border="0" VSPACE="5" HSPACE="5"';
		if((align == "right") || (align == "left") || (align == "center") || (align == "middle")){
			strWhaleImage = strWhaleImage + ' align="' + align + '"';
		}
	strWhaleImage = strWhaleImage + '></a>';
	document.write(strWhaleImage);
}

// String Validation
function fncReplaceChar(str, chr, newchr){
	for(var i=0;i<str.length;i++){
		str = str.replace(chr,newchr);		
	}
	return str;
}

function replaceChar(str, chr){
	for(var i=0;i<str.length;i++){
		str = str.replace(chr,"");		
	}
	return str;
}

function rep_leading_blanks(inString){
	var i, j = inString.length, retString = inString;
	for (var i = 0; i < j ; i++) {
		if (inString.charAt(i) == ' ')
			retString = inString.substr(i+1,j-i-1);
		else
			return(retString);
	}
	return(retString);
}

function rep_trailing_blanks(inString){
	var i, j = inString.length, retString = inString;
	
	for (var i = j; i > 0;  i--) {
		if (inString.charAt(i-1)==' ')
			retString=inString.substr(0,i-1);
		else
		return(retString);
	}	
	return(retString);
}

function mask (InString, Mask)  {
	var LenStr = InString.length, LenMsk = Mask.length;
	
	if ((LenStr==0) || (LenMsk==0))	return(0);
	if (LenStr!=LenMsk)	return(0);

	for(var Count=0; Count<=InString.length; Count++) {
		StrChar = InString.substring(Count, Count+1);
		MskChar = Mask.substring(Count, Count+1);
		if (MskChar=='#') {
			if(!isNumberChar(StrChar)) return(0);
		} else if (MskChar=='?') {
			if(!isAlphabeticChar(StrChar)) return(0);
		} else if (MskChar=='!') {
			if(!isNumOrChar(StrChar)) return(0);
		} else if (MskChar=='*') {
		} else {
			if (MskChar!=StrChar) return(0);
		}
	}
	return (1);
}

function isNumberChar (InString)  {
	var RefString="1234567890";

	if(InString.length!=1) return false;
	if (RefString.indexOf (InString, 0)==-1) return false;
	
	return true;
}

function isAlphabeticChar (InString)  {
	var	RefString="abcdefghijklmnopqrstuvwxyz";
	
	if(InString.length != 1) return false;	
	InString=InString.toLowerCase ()
	if (RefString.indexOf (InString, 0)==-1) return false;
	
	return true;
}

function isNumOrChar (InString)  {
	var RefString="1234567890abcdefghijklmnopqrstuvwxyz";

	if(InString.length!=1) return false;
	InString=InString.toLowerCase();
	if (RefString.indexOf (InString, 0)==-1) return false;

	return true;
}
var aryCommonJsNumbers = new Array("0","1","2","3","4","5","6","7","8","9");
var aryCommonJsLetters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
var aryCommonJsOtherChar = new Array(",",".","<",">","/","\\","|","{","}","[","]","`","~","!","@","#","$","%","^","&","*","(",")","-","_","+","=",";",":","?","'","\"");
	
//Verifies character based strings
function fncCharStringVerification(strValue,type)  {
	var allowNumbers = "no";
	if(type == "Name" || type == "Street Address" || "State License"){
		allowNumbers = "yes";
	}
	
	var foundNumber = "no";
	if(allowNumbers == "no"){
		for(var x = 0; x < aryCommonJsNumbers.length; x++){
			if(strValue.indexOf(aryCommonJsLetters[x]) > -1){
				foundNumber = "yes";
				//alert("Please do not enter numbers in the " + type + " filed.");
				return false;
				break;
			}
		}
	}
	
	if(foundNumber == "no"){
		for(var y = 0; y < aryCommonJsOtherChar.length; y++){
			if(strValue.indexOf(aryCommonJsOtherChar[y]) > -1){
				if(type == "Name"){	//allow periods, dashes, and commas in name fields
					if((aryCommonJsOtherChar[y] != ".") && (aryCommonJsOtherChar[y] != "-") && (aryCommonJsOtherChar[y] != ",")){
						return false;
						break;
					}
				}else if(type == "Street Address"){	//allow periods, dashes, and commas in street address fields
					if((aryCommonJsOtherChar[y] != ".") && (aryCommonJsOtherChar[y] != "-") && (aryCommonJsOtherChar[y] != "#")){
						return false;
						break;
					}
				}else if(type == "State License"){	//allow dashes in state license
					if((aryCommonJsOtherChar[y] != "-")){
						return false;
						break;
					}
				}else{
					return false;
					break;
				}
			}
		}
	}
}

//Checks to verify string is a number
function fncCleanNumbers(strValue,type){
	var foundLetter = "no";
	for(var x = 0; x < aryCommonJsLetters.length; x++){
		if(strValue.indexOf(aryCommonJsLetters[x]) > -1){
			foundLetter = "yes";
			//alert("Please enter only numbers and commas in the " + type + " fields.");
			return false;
			break;
		}
	}
	
	if(foundLetter == "no"){
		for(var y = 0; y < aryCommonJsOtherChar.length; y++){
			if(strValue.indexOf(aryCommonJsOtherChar[y]) > -1){
				if(type == "Annual Rates"){
					if((aryCommonJsOtherChar[y] != ".") && (aryCommonJsOtherChar[y] != "-")){	//allow decimals in rates
						//alert("Please enter only numbers and commas in the " + type + " fields.");
						return false;
						break;
					}
				}else if(type == "Tax ID"){
					if(aryCommonJsOtherChar[y] != "-"){	//allow dashes
						//alert("Please enter only numbers and commas in the " + type + " fields.");
						return false;
						break;
					}
				}else if(type == "Phone"){
					if(aryCommonJsOtherChar[y] != "-" && aryCommonJsOtherChar[y] != "(" && aryCommonJsOtherChar[y] != ")"){
						return false;
						break;
					}
				}else if(type == "Zip" || type == "Pure"){
					return false;
					break;
				}else{
					if(aryCommonJsOtherChar[y] != ","){	//allow commas, except for rates
						//alert("Please enter only numbers and commas in the " + type + " fields.");
						return false;
						break;
					}
				}
			}
		}
	}
}

// contact us section -> IW multiple extension solution.
function fncMultiIwExtension(ext1, ext2, url2)
{
	var intResults = strUrl.indexOf(url2,0);
	if(intResults == -1)
	{
		return ext1;
	}
	else
	{
		return ext2;
	}
}


// helps protect against cross site scripting
function removeInvalidChar(strFieldValue) {	
	// test for pattern
	var oRegExpPattern = /%3e|%3c/g;
	if (oRegExpPattern.test(strFieldValue)) {
		strFieldValue = "";		
	} else {	
		// if pattern is not present check every char
		var oRegExp = /[<>,<,>]/;		
		for(var i=0; i< strFieldValue.length; i++) {
			if(strFieldValue.charAt(i) != " ") {	
				if(oRegExp.test(strFieldValue.charAt(i))) {
					strFieldValue = strFieldValue.replace(strFieldValue.charAt(i), " ");
				}	
			}	
		}	
	}	
	return strFieldValue	
}
