﻿// ** Browser Detection **
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
safari = ((navigator.userAgent.indexOf("Safari")!=-1)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
IE6SP2 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)&&(window.navigator.userAgent.indexOf("SV1") != -1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
//NS6 = (!document.layers) && ((navigator.userAgent.indexOf('Netscape')!=-1)||(safari)) ? true : false;
NS6 = document.getElementById&&!document.all ? true : false;

// ********************** SafeOnload *******************
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	}
	else if  (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
		window.onload = f;
}
function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}


// ******************** TabJump ********************
function initTabs(e) {
    if (!loadinitTabs)
        return;
	if (!document.getElementById) return;
	if (!window.event) { window.event = e; }

	var sTempSrc,prevcolor,setfocus,debugtxt;
	var aInputs = document.getElementsByTagName('input');
	var aTextAreas = document.getElementsByTagName('textarea');
	for (var i = 0; i < aTextAreas.length; i++)
	{
		if (aTextAreas[i].disabled == false && aTextAreas[i].style.display == "") 
		{
			if(aTextAreas[i].getAttribute('readonly')!=true && aTextAreas[i].getAttribute('readonly')!='readonly')
			{
				prevcolor = aTextAreas[i].style.borderColor;
				aTextAreas[i].onfocus = function(e){ this.style.borderColor = '#FF0000'; };
				aTextAreas[i].onblur = function(e){ this.style.borderColor = prevcolor; };
				if((!setfocus) && aTextAreas[i].getAttribute('startfocus')!=null)
				{
					setfocus = true; 
					aTextAreas[i].focus();
				};
			}else
				aTextAreas[i].style.background = '#EEEEEE';
		}
	}
	for (var i = 0; i < aInputs.length; i++) {
		if (aInputs[i].type == 'text' || aInputs[i].type == 'password')
			if(!(aInputs[i].disabled==false && aInputs[i].style.display=="" && (aInputs[i].getAttribute('readonly')!=true && aInputs[i].getAttribute('readonly')!='readonly')))
			{	
				//aInputs[i].style.background = '#EEEEEE';
				aInputs[i].tabIndex = -1;
			}else{
				prevcolor = aInputs[i].style.borderColor;
				if(aInputs[i].getAttribute('markrange')!=null)
					aInputs[i].onfocus = function(e){this.style.borderColor='#FF0000';marktextrange(this,0,this.length);};
				else
					aInputs[i].onfocus = function(e){this.style.borderColor='#FF0000';};
				aInputs[i].onblur = function(e){this.style.borderColor=prevcolor;};
				if((!setfocus) && aInputs[i].getAttribute('startfocus')!=null)
					{setfocus=true;aInputs[i].focus();};
				sTempSrc=i;
				if(aInputs[i].getAttribute('jumpret')!=null){
					eval("aInputs[i].addonkeypress=function(e){if(xKey(e)==13 || xKey(e)==9){ document.getElementById(aInputs[" + i + "].getAttribute('jumpret')).focus();PreventDef(e);}}");
					XBrowserAddHandler(aInputs[i],"keypress","addonkeypress");
					continue;
			}
			if(aInputs[i].getAttribute('autoclick')!=null)
				eval("aInputs[i].addonkeypress=function(e){if(xKey(e)==13){ eval(unescape(document.getElementById(aInputs[" + i + "].getAttribute('autoclick')).href));PreventDef(e);}}");
			else
				while(aInputs[++sTempSrc]!=null){
					if(aInputs[sTempSrc].type == 'text' && aInputs[sTempSrc].disabled==false && aInputs[sTempSrc].style.display=="" && aInputs[sTempSrc].getAttribute('readonly')!=true)
					{
						if(aInputs[i].getAttribute('noret')==null)
							eval("aInputs[i].addonkeypress=function(e){if(xKey(e)==13 || xKey(e)==9){ document.getElementsByTagName('input')[" + sTempSrc + "].focus();PreventDef(e);}}");
						else
							eval("aInputs[i].addonkeypress=function(e){if(xKey(e)==13){PreventDef(e);}}");
						break;
					}
				}
			if(aInputs[i].addonkeypress) 
				XBrowserAddHandler(aInputs[i],"keypress","addonkeypress");
		}
	}
}

