//  External Comminications
// (c) 2008 MRI
//
// JavaScript Utility Library
//
// Interface:
//
//      isInteger(String)           Returns true is the string is an integer
//      isNumeric(String)           Returns true is the string is a number

//      DecodeDelimittedList(List, Delimitter)  
//          Returns an array where each entry is a list item. The specified list
//          specifies entries seperated by the delimitter character.

//      siif(Condition, If True, If False)
//          Performs an inline test of som condition, returning the If True expression
//          if the condition true, and If False otherwise.  Both If True and If False
//          must evalauate to valid expressions regardless of the condition.

//      HideElement(Control ID)   Makes the specified control invisible. The element is returned.
//      ShowElement(Control ID)   Makes the specified control visible. The element is returned.

//      PlaceElement(Control ID, Top, Left)
//          Shows the specified control at the specified coordinates. Returns the control.

//      AddElement(Parent, Tag, ID)
//          Returns a new HTML element given by the Tag.  Both the NAME and ID 
//          properties are set to the specified ID.  If the Parent exists,
//          the new control is added to the parent's list of controls.

//      RemoveElement(Control ID)
//          Removes the specified control from its parent.  The control is returned.

//      SwapElement(Control ID, New Parent ID, Show Flag)
//          Removes the specified control from its parent, and assigns the control
//          to the New Parent's list of controls.  If the Show Flag is true, the
//          control will be displayed.  The control is returned.

//      SetClassName(Control ID, Class Name)
//          Sets the specified control's CLASS property to the specified class name.
//          The control is returned.

//       SetBrowserClassName(Control ID, ClassName) 
//          Sets the CLASS property of the specified control based the current browser
//          environment IE vs FireFox.  Class names beginning with "IE" indicate Internet
//          Explorer classes, where "FF" indicates FireFox classes.  The control is returned.

//      FormatCurrency(Number, Include Cents)
//          Returns a formatted currency string based on the number, including cents
//          if the Include Cents flag is set to true.

//      Dice(Sides, Number)
//          Returns an integer represting die of the specified sides thrown the
//          specified number of times.

//      Wait(Milleseconds)
//          Pauses script execution for the specified number of milleseconds.

//      IndexOf(Value, Array) 
//          Returns the index of the first occurrence of the specified value
//          within some array.  If the value is not found, the function returns -1.

//      Thousands(Number)
//          Returns a string based on some number in comma-seperated format: ...#,###,##0

//      ZeroStr(Number, Length)   
//          Returns the stringer version of some number, padded to the left with zeros,
//          and cut to the specified length.

//      IsOdd(Number)          Returns true of the number is odd, otherwise false (even).

//      SetBackgroundImage(Control ID, Image SRC URL)
//          Sets the background image of the specified control to some URL.
//          The control is returned.

//      SetSpanString(Control ID, Text)
//          Sets the InnerHTML property of the specified control. The control is returned.


//      SetSelectedIndex(Control ID, Index)
//          Sets the SelectedIndex property of the specified control.  The control is returned.

//      FindByID(ID, Array of Objects)
//          Returns the index of the first occurrence of the ID within an array of objects.
//          Each object in the array must possess a property named "iID", a common method
//          of indexing objects in memory.  If the object is not found, the function returns -1.

//      IsEMailValid(EMail) 
//          Returns true if the e-mail address is valid, otherwise false.

//      StrToHex(Integer)       Returns the hexedecimal version of the specified integer.
//      

var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var CRLF="\r\n";
var dCurrentDate = new Date();
var sApp  = navigator.appName;
var bIsIE = (sApp.indexOf("Microsoft Internet Explorer") > -1);
var bIsIE6 = ((window.external) && (typeof window.XMLHttpRequest == "undefined"));
var sBaseURL = "http://moviereviewintelligence.com/";
var sControlPrefix = "ctl00_ContentPlaceHolder1_";
var sNamePrefix = "ctl00$ContentPlaceHolder1$";
var bIsFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") > -1);
var bIsChrome = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1);
var bIsSafari = (navigator.userAgent.toLowerCase().indexOf('safari') > -1);
var bIsOpera = (navigator.userAgent.toLowerCase().indexOf('opera') > -1);
var iCursorX = 0;
var iCursorY = 0;

