
var AWStatusMsg = "Alchemy Web" + unescape("%AE") + " 8.2.100";
function OpenAwWindow(page,name)
{
   window.open(URLEncode(page),name,'toolbar=no,status=no,resizable=yes');
   window.status = AWStatusMsg;
}

//////////////////////////////////////////////////////////
// Back link navigation helper functions                //
// Usage:                                               //
// - On the page that has the back link add (Page_Load) //
//   PageBody.Attributes.Add("onload","initHistory()"); //
// - Set the NavigationURL of the back link to          //
//   javascript:goBack();                               //
//                                                      //
// These scripts will account for any number of         //
// postbacks and take the page 'back' to where          //
// it started.                                          //
//////////////////////////////////////////////////////////
var bInitialized = false;
var startHistory = 0;
function initHistory()
{
   if (!bInitialized) {
      bInitialized = true;
      startHistory = self.history.length;
   }
}
function goBack()
{
   self.history.go(-(self.history.length - startHistory + 1));
}


function URLEncode( StrToEncode )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = StrToEncode;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
				        "(URL encoding only supports 8-bit characters.)\n" +
						"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

   // Make sure the ? is in place otherwise you will get a page not found error.  
   // ? is either %3f or %3F   
   encoded = encoded.replace(/\%3[fF]/, "?");
   
	return encoded;
	
}


//
//
// BIN BASE64 Encoding
//
//

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
	"abcdefghijklmnopqrstuvwxyz" + //all lowercase
	"0123456789+/="; // all numbers plus +/=


//Heres the decode function
function decode64(inp)
{
   var out = ""; //This is the output
   var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
   var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
   var i = 0; //Position counter

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   var base64test = /[^A-Za-z0-9\+\/\=]/g;

   if (base64test.exec(inp)) { //Do some error checking
      alert("There were invalid base64 characters in the input text.\n" +
      "Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
      "Expect errors in decoding.");
   }

   inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do { //Here’s the decode loop.
      //Grab 4 bytes of encoded content.
      enc1 = keyStr.indexOf(inp.charAt(i++));
      enc2 = keyStr.indexOf(inp.charAt(i++));
      enc3 = keyStr.indexOf(inp.charAt(i++));
      enc4 = keyStr.indexOf(inp.charAt(i++));

      //Heres the decode part. There’s really only one way to do it.
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      //Start to output decoded content
      out = out + String.fromCharCode(chr1);

      if (enc3 != 64) {
         out = out + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         out = out + String.fromCharCode(chr3);
      }

      //now clean out the variables used
      chr1 = chr2 = chr3 = "";
      enc1 = enc2 = enc3 = enc4 = "";

   } while (i < inp.length); //finish off the loop

   //Now return the decoded values.
   return out;
}


//Heres the encode function
function encode64(inp)
{
   var out = ""; //This is the output
   var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
   var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
   var i = 0; //Position counter

   do { //Set up the loop here
      chr1 = inp.charCodeAt(i++); //Grab the first byte
      chr2 = inp.charCodeAt(i++); //Grab the second byte
      chr3 = inp.charCodeAt(i++); //Grab the third byte

      //Here is the actual base64 encode part.
      //There really is only one way to do it.
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      //Lets spit out the 4 encoded bytes
      out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
      keyStr.charAt(enc4);

      // OK, now clean out the variables used.
      chr1 = chr2 = chr3 = "";
      enc1 = enc2 = enc3 = enc4 = "";

   } while (i < inp.length); //And finish off the loop

   //Now return the encoded values.
   return out;
}

function IsLanguageLeftToRight()
{
   var sUserLanguage = navigator.userLanguage;
   if( (sUserLanguage.indexOf( "ar" ) == 0) ||
       (sUserLanguage.indexOf( "he" ) == 0) || 
       (sUserLanguage.indexOf( "fa" ) == 0) ||
       (sUserLanguage.indexOf( "ur" ) == 0) )
   {
      return (false);
   }
   
   return (true);   
}