function textareajump(e,next)
{
	if(xKey(e)==13)
	{
		document.getElementById(next).focus();
		PreventDef(e);
	}
}

function PreventDef(e)
{
	if(e==null)e=event;
	if (e.preventDefault) {
        	 // mozilla
         	e.preventDefault();
     	} else {
		e.returnValue = false;
	}
}

function NumbersOnly(e,validdiv,obj){
	var validnum = '0123456789' + validdiv;
	var keypressed = String.fromCharCode(xKey(e));
	if (validnum.indexOf(keypressed) == -1)
		PreventDef(e);
	if(validdiv.indexOf(keypressed) > -1 && obj!=null)
		for (var i = 0; i < obj.value.length; i++)
			if (validdiv.indexOf(obj.value.charAt(i)) > -1)
					PreventDef(e);
}

function marktextrange(o,istart,iend){
	if (IE4plus) {
 		var oRange = o.createTextRange();
        oRange.moveStart("character", istart);
        oRange.moveEnd("character", -o.value.length + iend);
        oRange.select();
    } else if (NS6){
       	o.setSelectionRange(istart, iend);
    }
    o.focus();
}

function XBrowserAddHandler(target,eventName,handlerName) {
  if ( target.addEventListener ) {
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) {
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else {
    var originalHandler = target["on" + eventName];
    if ( originalHandler ) {
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);};
    } else {
      target["on" + eventName] = target[handlerName];
    }
  }
}

var GoesUp = false;
function FirstUp(obj,e){
	var mykey = xKey(e);
	var tmpLen = obj.value.length;
	if (tmpLen==0||GoesUp==true){
		PreventDef(e);
		obj.value += String.fromCharCode(mykey).toUpperCase();
	}
	if (mykey==32)
		GoesUp = true;
	else
		GoesUp = false;
}

function xKey(e) {
	eventChooser = (document.all) ? event.keyCode : e.which;
	which = String.fromCharCode(eventChooser);
	return which.charCodeAt(0);
}

var loadinitTabs = true;
SafeAddOnload(initTabs);

// ==========  zoom in/out  ==============
function zoominout(zin){
	if(!IE6) return;
	if(zin){
		if (document.body.style.zoom<0.01)
			document.body.style.zoom = 1.1;
		else if (document.body.style.zoom<1.3)
			document.body.style.zoom = document.body.style.zoom*1 + 0.1;
	}
	else
		if(document.body.style.zoom!=0 & document.body.style.zoom>1) 
		document.body.style.zoom-=0.1; else document.body.style.zoom=1;
}

function hotkey(e){
	//alert( xKey(e));
	switch(xKey(e)){
		case 29:
			zoominout(true);
			break;
		case 27:
			zoominout(false);
			break;
		case 93:
			if(IE4plus) window.status = event.clientX + "|" + event.clientY;
		break;
	}
}

document.onkeypress = hotkey;


//
// *********  common function  *********
//
function __getElement(elementID)
{
    return document.getElementById?document.getElementById(elementID):document.all[elementID];
}
function __hiddenElement(item)
{
    if(typeof(item)=='string')
        item = __getElement(item);
    if(item.style.display!='none')
        item.style.display='none';
}
function __showElement(item)
{
    if(typeof(item)=='string')
        item = __getElement(item);
    if(item.style.display!='')
        item.style.display='';
}

function __hideSELECT(){
    var sels = document.getElementsByTagName('SELECT');
	for (var i=0 ; i < sels.length ; i++){
		sels[i].style.visibility="hidden";
	}
}
function __showSELECT(){
    var sels = document.getElementsByTagName('SELECT');
	for (var i=0 ; i < sels.length ; i++){
		sels[i].style.visibility="visible";
	}
}    

function AddFavourite(){ 
    url = "http://www.RafflesEdu.cn/CPD/"; //你自己的主页地址 
    title = "RafflesCPD"; //你自己的主页名称 
    window.external.AddFavorite(url,title); 
}