function isLetter(iChar) {
    return (((iChar >= 64) && (iChar <= 90)) || ((iChar >= 97) && (iChar <= 122)) ||
           ((iChar >= 48) && (iChar <= 57)) || (iChar == 32) || (iChar == 34) || (iChar == 37) ||
           (iChar == 36) || (iChar == 46) || (iChar == 40) || (iChar == 41) || (iChar == 35) ||
           (iChar == 45) || (iChar == 42) || (iChar == 39) || (iChar == 44)) 
}			 
			 
function isInteger(s){
	var i;
	
	if (s == "") return false;
	
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c!=".") && ((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isNumeric(fValue)  {
    var s = fValue;
    
    do {
       s = s.replace(",","");
    } while (s.indexOf(",") > -1)

	if (s == "") {
		return false;
	} else if (parseFloat(s) == s) {
		return true;
	} else {
		return false;
	}
}

function ReplaceAll(s, sOld, sNew) {
    if ((s == null) || (sOld == null) || (sNew == null)) return s;

    do {
        s = s.replace(sOld, sNew);
    } while (s.indexOf(sOld) > -1)

    return s;
}

function RemoveSpecialChars(s) {
    var iPos = s.indexOf("&#");
    var bDone = (iPos < 0);
    var t = "";

    if (bDone) return s;

    do {
        t = t + s.substr(0, iPos - 1);
        iPos = s.indexOf(";");

        if (iPos > -1) s = s.substr(iPos + 1, s.length - iPos);
        
        t = t + s;
        iPos = s.indexOf("&#");
        bDone = (iPos < 0);
    } while (!bDone);

    return t; 
}

function DecodeDelimittedList(sList,sChar) {
	var aReturn = new Array();
  	var i;
	var iIndex = 0;
  	var bDone  = false;

	if (sList != '') {
	    while (!bDone) {
	        i = sList.indexOf(sChar);
			
	        if (i > -1) {
				aReturn[iIndex] = sList.substr(0,i);
				iIndex = iIndex + 1;
				sList  = sList.substr(i+1,sList.length-i);
							            
            	if (sList == '') {
	               	aReturn[iIndex] = '';
				    iIndex = iIndex + 1;
				}
			} else if (sList != '') { 
			    aReturn[iIndex] = sList;
				iIndex = iIndex + 1;
				sList = '';
			}
     
	 		bDone = ((i < 0) || (sList == ''));
		}
	}

   return aReturn;
}

function siif(bCond,s1,s2) {
	if (bCond) {
		return s1;
	} else {
		return s2;
	}
}

function CloseWindow() {
    window.open('', '_self', '');
    window.close();
}

function IsVisible(oControl) {
    if (oControl != null) {
       return (oControl.style.visibility != 'hidden'); 
    } else {
        return false;
    }
}

function RemoveElementFromParent(oControl) {
    if (oControl != null) {
        if (oControl.parentNode != null) {
            oControl.parentNode.removeChild(oControl);
        }
    }
}

function HideElement(sID) {
	var oElement = document.getElementById(sID);
	
	if (oElement != null) {
		oElement.style.visibility = 'hidden';
		//oElement.style.position   = 'relative';
		oElement.style.display = "none";
	}
	
	return oElement;
}

function ShowElement(sID) {
	var oElement = document.getElementById(sID);
	
	if (oElement != null) {
		oElement.style.visibility = 'visible';
		oElement.style.display = "block";
	}
	
	return oElement;
}

function PlaceElement(sID,iTop,iLeft) {
	var oElement = document.getElementById(sID);

	if (oElement != null) {
		oElement.style.position   = 'absolute';
		oElement.style.top        = iTop;
		oElement.style.left       = iLeft;
		oElement.style.visibility = 'visible';
	}
	
	return oElement;
}

function AddElement(sParent,sTag,sName) {
	var oParent  = document.getElementById(sParent);
	var oObject  = document.createElement(sTag);
	oObject.name = sName;
	oObject.id   = sName;

	if (oParent != null) {		
		oParent.appendChild(oObject);			
	}
	
	return oObject;
}

function RemoveElement(sID) {
	var oObject  = document.getElementById(sID);

	if (oObject != null) {
		oObject.parentNode.removeChild(oObject);
	}
	
	return oObject;
}

function SwapElement(sObject,sNew,bShow) {
	var oNew    = document.getElementById(sNew);
	var oObject = document.getElementById(sObject);
	
	if ((oNew != null) && (oObject != null)) {
		oObject.parentNode.removeChild(oObject);
		oNew.appendChild(oObject);
		
		if (bShow) ShowElement(sObject);
		else HideElement(sObject);
	}
	
	return oObject;
}

function SetClassName(sObject,sValue) {
	var oObject = document.getElementById(sObject);
	if (oObject != null) oObject.className = sValue;
	return oObject;
}

function SetBrowserClassName(sPane,sClass) {
    var oPane =  document.getElementById(sPane);
	if (bIsIE) oPane.className = "IE" + sClass;
	else oPane.className = "FF" + sClass;
	return oPane;
}
	
function FormatCurrency(num,bCents) {	
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	return '$'+((sign)?'':'-') + num + siif(bCents,'.'+cents,'');
}

function Dice(iSides,iNum) {
	var iTotal = 0;
	
	for (i=0;i<iNum;i++) {
		iTotal = iTotal + Math.floor(Math.random()*(iSides)) + 1;
	}
	
	return iTotal;
}

function Wait(iMSec)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < iMSec);
} 

