/**
 * Genworth Header Forms
 *
 * The GenworthHeaderForms.js is a collection of JavaScript methods used by the 
 * Country Select and Search Forms located in the header of pages in the 
 * Gewnworth.com domain.
 *
 * @author Genworth Financial, Inc.
 * @created 19 April 2007 
 * @since 19 April 2007
 * @version 1.0
 * @modified 19 April 2007 : Birth
 */

/* ===== COUNTRY SELECTOR ===== */
/**
 * public initCountrySelector method
 */
  /* void */ function initCountrySelector ()
  {
    /*String*/    var loc = window.location.href;
    /*SelectOne*/ var sel = document.getElementById('countrySelector');
   


if (sel){
	 for (/*int*/ var i=1; i<sel.length; i++)
    {	
      if (loc.indexOf(sel.options[i].value) >= 0)
      {
        sel[i].selected = true;
        break;
      }
    }
}



  }

/**
 * public submitGatewayCountrySelect method
 */
  /*void*/ function submitGatewayCountrySelect ()
  {
    /*Checkbox*/  var cb = document.getElementById('remember');
    submitCountrySelect(cb.checked);
  }


/**
 * public submitCountrySelect method
 */
  /*void*/ function submitCountrySelect (/*boolean*/ writeCookie)
  {
    /*SelectOne*/ var sel = document.getElementById('countrySelector');
    /*int*/       var sid = sel.selectedIndex;
    //sel.selectedIndex = 0;
    if (sid < 1) { alert ('Please select a Country.'); return; }

    /*String[]*/  var csa = _parseSelectValue(sel[sid].value);
    if (window.location.href.indexOf(csa[0]) == 0) { return; }
    if (writeCookie) { CookieFactory.createCookie('country', encode(csa[0]), 365); }
    window.location.href=csa[0];
  }

/**
 * private parseSelectValue method
 *
 * @param val The value from the countrySelector
 * @return String[] The array of values parsed from the val<br>
 *          0: The URL
 *          1: The QueryString
 *          2: Mapping value for Flash Map
 */
  /*String[]*/ function _parseSelectValue (/*String*/ val)
  {
    /*String*/    var va = new Array();
    /*String[]*/  var ar1 = val.split( "|" );
    /*String[]*/  var ar2 = ar1[0].split("?");
    va[0] = ar2[0];
    va[1] = ar2[1];
    va[2] = ar1[1];
    return va;
  }

/**
 * public resetCountrySelector method
 */
  /*void*/ function resetCountrySelector ()
  {
    /*SelectOne*/ var sel = document.getElementById('countrySelector');
    sel.selectedIndex = 0;
  }


/* ===== SEARCH FORM ===== */
/**
 * public submitSearchForm method
 */
  /*void*/ function submitSearchForm ()
  {
    /*TextBox*/ var sb = document.getElementById('search_text');
    if (!sb.value || sb.value.match(/^\s*$/) || sb.value == 'Search')
    { 
      alert('Please enter your search criteria.');
      return;
    }
    document.searchForm.q.value=sb.value;  
    document.searchForm.submit();   
  }
/**
 * onclick for printThisPage component
 */

	function printThisPage(){

	sHeader="<html><head>	<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>";
	sHeader+="<title>Print View</title>";
	sHeader+="<link href='/author/images/favicon.ico' rel='shortcut icon' type='image/x-icon' />";
	sHeader+=" <link href='/apps/genworth/docroot/css/default_screen_small.css' rel='stylesheet' type='text/css'  title='Small Texts'/>";
	sHeader+=" <link href='/apps/genworth/docroot/css/default_screen_medium.css' rel='stylesheet' type='text/css' title='Medium Texts'/>";
	sHeader+=" <link href='/apps/genworth/docroot/css/default_screen_large.css' rel='stylesheet' type='text/css'  title='Large Texts'/>";

	sHeader+="<script type='text/javascript' src='/content/scripts/dom.js' language='JavaScript'></script>";
	sHeader+="<script type='text/javascript' src='/content/scripts/navs.js' language='JavaScript'></script>";
	sHeader+="<script type='text/javascript' src='/content/scripts/selectCountry.js' language='JavaScript'></script>";
  
  	sHeader+="<script language='JavaScript' src='http://dev.genworth.com/comweb2/scripts/jump.js' type='text/javascript'></script>";
  
	sHeader+="<script type='text/javascript' src='/content/scripts/CookieFactory.js'></script>";
	sHeader+="<script type='text/javascript' src='/content/scripts/GenworthStyleSwitcher.js'></script>";
	sHeader+="<script type='text/javascript' src='/content/scripts/GenworthHeaderForms.js'></script>";
	sHeader+="</head>";
	
	sBody="<body marginheight='0' topmargin='0' marginwidth='0' leftmargin='0' style='background-color:#FFF;'>";
	sBody+="<p><img src='/author/images/wellness/ie-print.gif'/><small><a href='#' onClick='window.print()'>";
	sBody+="Print this page</a> | <a href='javascript:window.close()'>Close Window</a></small></p>";
	sBody+="<div id = 'content'>";
	sBody+= document.getElementById("contentContainer").innerHTML;
	sBody+="<!-- finish body--></div></body>";
	
	sFooter ="</html>";	
	var sTotalDoc = sHeader+sBody+sFooter;
	myWindow=window.open('','','scrollbars=yes,resizable=yes,location=no,height=580,width=620');
	//myWindow=window.open('');
	myWindow.document.write(sTotalDoc);
	myWindow.focus();
	myWindow.location.reload();

	}

