/*
	File Name: common.js
	Author: David Zhenwei Guo, Kamal
	Date: Mar 20, 2006, Created the file for Gandy system
	Desc: All javascription functions for sharing with different components
	Modifications:
		- Mar 20, 2006, created this file

*/


//Specify highlight behavior. "TD" to highlight table cells, "TR" to highlight the entire row:
var highlightbehavior="TD"

var ns6=document.getElementById&&!document.all
var ie=document.all

//---------------------------------
function changeto(e,highlightcolor){
    source=ie? event.srcElement : e.target
    if (source.tagName=="TABLE")
    return
    while(source.tagName!=highlightbehavior && source.tagName!="HTML")
    source=ns6? source.parentNode : source.parentElement
    if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore")
    source.style.backgroundColor=highlightcolor
}
//---------------------------------
function contains_ns6(master, slave) { //check if slave is contained by master
    while (slave.parentNode)
    if ((slave = slave.parentNode) == master)
    return true;
    return false;
}

//---------------------------------
function changeback(e,originalcolor){
    if (ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")||source.tagName=="TABLE")
    return
    else if (ns6&&(contains_ns6(source, e.relatedTarget)||source.id=="ignore"))
    return
    if (ie&&event.toElement!=source||ns6&&e.relatedTarget!=source)
    source.style.backgroundColor=originalcolor
}


var type = "IE";    //Variable used to hold the browser name
//---------------------------------
//detects the capabilities of the browser
function BrowserSniffer() {
    if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";     //Opera
    else if (document.all) type="IE";                                                       //Internet Explorer e.g. IE4 upwards
    else if (document.layers) type="NN";                                                    //Netscape Communicator 4
    else if (!document.all && document.getElementById) type="MO";                           //Mozila e.g. Netscape 6 upwards
    else type = "IE";       //I assume it will not get here
}


//---------------------------------
function ChangeMessage(id, str) {
	/* 
	   	1. Level 1 DOM (Mozilla, Explorer 5+, Opera 5+, Konqueror, Safari, iCab, Ice, OmniWeb 4.5)
   		2. document.all (Explorer 4+, Opera 6+, iCab, Ice, Omniweb 4.2-)
   		3. document.layers (Netscape 4, Ice, Escape, Omniweb 4.2-) 
	*/
    if (type=="IE") { //IE 4
		//alert("IE");
        document.all[id].innerHTML = str;
		//alert("after str writing re=" + str);
		
    }
    if (type=="NN") { 
        document.layers[id].document.open();
        document.layers[id].document.write(str);
        document.layers[id].document.close();
		//alert("NN");
    }
    if (type=="MO" || type=="OP") {
        document.getElementById(id).innerHTML = str;
		//alert("MO and OP");
    }
	//alert("end of ChangeMessage() function");
	//return re;
}       




//---------------------------------
function ShowIt(current, num) {			
	if (type=="IE") {
		ms=document.all[current];
		ms.style.visibility = "visible";				
	}
	if (type=="NN") {
		ms=document.layers[current];
		ms.visibility = "visible";				
	}
	if (type=="MO" || type=="OP") {
		ms=document.getElementById(current);
		ms.style.visibility = "visible";				
	}	
}


//---------------------------------
function HideIt(current) {
	if (type=="IE") {
		ms=document.all[current];		
		ms.style.visibility = "hidden";
	}
	if (type=="NN") {
		ms=document.layers[current];
		ms.visibility = "hidden";
	}
	if (type=="MO" || type=="OP") {
		ms=document.getElementById(current);
		ms.style.visibility = "hidden";
	}		
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Javascript Validations


//---------------------------------
// trim leading and trailing white space globally
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};



//---------------------------------
function validateEmail(email)
{
	var re = false;
	//trim off leading and trailing white space
	var ea = trim(email);
	// Declare the regular expression for e-mail validation
	// var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.[a-zA-Z]{2,3})+$/; 
	var pattern = /^[a-zA-Z][\w\-\.]{0,45}\@[a-zA-Z0-9]\w+(\.[a-zA-Z]\w+)+$/;
	// ^ : begining with
	// \w: a single letters, numbers and underscore
	// + : One or more occurences of previuos item.
	// (): Forms a group
	// [] : Any one of the characters
	// ? : zero or one occurance of previous item
	// * : Zero or more occurances of previous item
	// \ : To escape a special charcter
	// {n1,n2, .. }) : n1 or n2 occurances of previous item 
	// \s : One white space character
    if (ea != "" && pattern.test(ea)) {
    	re = true;
    }
    return re;
}


//---------------------------------
function validateEmpty(nm){
	var name = trim(nm);
	return name==""? false : true;
}


//---------------------------------
function validatePhone(num){
	var re = false;
	var n = trim(num);
	var pattern = /^\(?\d{3}\)?\s?\d{3}[\s-]?\d{4}$/;
	if(!n=="" && pattern.test(n)){
		re = true;
	}
	return re;
	
}


//--------------------------------
function validatePostcode(pcode, country){
	var re = false;
	var pc = trim(pcode);
	var c = trim(country);
	
	var pattern_ca = /^[a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d$/;
	var pattern_us = /^\d{5}(-\d{4})?$/;
	if(c =="Canada"){
		re = pattern_ca.test(pc);
	}else{
		re = pattern_us.test(pc);
	}
	return re;
}


//---------------------------------
function validateTime(t){
	var re = false;
	var pattern = /^\d?\d:\d\d([aA]|[pP])[mM]$/;
	if(pattern.test(t)){
		re = true;
	}
	return re;
}



//----------------------------------
function isDigits(d){
	var re = false;
	var pattern = /^[1-9]\d*$/;
	if(pattern.test(d)){
		re = true;
	}
	return re;
}	



function validsub() {
	
//---------------------------------
	function validateEmpty(sn_name){
	var name = trim(sn_name);
	return name==""? false : true;
}

//---------------------------------
	function validateEmail(sn_email)
	{
		var re = false;
		//trim off leading and trailing white space
		var ea = trim(sn_email);
		// Declare the regular expression for e-mail validation
		// var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.[a-zA-Z]{2,3})+$/; 
		var pattern = /^[a-zA-Z][\w\-\.]{0,45}\@[a-zA-Z0-9]\w+(\.[a-zA-Z]\w+)+$/;
		// ^ : begining with
		// \w: a single letters, numbers and underscore
		// + : One or more occurences of previuos item.
		// (): Forms a group
		// [] : Any one of the characters
		// ? : zero or one occurance of previous item
		// * : Zero or more occurances of previous item
		// \ : To escape a special charcter
		// {n1,n2, .. }) : n1 or n2 occurances of previous item 
		// \s : One white space character
	    if (ea != "" && pattern.test(ea)) {
	    	re = true;
	    }
    return re;
	}

}

