// widget.js

<!--

var Domain = "www.courserewards.com";
var isLocal = false;
var privateLabel = true; 

// decide whether we need to append the prefix or not by deciding whether this script is
// running on a local server or from a remote server...
if( document.location.host.match(/192\.168\./) ||
    document.location.host.match(/bean[\d+]\.courserewards/) ||
    document.location.host.match(/demo[\d+]\.courserewards/) ) isLocal = true;

// variables that are adjusted with functions below...
var headingStr = "";
var drawContainer = true;
var btnUrl = "";
var Target = "";

// colors and fonts...
var borderColor = "#5C8608";
var bgColor = "#ffffff";
var fontFamily = "Verdana, Arial, Helvetica, sans-serif";
var fontSize = "12px";
var enclosureBorderColor = "#5C8608";

function CR_PrivateLabel()
{
 privateLabel = true;
}

function CR_SetHeadingText(str)
{
 headingStr = str;
}

function CR_SetButtonUrl(url)
{
 btnUrl = url;
}

function CR_SetEnclosureBorderColor(clr)
{
 enclosureBorderColor = clr;
}

function CR_NoDrawContainer()
{
 drawContainer = false;
}

function CR_SetBorderColor(clr)
{
 borderColor = clr;
}

function CR_SetBackgroundColor(clr)
{
 bgColor = clr;
}

function CR_setFontFamily(fam)
{
 fontFamily = fam;
}

function CR_setFontSize(fsize)
{
 fontSize = fsize;
}

function CR_SetTarget(t)
{
 Target = t;
}

function CR_StyleWidget()
{
 document.writeln( "<style type=\"text/css\">" ); 

 document.writeln( ".crWidgetBtn {" );
 document.writeln( " font-family: " + fontFamily + ";" );
 document.writeln( " font-size: 100%; line-height: 130%;" );
 document.writeln( " width: 0; /* IE table-cell margin fix */" );
 document.writeln( " min-width: 120px; min-height: 20px max-height: 42px; max-width: 160px;" );
 document.writeln( " overflow: visible; padding: 5px; text-decoration: none; cursor: pointer;" );
 document.writeln( " white-space: nowrap; margin: auto;" );
 document.writeln( " border: 1px solid #666666; background-color: #f2f2f2;" );
 document.writeln( "}" );

 document.writeln( ".crWidgetBtn[class] { /* IE ignores [class] */ width: auto; }" );

 document.writeln( ".widgetHighlightInput { border: 1px #F7B512 solid; background-color: #ffff99; }" );

 document.writeln( "table.crcontainer { " );
 document.writeln( " font-family: " + fontFamily + ";" );
 document.writeln( " font-size: 10px;" );
 document.writeln( " background-color: " + bgColor + ";" );
 document.writeln( " border-left: 1px " + borderColor + " solid;" );
 document.writeln( " border-right: 1px " + borderColor + " solid;" );
 document.writeln( " border-bottom: 1px " + borderColor + " solid;" );
 document.writeln( "}" );

 document.writeln( "table.crcontainer td, th { padding: .6em; }" );

 document.writeln( "table.crcontainer thead th { text-align: center; background: #F5F5F5; color: #666; border: 1px solid #FFF; text-transform:uppercase; }" );

 document.writeln( "table.crcontainer tbody tr { text-align:left; }" );

 document.writeln( "table.crcontainer tbody tr td { height: 30px; }" );

 document.writeln( ".crcaption { font-weight: bold; background: #5C8608 url(/images/corner2.gif) no-repeat right top; border-bottom: 2px solid #016600; color: #ffffff; text-transform: uppercase; padding: 7px; }" );

 // crsimple style is used when no container is drawn and is used when customization is desired...
 document.writeln( "table.crsimple {" );
 document.writeln( " font-family: " + fontFamily + ";" );
 document.writeln( " font-size: 10px;" );
 document.writeln( " background-color: " + bgColor + ";" );
 document.writeln( " border-top: 1px " + borderColor + " solid;" );
 document.writeln( " border-left: 1px " + borderColor + " solid;" );
 document.writeln( " border-right: 1px " + borderColor + " solid; " );
 document.writeln( " border-bottom: 1px " + borderColor + " solid;" );
 document.writeln( "}" );

 document.writeln( "table.crsimple td { padding: .6em; }" );

 // crnormal style is used typically for returned text from form submissions...
 document.writeln( ".crnormal {" );
 document.writeln( "font-family: " + fontFamily + ";" );
 document.writeln( "font-size: " + fontSize + ";" );
 document.writeln( "}" );

 // crheading style is used typically for headings returned as part of text submissions...
 document.writeln( ".crheading {" );
 document.writeln( "font-family: " + fontFamily + ";" );
 document.writeln( "font-size: 16px;" );
 document.writeln( "font-weight: bold;" );
 document.writeln( "}" );

 document.writeln( "</style>" );
}

