﻿/***************************************************************************
@doc           /includes/global/incUtility.js
@desc          Common miscellaneous page functions
****************************************************************************
Amendment History:
==================

20070619LM
- By Leo Mok
- Add AJAX Search Feature for Contributors' Award Search

****************************************************************************
NO NEED TO INCLUDE THIS FILE DIRECTLY
****************************************************************************
|   MM_swapImgRestore()
|
|   MM_preloadImages()
|
|	MM_findObj(n,d)
|
|	MM_swapImage()
|
|	openWindow(strName,strURL,strWidth,strHeight)
|
|	openOffsite(strURL)
|
|	openUS(strURL)
|
|	popuponclick()
|
|	isNumeric(sText)
|
***************************************************************************/

// 20070619LM starts

var xmlHttp

// 20070619LM ends

function MM_swapImgRestore() { //v3.0
		  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
		}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openWindow(strName,strURL,strWidth,strHeight) {
	var objWindow = window.open(strURL,strName,'width='+strWidth+',height='+strHeight+',toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
	objWindow.focus();
}

function openOffsite(strURL) {
	openWindow('offsite','/kids/exit.asp?URL='+strURL,800,600);
}

function openUS(strURL) {
	openWindow('Scholastic_US','/kids/exit_ScholasticUS.asp?URL='+strURL,800,600);
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=660,height=600,left = 182,top = 84');");
}

function openNoMessage(strURL) {
	openWindow('offsite',strURL,strURL,800,600);
}

//var objPopUpWindow=0;
function popUpWin(URLStr, left, top, width, height) {
	//if (objPopUpWindow)
	//{
	//	if (!objPopUpWindow.closed)
	//	{
	//		objPopUpWindow.close();
	//	}
	//}
	
	var objPopUpWindow = open(URLStr, 'objPopUpWindow', 'toolbar=yes,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	objPopUpWindow.focus();
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours) {
	var expire = "";
	if (hours != null)
	{
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}

// Example:
// alert( readCookie("myCookie") );
function readCookie(name) {
	var cookieValue = "";
	var search = name + "=";
	if (document.cookie.length > 0)
	{ 
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			{
				end = document.cookie.length;
			}
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

function goToURL(sTarget, sURL) {
	eval (sTarget + ".location='" + sURL + "'");
}

function trim(s) {
	var len = s.length;
	var begin = 0, end = len-1;
	while(s.charAt(begin) == " " && begin < len) begin++;
	while(s.charAt(end) == " " && begin < end) end--;
	return s.substring(begin, end+1);
}

function isNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

// 20070619LM starts

function showSearchResult(searchProgram, searchString)
{ 
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
	} 

	var url=searchProgram;

	url=url+"&SearchString="+searchString;

	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
	document.getElementById("txtResult").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject()
{
	var xmlHttpTmp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttpTmp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttpTmp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttpTmp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttpTmp;
}

// 20070619LM ends