// JavaScript Document
/******************************************/
/*****        By : Terence Toh        *****/
/******************************************/

// Variable Declaration

months = new Array('January','February','March','April','May','June','July','August','September','October','November','December')
smonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')


// General Functions

function writeDateFull(){
now = new Date()
document.write(days[now.getDay()]+", "+now.getDate()+" "+months[now.getMonth()]+" "+now.getFullYear())
}

function goURL(g) {if(g.options[g.selectedIndex].value) {document.location=g.options[g.selectedIndex].value}}


// Form Functions

function isBlank(input){
if(!input.value) {return true}
var inputStr=input.value
if(window.RegExp && inputStr){
	var regExp1 = /\S/
	if(!regExp1.test(inputStr)) {input.value="";return true}; return false
}}

function isEmail(input){
var inputStr=input.value
if(window.RegExp && inputStr){
	var regstr1 = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"
	var regstr2 = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
	var regExp1 = new RegExp(regstr1)
	var regExp2 = new RegExp(regstr2)
	var regExp3 = /[^a-zA-Z0-9\-_.@]/
	if(!(!regExp1.test(inputStr) && regExp2.test(inputStr) && !regExp3.test(inputStr)) || !(inputStr.indexOf("@")>=0)) {alert("Invalid entry.\n\nPlease enter a valid Email Address.");input.value=input.defaultValue;input.focus();return false}; return true
}}

function chkMaxLen(input,max,count){
var inputStr=input.value
if(max && inputStr.length>max) {alert("Your message has exceeded the maximum length of "+max+" characters.\n\nPlease shorten your entry.");input.value=inputStr.substring(0,max);input.focus()}
if(count) {count.value=input.value.length;input.blur();input.focus()}
}

function chkSelect(input){
if(!input.length) {if(input.checked) return true}
else {for(i=0;i<input.length;i++) {if(input[i].checked) return true}}
return false
}

function writeDateList(dd){
for(i=1; i<=31; i++){
	if(i==parseInt(dd,10)) {document.write("<option value="+insZero(i,2)+" selected>"+insZero(i,2))}
	else {document.write("<option value="+insZero(i,2)+">"+insZero(i,2))}
}}
function writeMonthList(mm){
for(i=1; i<=12; i++){
	if(i==parseInt(mm,10)) {document.write("<option value="+insZero(i,2)+" selected>"+smonths[i-1])}
	else {document.write("<option value="+insZero(i,2)+">"+smonths[i-1])}
}}
function insZero(num,sf){
var inputStr=num.toString()
while(inputStr.length<sf) {inputStr="0"+inputStr}
return inputStr
}

// Google Search in SingNet MAIN PAGE
var MAX_HOMEWIZ=42;
var HOMEWIZ_STRING="hm+wz";


function MakeArray(n)
{
  for(var i=1;i<=n;i++)
  {
    this[i]=0;
  }
  this.maxlen=n;
  this.len=0;
  return this;
}

var hwiz=new MakeArray(MAX_HOMEWIZ);

function find_substring(wiz,home)
{
  var i,wizn=wiz.length,homelen=home.length;
  for(i=0;i<=homelen-wizn;i++)
  {
    if(wiz==home.substring(i,i+wizn))return i;
  }
  return false;
}

function Homewiz(name,opts,home,search)
{
  var ehgo=find_substring(HOMEWIZ_STRING,search);
  this.name=name;
  this.opts=opts;
  this.home=home;
  this.pre_ehgo=search.substring(0,ehgo);
  this.post_ehgo=search.substring(ehgo+HOMEWIZ_STRING.length,search.length);
}

function Add(name,opts,home,search)
{
  hwiz.len++;
  if(hwiz.len<=hwiz.maxlen)
  {
    hwiz[hwiz.len]=new Homewiz(name,opts,home,search)
  }
  else
  {
    alert("Add")
  }
}