function IndexOf(xValue,aArray) {
	var iReturn = -1;
	
	for (i=0;i<aArray.Length;i++) {
		if (aArray[i] == xValue) {
			iReturn = i;
			break;
		}
	}
	
	return iReturn;
}

function Thousands(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	
	//return x1 + x2;
	return x1;
}

function ZeroStr (n,l) {
   var s = n + '';

   while (s.length < l) s = '0' + s;

   return s;
}

function IsOdd(n) {
	var fVal  = n/2;
	var fDiff = fVal - Math.floor(n/2);
	return (fDiff > 0);
}

function SetBackgroundImage(sObject,sImg) {
	var oObject = document.getElementById(sObject);
	
	if (oObject != null) {						
	 	if (bIsIE) oObject.background = sImg;
		else oObject.style.backgroundImage = 'url("' + sImg + '");';
	}
	
	return oObject;
}

function SetSpanString(sSpan,sText) {
	var oControl = document.getElementById(sSpan);
	
	if (oControl != null) {
		oControl.innerHTML = sText;
	}
	
	return oControl;
}

function GetSpanString(sSpan) {
    var oControl = document.getElementById(sSpan);

    if (oControl != null) {
        return oControl.innerHTML;
    }

    return oControl;
}

function SetSelectedIndex(sControl,iIndex) {
	var oControl = document.getElementById(sControl);
	
	if (oControl != null) {
		oControl.selectedIndex = iIndex;
	}
	
	return oControl;
}

function SetSelectByValue(sControl,sValue) {
    var oControl = document.getElementById(sControl);
    
    if (oControl != null) {
        for (i=0;i<oControl.options.length;i++) {
            if (oControl.options[i].value == sValue) {
                oControl.selectedIndex = i;
                return;
            }
        }
    }
}

function GetSelectedValue(sControl) {
    var oControl = document.getElementById(sControl);
    
    if (oControl != null) {
        if (oControl.selectedIndex > -1) return oControl.options[oControl.selectedIndex].value;
        else return "";
    } else return "";
}

function GetValue(sControl) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) return oControl.value;
    else return "";
}

function SetValue(sControl,sValue) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) oControl.value = sValue;
}

function SetChecked(sControl) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) oControl.checked = true;
}

function ClearChecked(sControl) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) oControl.checked = false;
}

function SetFocus(sControl) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) oControl.focus();
}

function GetChecked(sControl) {
    var oControl = document.getElementById(sControl);

    if (oControl != null) return oControl.checked;
    else return false;
}

function SetImageSrc(sControl, sSrc) {
    var oControl = document.getElementById(sControl);
    if (oControl != null) oControl.src = sSrc;
}

function FindByID(iID,aList) {
	var oObject;
	var i;
	
	for (i=0;i<aList.length;i++) {
		oObject = aList[i];
		
		if (oObject.iID == iID) {
			return oObject;
		}
	}
	
	return null;
}