/***********************************/
// internal functions...
/***********************************/

function widgetHideHighlights(formObj)
{
 for( var i = 0; i < formObj.elements.length; i++ )
  {
   if( formObj.elements[i].className == "highlightInput" ) formObj.elements[i].className = "";
  }
}

function widgetHighlightError(inputObj)
{
 inputObj.className = "widgetHighlightInput";
 inputObj.focus();
}

function widgetEmailCheck(str)
{
 var emailStr = str.toLowerCase();

 var checkTLD=1;
 var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

 var emailPat=/^(.+)@(.+)$/;
 var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

 var validChars="\[^\\s" + specialChars + "\]";
 var quotedUser="(\"[^\"]*\")";

 var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
 var atom=validChars + '+';

 var word="(" + atom + "|" + quotedUser + ")";
 var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
 var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

 var matchArray=emailStr.match(emailPat);

 if( matchArray==null )
  {
   alert("Email address seems incorrect (check @ and .'s)");
   return false;
  }

 var user=matchArray[1];
 var domain=matchArray[2];

 // Start by checking that only basic ASCII characters are in the strings (0-127).
 for( i=0; i < user.length; i++ )
  {
   if( user.charCodeAt(i) > 127 )
    {
     alert("Ths username contains invalid characters.");
     return false;
    }
  }

 for( i=0; i<domain.length; i++ )
  {
   if( domain.charCodeAt(i) > 127 )
    {
     alert("Ths domain name contains invalid characters.");
     return false;
    }
  }

 // See if "user" is valid
 if( user.match(userPat) == null )
  {
   alert("The username doesn't seem to be valid.");
   return false;
  }

 /* if the e-mail address is at an IP address (as opposed to a symbolic
 host name) make sure the IP address is valid. */
 var IPArray=domain.match(ipDomainPat);
 if( IPArray != null )
  {
   // this is an IP address
   for( var i=1;i<=4;i++ )
    {
     if( IPArray[i] > 255 )
      {
       alert("Destination IP address is invalid!");
       return false;
      }
    }

   return true;
  }

 // Domain is symbolic name.  Check if it's valid.
 var atomPat=new RegExp("^" + atom + "$");
 var domArr=domain.split(".");
 var len=domArr.length;

 for( i=0;i<len;i++ )
  {
   if(domArr[i].search(atomPat)==-1)
    {
     alert("The domain name does not seem to be valid.");
     return false;
    }
  }

 /* domain name seems valid, but now make sure that it ends in a
 known top-level domain (like com, edu, gov) or a two-letter word,
 representing country (uk, nl), and that there's a hostname preceding
 the domain or country. */

 if(checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
  {
   alert("The address must end in a well-known domain or two letter " + "country.");
   return false;
  }

 // Make sure there's a host name preceding the domain.
 if( len < 2 )
  {
   alert("This address is missing a hostname!");
   return false;
  }

 return true;
}

function widgetClearNodes(nref)
{
 if( nref == null ) return;

 while( nref.hasChildNodes() == true )
  {
   nref.removeChild(nref.firstChild);
  }
}

function widgetShowMessage(request)
{
 var func = new Function("return " + request);
 var obj = func();

 var targetObj = document.getElementById( obj.target );
 if( ! targetObj ) return;

 widgetClearNodes(targetObj);
 targetObj.innerHTML = obj.html;
}

function startWidgetEnclosure(w)
{
 var str = "<table border=\"0\" width=\"" + w + "\" cellspacing=\"0\" cellpadding=\"4\" style=\"border: 1px " + enclosureBorderColor + " solid\">\n";
 str += "<tr><td colspan=\"2\"><img src=\"/graphics/clr.gif\" border=\"0\" align=\"absmiddle\"></td></tr>\n";
 str += "<tr><td colspan=\"2\" valign=\"middle\" align=\"center\">\n";

 return str;
}

function endWidgetEnclosure()
{
 var str = "</td></tr>";
 str += "<tr><td colspan=\"2\"><img src=\"/graphics/clr.gif\" border=\"0\" align=\"absmiddle\"></td></tr>\n";
 str += "<tr><td colspan=\"2\"><div style=\"border-bottom: 1px " + enclosureBorderColor + " solid\">&#32;</div></td></tr>\n";
 str += "<tr><td align=\"left\"><img src=\"/images/CourseRewardsLogoSml.jpg\" border=\"0\" align=\"absmiddle\"></td><td valign=\"middle\" align=\"right\">Copyright &#169;&#32;2010<br><a href=\"http://www.courserewards.com\">www.courserewards.com</a></td></tr>\n";
 str += "</table>\n";

 return str;
}

/***********************************/
// login widget...
/***********************************/

function CR_ForgotSubmit()
{
 if( ! widgetEmailCheck(document.crforgotfm.email.value) ) return false;

 return true;
}

function drawForgotPswd()
{
 var w = 410;

 var obj = document.getElementById("crlogindiv");
 if( ! obj ) return;

 widgetClearNodes(obj);

 str = "<form name=\"crforgotfm\" method=\"post\" action=\"";
 if( ! isLocal ) str += "http://" + Domain;
 str += "/cgi-bin/forgot.cgi\" target=\"crloginfr\" onsubmit=\"return CR_ForgotSubmit()\">";

 if( ! privateLabel ) str += startWidgetEnclosure(w + 20);

 str += "<table border=\"0\" width=\"" + w + "\" cellspacing=\"0\" cellpadding=\"4\">";
 str += "<tr>";
 str += "<td><span>If you are a registered rewards member, you can request your password to be sent to the email address registered to your account.<br><br>Simply provide the email address linked to your account and we will email you the password.<br><br><a href=\"#\" onclick=\"redrawLoginWidget(); return false\">Click here</a> to return to the login form.</span></td>";
 str += "</tr>";

 str += "<tr><td>&nbsp;</td></tr>";

 str += "<tr>";
 str += "<td>";

 if( drawContainer )
  {
   str += "<table width=\"" + w + "\" class=\"crcontainer\" cellspacing=\"0\" cellpadding=\"0\">";
   str += "<caption class=\"crcaption\">Forgot Password Request</caption>";
  }
 else
  {
   str += "<table width=\"" + w + "\" class=\"crsimple\" cellspacing=\"0\" cellpadding=\"0\">";
  }

 str += "<tbody>";

 str += "<tr>";
 str += "<td align=\"right\">Email Address</td>";
 str += "<td>&nbsp;</td>";
 str += "<td align=\"left\"><input name=\"email\" type=\"text\" size=\"30\" maxlength=\"40\"></td>";
 str += "</tr>";
 str += "</tbody>";
 str += "</table>";

 str += "<br>";
 str += "<table width=\"" + w + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
 str += "<tr><td align=\"center\"><input type=\"submit\" class=\"crWidgetBtn\" value=\"Submit\"></td></tr>";
 str += "</table>";
 str += "</td>";
 str += "</tr>";
 str += "</table>";

 if( ! privateLabel ) str += endWidgetEnclosure();

 str += "</form>";

 obj.innerHTML = str;
}

function CR_LoginSubmit()
{
 widgetHideHighlights(document.crloginfm);

 if( ! widgetEmailCheck(document.crloginfm.user.value) ) return false;

 if( document.crloginfm.password.value.length < 1 )
  {
   widgetHighlightError(document.crloginfm.password);
   alert( "A password is required to login." );
   return false;
  }

 return true;
}

function redrawLoginWidget()
{
 var obj = document.getElementById("crlogindiv");
 if( ! obj ) return;

 widgetClearNodes(obj);

 obj.innerHTML = drawLoginWidget();
}

function CR_LoginWidget()
{
 document.writeln( "<div id=\"crlogindiv\">" );

 document.write( drawLoginWidget() );

 document.writeln( "</div>" );

 document.writeln( "<iframe name=\"crloginfr\" id=\"crregisterfr\" src=\"about:blank\" frameborder=\"0\" border=\"0\" height=\"0\" width=\"0\"></iframe>" );
}

function drawLoginWidget()
{
 var w = 410;

 var str = "<form name=\"crloginfm\" method=\"post\" action=\"";
 if( ! isLocal ) str += "https://" + Domain;
 str += "/cgi-bin/login.cgi\"";

 if( Target.length > 0 ) str += " target=\"" + Target + "\"";
 str += " onsubmit=\"return CR_LoginSubmit()\">\n";

 if( arguments.length > 0 )
   str += "<input type=\"hidden\" name=\"exit_url\" value=\"" + arguments[0] + "\">\n";

 if( ! privateLabel ) str += startWidgetEnclosure(w + 20);

 str += "<table border=\"0\" width=\"" + w + "\" cellspacing=\"0\" cellpadding=\"4\">\n";
 str += "<tr>\n";
 str += "<td><span>If you are a registered rewards member, log in using the form below.</span><br><br>";
 str += "<span>Forgot your password?&nbsp;&nbsp;<a href=\"#\" onclick=\"drawForgotPswd(); return false\">Click here</a>.</span></td>\n";
 str += "</tr>\n";

 str += "<tr><td>&#32;</td></tr>";
 str += "<tr><td id=\"mymesg\" align=\"center\">&#32;</td></tr>\n";

 str += "<tr>\n";
 str += "<td>\n";

 if( drawContainer )
  {
   str += "<table width=\"" + w + "\" class=\"crcontainer\" cellspacing=\"0\" cellpadding=\"0\">\n";
   if( headingStr.length == 0 ) headingStr = "Rewards Member Login";
   str += "<caption class=\"crcaption\">" + headingStr + "</caption>\n";
  }
 else
  {
   str += "<table width=\"" + w + "\" class=\"crsimple\" cellspacing=\"0\" cellpadding=\"0\">\n";
  }

 str += "<tbody>\n";

 str += "<tr>\n";
 str += "<td align=\"right\">Email Address</td>\n";
 str += "<td>&#32;</td>\n";
 str += "<td align=\"left\"><input name=\"user\" type=\"text\" size=\"30\" maxlength=\"60\"></td>\n";
 str += "</tr>\n";
 str += "<tr>\n";
 str += "<td align=\"right\">Password</td>\n";
 str += "<td>&nbsp;</td>\n";
 str += "<td align=\"left\"><input name=\"password\" type=\"password\" size=\"30\" maxlength=\"40\"></td>\n";
 str += "</tr>\n";
 str += "</tbody>\n";

 str += "</table>\n";
 str += "<br>\n";
 str += "<table width=\"" + w + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
 if( btnUrl.length == 0 ) btnUrl = "/graphics/button_securelogin.gif";
 str += "<tr><td align=\"center\"><input type=\"image\" src=\"" + btnUrl + "\"></td></tr>\n";
 str += "</table>\n";
 str += "</td>\n";
 str += "</tr>\n";
 str += "</table>\n";

 if( ! privateLabel ) str += endWidgetEnclosure();

 str += "</form>\n";

 return str;
}

/***********************************/
// register card widget...
/***********************************/

function CR_RegisterSubmit()
{
 widgetHideHighlights(document.crregisterfm);

 if( document.crregisterfm.card_num.value.length != 6 ||
     document.crregisterfm.card_num.value < 1 || document.crregisterfm.card_num.value > 900001 )
   {
    widgetHighlightError(document.crregisterfm.card_num);
    alert( "A valid rewards card number is required." );
    return false;
   }

 if( document.crregisterfm.first_name.value.length < 2 )
  {
   widgetHighlightError(document.crregisterfm.first_name);
   alert( "Please provide your first name." );
   return false;
  }

 if( document.crregisterfm.last_name.value.length < 2 )
  {
   widgetHighlightError(document.crregisterfm.last_name);
   alert( "Please provide your last name." );
   return false;
  }

 if( ! widgetEmailCheck(document.crregisterfm.email.value) )
  {
   widgetHighlightError(document.crregisterfm.email);
   return false;
  }

 if( ! widgetEmailCheck(document.crregisterfm.email2.value) )
  {
   widgetHighlightError(document.crregisterfm.email2);
   return false;
  }

 // make sure the two emails match
 if( document.crregisterfm.email.value != document.crregisterfm.email2.value )
  {
   alert( "Email addresses do not match.  Please try again." );
   widgetHighlightError(document.crregisterfm.email);
   return false;
  }

 return true;
}

function CR_RegisterCardWidget()
{
 var w = 450;

 document.writeln( "<div id=\"crregisterdiv\">" );

 document.write( "<form name=\"crregisterfm\" method=\"post\" action=\"" );
 if( ! isLocal ) document.write( "http://" + Domain );
 document.writeln( "/cgi-bin/register.cgi\" target=\"crregisterfr\" onsubmit=\"return CR_RegisterSubmit()\">" );

 if( ! privateLabel ) document.write( startWidgetEnclosure(w+60) );

 if( drawContainer )
  {
   document.writeln( "<table width=\"" + w + "\" class=\"crcontainer\" cellspacing=\"0\" cellpadding=\"0\">" );
   if( headingStr.length == 0 ) headingStr = "Your Contact Info (All Required)";
   document.writeln( "<caption class=\"crcaption\">" + headingStr + "</caption>" );
  }
 else
  {
   document.writeln( "<table width=\"" + w + "\" class=\"crsimple\" cellspacing=\"0\" cellpadding=\"0\">" );
  }

 document.writeln( "<tbody>" );
 document.writeln( "<tr>" );
 document.writeln( "<td>Card Number</td>" );
 document.writeln( "<td><input name=\"card_num\" type=\"text\" size=\"20\" maxlength=\"10\"></td>" );
 document.writeln( "</tr>" );
 document.writeln( "<tr>" );
 document.writeln( "<td>First Name</td>" );
 document.writeln( "<td><input name=\"first_name\" type=\"text\" size=\"40\" maxlength=\"40\"></td>" );
 document.writeln( "</tr>" );
 document.writeln( "<tr>" );
 document.writeln( "<td>Last Name</td>" );
 document.writeln( "<td><input name=\"last_name\" type=\"text\" size=\"40\" maxlength=\"40\"></td>" );
 document.writeln( "</tr>" );
 document.writeln( "<tr>" );
 document.writeln( "<td>Email</td>" );
 document.writeln( "<td><input name=\"email\" type=\"text\" size=\"40\" maxlength=\"50\"></td>" );
 document.writeln( "</tr>" );
 document.writeln( "<tr>" );
 document.writeln( "<td>Email (again)</td>" );
 document.writeln( "<td><input name=\"email2\" type=\"text\" size=\"40\" maxlength=\"50\"></td>" );
 document.writeln( "</tr>" );
 document.writeln( "</tbody>" );
 document.writeln( "</table>" );

 document.writeln( "<br>" );
 document.writeln( "<table width=\"" + w + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" );
 if( btnUrl.length > 0 )
   document.writeln( "<tr><td align=\"center\"><input type=\"image\" src=\"" + btnUrl + "\"></td></tr>" );
 else
   document.writeln( "<tr><td align=\"center\"><input type=\"submit\" class=\"crWidgetBtn\" value=\"Submit\"></td></tr>" );
 document.writeln( "</table>" );

 if( ! privateLabel ) document.write( endWidgetEnclosure() );

 document.writeln( "</form>" );
 document.writeln( "</div>" );

 document.writeln( "<iframe name=\"crregisterfr\" id=\"crregisterfr\" src=\"about:blank\" frameborder=\"0\" border=\"0\" height=\"0\" width=\"0\"></iframe>" );
}

//-->