function DisplayForm()
{

  document.writeln('<TR>');
  document.writeln('<TD>');
  document.writeln('<FORM name=HWform OnSubmit="HandleForm(this);return false">');
  document.writeln("<table border='0' cellpadding='0' cellspacing='5' width='100%' align='center'>");
  document.writeln("<tr valign='top'><td colspan='2' class='nav'><B>Search the Web</B></td></tr>");
  document.writeln('<TR valign="TOP">');
  document.writeln('<TD colspan="2"><input size=14 name=query></TD>');
  document.writeln('</TR>');
  document.writeln('<TR><TD width="50%">');
  document.writeln('<span class=infosearch><select name=service>');
  for (i=1;i<=hwiz.len;i++)
  {
    document.writeln("<option "+hwiz[i].opts+"> "+hwiz[i].name);
  }
  document.writeln('</select></span>');
  document.writeln('</TD>');
  document.writeln('<TD align="left"><input type="image" border="0" value="Submit Search" name="go" src="http://home.singtel.com/include_media_hub/bn_enter.gif" width="21" height="17"></TD>');
  document.writeln('</TR>');
  document.writeln('</table></FORM>');
  
  document.writeln('</TD>');
  document.writeln('</TR>');

  query=self.location.search;
  if (query != null && query !='')
  {
    if (query.length > 1)
	{
	  query = query.substring(1, query.length);
	  document.HWform.query.value=query
    }
  }
}

function HandleForm(form)
{
  var i,newq="",oldq=form.query.value;
  for ( i=0; i < oldq.length; i++ )
    {
      var thischar=oldq.charAt(i);
	  if(thischar != ' ')newq += thischar;
	  else if(lastchar != ' ')newq += '+';
	       lastchar=thischar;
    }

    var hw=hwiz[1+form.service.selectedIndex];
    tmp = newq ? hw.pre_ehgo+newq+hw.post_ehgo : hw.home;
    window.open(tmp,'new');
}

Add("Google","","http://google.com","http://google.com/search?q=hm+wz");
Add("Altavista","","http://www.altavista.com","http://www.altavista.com/cgi-bin/query?q=hm+wz&text=yes");
Add("Go.com","","http://www.go.com","http://www.goto.com/d/search/p/go/?Partner=go_home&Keywords=hm+wz");
Add("GoTo.com","","http://goto.com","http://www.goto.com/d/search/?type=topbar&Keywords=hm+wz");
Add("Looksmart","","http://www.looksmart.com","http://www.looksmart.com/r_search?key=hm+wz");
Add("MSN","","http://www.msn.com","http://search.msn.com/results.asp?RS=CHECKED&FORM=MSNH&v=1&q=hm+wz");
Add("Search.com","","http://www.search.com","http://www.search.com/search?channel=1&tag=st.se.fd..sch&q==hm+wz");
Add("Webcrawler","","http://www.webcrawler.com","http://search.excite.com/search.gw?c=web&lk=webcrawler&onload=&s=hm+wz");
Add("Yahoo","","http://sg.yahoo.com","http://search.sg.yahoo.com/search/sg?p=hm+wz");
Add("Yahooligans","","http://www.yahooligans.com","http://search.yahooligans.com/search/ligans?p=hm+wz");


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 open_window_sitemap (path){
   window.open(path, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,width=510,height=400');
}


function Browser() {
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
	this.isOP = true;
	this.version = parseFloat(ua.substr(i + s.length));
	return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
	this.isNS = true;
	this.version = parseFloat(ua.substr(i + s.length));
	return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
	this.isNS = true;
	this.version = 6.1;
	return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
	this.isIE = true;
	this.version = parseFloat(ua.substr(i + s.length));
	return;
  }
}

 var newwin;
 function launchwin(winurl,winname,winfeatures) {
	var width = 800;
    var height = 620;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;
    newwin = window.open(winurl+"?nc=3", winname, windowFeatures); 

	setTimeout('newwin.focus();',250); 
 }