function IsEMailValid(sEMail) {
   return (sEMail.indexOf(".") > 2) && (sEMail.indexOf("@") > 0);
}

function StrToHex(sValue) {
	var sReturn = "";
	var i;
	var iCode;
	
	for (i=0;i<sValue.length;i++) {
		iCode   = sValue.charCodeAt(i);
		sCode   = iCode.toString(16).toLowerCase();
		sReturn = sReturn + siif(sCode.length==1,'0'+sCode,sCode);
	}
	
	return sReturn;	
}

function GetAppName() {
    var iPos = window.location.pathname.lastIndexOf('/');
    var sFile = window.location.pathname.substr(iPos+1);
    iPos = sFile.indexOf('.');
    if (iPos > -1) sFile = sFile.substr(0,iPos);
    return sFile.toLowerCase();
}
	
function trim(sString) {
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }
    
    return sString;
}

function GetValueOfRadioGroup(oForm,sGroup) {
    for(i=0;i<oForm.elements.length;i++) {
        if (oForm.elements[i].type == 'radio') {
            if ((oForm.elements[i].checked) && (oForm.elements[i].name == sGroup)) {
                return oForm.elements[i].id;
            } 
        }       
    }

    return '';  
}

function GetApp() {
    var iPos = window.location.pathname.lastIndexOf('/');
    var sFile = window.location.pathname.substr(iPos+1);
    iPos = sFile.indexOf('.');
    if (iPos > -1) sFile = sFile.substr(0,iPos);
    return sFile.toLowerCase();
}

function copyToClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
	else
	{
		// You have to sign the code to enable this or allow the action in about:config by changing
		user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;

		// create a transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		// specify the data we wish to handle. Plaintext in this case.
		trans.addDataFlavor('text/unicode');

		// To get the data from the transferable we need two new objects
		var str = new Object();
		var len = new Object();

		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

		var copytext=meintext;

		str.data=copytext;

		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

		var clipid=Components.interfaces.nsIClipboard;

		if (!clip) return false;

		clip.setData(trans,null,clipid.kGlobalClipboard);	   
	}
}

function scrollX() {
    return window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ?
        document.documentElement.scrollLeft : document.body.scrollLeft;
}

function scrollY() {
    return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ?
        document.documentElement.scrollTop : document.body.scrollTop;
}

function getCursorXY(e) {
    if ((bIsIE)) {
        var oBody = document.body;
        var oElement = document.documentElement;
        
        iCursorX = event.clientX + (oElement.scrollLeft || oBody.scrollLeft) - (oElement.clientLeft || 0);
        iCursorY = event.clientY + (oElement.scrollTop || oBody.scrollTop) - (oElement.clientTop || 0);
        
    } else {
        iCursorX = e.pageX;
        iCursorY = e.pageY;
    }  

    if (iCursorX < 0){iCursorX = 0}
    if (iCursorY < 0){iCursorY = 0} 
}

function getInternetExplorerVersion() {    
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {        var ua = navigator.userAgent;        
             var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");    
             if (re.exec(ua) != null)   
             rv = parseFloat(RegExp.$1);    }  
return rv;}

function bIsIE8() {
    var iVer = getInternetExplorerVersion();
    return ((iVer >= 8) && (iVer < 9));
}

function getClientWidth() {
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        return window.innerWidth;
    } else if ((document.documentElement) && (document.documentElement.clientWidth)) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientWidth;
    } else if ((document.body) && (document.body.clientWidth)) {
        //IE 4 compatible
        return document.body.clientWidth;
    } else return 0;
}

function GetParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);

    if (results == null) return "";
    else return results[1];
}

function DetectIphone() {
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search("iphone") > -1)
        return true;
    else
        return false;
}

function DetectIpod() {
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search("ipod") > -1)
        return true;
    else
        return false;
}

function DetectIphoneOrIpod() {
    var uagent = navigator.userAgent.toLowerCase();
    if (DetectIphone())
        return true;
    else if (DetectIpod())
        return true;
    else
        return false;
}

function DetectIpad() {
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search("ipad") > -1)
        return true;
    else
        return false;
}

function DetectMobile() {
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search("mobile") > -1)
        return true;
    else
        return false;
}