/**
 * onclick for emailThisPage component
 */

function emailThisPage(/*String*/ handle){


	sHeader="<html><head><title>Send Email This Page 13</title>\n";
		
	sHeader+="<SCRIPT language='javascript' type='text/javascript'>";

	sHeader+="function sendEmailURL(){";


	sHeader+=" var valid1 = /(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)/;";
	sHeader+=" var valid2 = /^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$/;";


	sHeader+=" if (!valid1.test(form.Email.value) && valid2.test(form.Email.value)) {";

	sHeader+="	if (form.yourName.value==''){form.yourName.value='Anonymous';}";

	sHeader+=" if (form.name.value==''){form.name.value='Friend';}";

	
	
	sHeader+=" var subj='Visit this Page '+form.name.value+'!!!';";
	
	sHeader+=" form.action = 'mailto:'+ form.Email.value+'?subject='+subj+'&body=Hi '+form.name.value+', '+form.yourName.value+' thought you would enjoy this article. The link is posted below. %0D%0A%0D%0A'+ form.message.value + '%0D%0A%0D%0A' + opener.location.href;";

	sHeader+=" form.Email.disabled=true;";
	sHeader+=" form.name.disabled=true;";
	sHeader+=" form.yourEmail.disabled=true;";
	sHeader+=" form.yourName.disabled=true;";
	sHeader+=" form.message.disabled=true;";
	
	sHeader+=" form.submit();";

	sHeader+=" form.Email.disabled=false;";
	sHeader+=" form.name.disabled=false;";
	sHeader+=" form.yourEmail.disabled=false;";
	sHeader+=" form.yourName.disabled=false;";
	sHeader+=" form.message.disabled=false;";
	
	sHeader+=" document.getElementById('thanksPage').innerHTML='Your email has been sent';";
	
	sHeader+=" }";


	sHeader+=" else {	";


	sHeader+=" alert(\"''\"+form.Email.value+\"''\"+' is not valid!');";

	sHeader+=" form.Email.focus();";

	sHeader+=" form.Email.select();";	

	
	sHeader+=" }} \n";

	sHeader+="function fillParentAndSubmit(){ \n";
	sHeader+="	//Take the form object from opener(the page that fired the popup) \n";
	sHeader+=" var pform = window.opener.document.mailform; \n";
	sHeader+=" //forma is the form in the popup little window \n";
	sHeader+=" //forma was dinamically built by emailThisPage() JS function \n";
	sHeader+=" //pform.email.value = forma.email.value;\n";
	sHeader+=" //pform.name.value = forma.name.value;\n";
	sHeader+=" //pform.youremail.value = forma.youremail.value;\n";
	sHeader+=" //pform.yourname.value = forma.yourname.value;\n";
	sHeader+=" //pform.message.value = forma.message.value;\n";
	sHeader+=" forma.submit();	\n";
	sHeader+=" }\n";





	sHeader+=" </script>";


	sHeader+="<style type='text/css'>";
	sHeader+="td {";
  	sHeader+="text-decoration: none;";
  	sHeader+="border: none;";
	sHeader+="}";
	sHeader+="img {";
  	sHeader+="text-decoration: none;";
  	sHeader+="border: none;";
	sHeader+="}";
	sHeader+="font {";
  	sHeader+="font-style: arial,helvetica,sans-serif;";
  	sHeader+="font-size: 12px;";
  	sHeader+="border: none;";
	sHeader+="}";
	sHeader+="a {";
  	sHeader+="font-family: arial,helvetica,sans-serif;";
  	sHeader+="font-size: 12px;";
  	sHeader+="text-decoration: none;";
  	sHeader+="border: none;";
  	sHeader+="color: #808080;";
	sHeader+="}";
	sHeader+="body {";
	sHeader+="height:			20px;";
	sHeader+="vertical-align:		middle;";
	sHeader+="	font-size:		12px;";
	sHeader+="	font-style:		arial,helvetica,sans-serif;";
	sHeader+="	cursor:			default;";
	sHeader+="	background-color: #CCCCCC;";
	sHeader+="}";
	sHeader+="</style>";
	sHeader+="</head>";

	sHeader+="<BODY><center>";
	sHeader+="<div id='thanksPage'>";
	sHeader+="<FORM name='forma' action='"+handle+"' METHOD='GET' enctype='text/plain'><BR>";
	sHeader+="<table cellpadding='2' cellspacing='2'>";
	sHeader+="<tr><td>Friend Email:</td><td><INPUT TYPE='text' NAME='email' SIZE='26'></td></tr>";
	sHeader+="<tr><td>Friend Name:</td><td><INPUT TYPE='text' NAME='name' SIZE='26'></td></tr>";

	sHeader+="<tr><td>Your Email:</td><td><INPUT TYPE='text' NAME='youremail' SIZE='26'></td></tr>";

	sHeader+="<tr><td>Your Name:</td><td><INPUT TYPE='text' NAME='yourname' SIZE='26'></td></tr>";

	sHeader+="<tr><td>Message:</td><td><textarea rows='5' cols='20' name='message'></textarea></td></tr>";

	sHeader+="<tr><td></td><td><button onclick= 'javascript:fillParentAndSubmit()' > Send Page </button> <INPUT type='reset' value='Clear all' name='clear_all'></td></tr>";

	sHeader+="</table> <input type='hidden' name='urlTxt' value='"+document.URL+"'/></FORM></div></center></body>";
	sHeader+="</html>";

	myWindow=window.open('','','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=300');
	//myWindow=window.open('');
	myWindow.document.write(sHeader);
	myWindow.focus();
}

/**
* to send the URL trough an email
*/
function sendEmailURL(){

var valid1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
var valid2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;


if (!valid1.test(form.Email.value) && valid2.test(form.Email.value)) {


	if (form.yourName.value==""){form.yourName.value="Anonymous";}

	if (form.name.value==""){form.name.value="Friend";}

	
	
	var subj="Visit this Page "+form.name.value+"!!!";
	
	form.action = 'mailto:'+ form.Email.value+'?subject='+subj+'&body=Hi '+form.name.value+', '+form.yourName.value+' thought you would enjoy this article. The link is posted below. %0D%0A%0D%0A'+ form.message.value + '%0D%0A%0D%0A' + opener.location.href;

	form.Email.disabled=true;
	form.name.disabled=true;
	form.yourEmail.disabled=true;
	form.yourName.disabled=true;
	form.message.disabled=true;
	
	form.submit();

	form.Email.disabled=false;
	form.name.disabled=false;
	form.yourEmail.disabled=false;
	form.yourName.disabled=false;
	form.message.disabled=false;
	
	document.getElementById("thanksPage").innerHTML="Your email has been sent";
	
	}


else {	
	alert("''"+form.Email.value+"''"+" is not valid!");
	form.Email.focus();
	form.Email.select();	
	}
}

/**
*
*This function set the parent hidden vars.
*/


function fillParentAndSubmit(){
	//Take the form object from opener(the page that fired the popup)
	var pform = window.opener.document.mailform;		
	//forma is the form in the popup little window
	//forma was dinamically built by emailThisPage() JS function
	pform.email.value = forma.email.value;
	pform.name.value = forma.name.value;
	pform.youremail.value = forma.youremail.value;
	pform.yourname.value = forma.yourname.value;
	pform.message.value = forma.message.value;
	forma.submit();
	
}
