/* 

The function validate expects 2 arguments . The first if frm => The form object which is to be validated 
The second is an array validate_data_arr : 
Each element of the array validate_data_arr is an array of the form 
'name_of_input_typ_in_form , 'name_of_js_library_function_to_be_called' , 'error_message_if_called_function-fails' , 'Optionally any munber of additional arguments expected the the function bein caled can be passed '' 

Eg.	var validate_data_arr=new Array(); 
validate_data_arr[validate_data_arr.length]= new Array("table[name]", "isEmpty","Please fill in your  user name"); 
validate_data_arr[validate_data_arr.length]= new Array("table[name]", "checkLength","Username should be between of 4 & 16 characters ",4,16); 
TBD : Can be Enhanced such that all the messages of one field can be clubbed together and shown. 


*/ 

// This function used the array validata to validate fields in the form frm "\n"

function validate(form,validate_data_arr){ 

var errs = new Array(); 
var focusobj; 
var frm = 'document.'+form.name;

for(var i =0 ; i < validate_data_arr.length ; i++){
	var cur_validation = validate_data_arr[i]; 
	var obj = eval(frm+"['"+cur_validation[0]+"']"); 
	var additional_args = "";
	for(var j = 3 ; j < cur_validation.length;j++){ 
		additional_args += ","+cur_validation[j]; 
	} 
	additional_args +=")"; 
	var func_to_invoke = cur_validation[1]+'(obj'+additional_args; 
	var result =  eval(func_to_invoke);
	if(!result){ 
		if(errs.length==0) focusobj =obj; 
		errs +=cur_validation[2]+"\n"; 
	}	 
}
if(errs.length > 0){ 

	alert(errs); 
	if(focusobj.tab){
		showTab(focusobj['tab'])
		//alert(focusobj.tab)
	}

	if(focusobj && focusobj.type != undefined){ 
		focusobj.focus(); 
	}else if(focusobj.type == undefined) 
		focusobj[0].focus();
	return false; 
} 
return true; 
}

function showTab (show) {
	if (! document.getElementById) { return true; }
	if (typeof(vartabs) != "undefined"){
		for(var i in vartabs){
			var div =  document.getElementById(vartabs[i]);
			if(div) {
				div.style.display = ((vartabs[i] == show ) ? "block"  : "none" );
				if(show == 'newsletter'){
					var division =  document.getElementById('newsletter');
					division.style.display = "block";
					div.style.display = "none";	
				}else{
					var division1 = document.getElementById('newsletter');
					if(division1) division1.style.display = "none";
				}
			//alert(show +'-'+ div.style.display);	
			}	
		}
	}
	return true;
}



function isEmpty(obj){	//This function checks to see if value has been provided 
	//alert("in function isEmpty Name "+obj.name + "len  "+obj.length + "value   "+obj.value)
	if(!obj.value)	{ // This will trap most controls 
			// check if is a field with multiple controls ie  a radio button  or a check box 
			if(obj.length){ // Yes it is 
				for(var i = 0 ; i < obj.length ;i++){
					if(obj[i].checked) return true;
				}
			}
			return false;
	}
	return true;
}



function checkValidemail(obj){
	if(!obj.value){
		return true;
	}else{
	if(obj.value.search(/^\w+(\.\w+)*@\w+(\.\w+)*\.\w{2,3}$/) == -1) return false;
	return true;
	}
}

function checkEmail(obj){
	if(obj.value.search(/^\w+(\.\w+)*@\w+(\.\w+)*\.\w{2,3}$/) == -1) return false;
	return true;
}



// JAVA Script Picked up from Web Calendar to hid and display divs
// The following code is used to support the small popups that
// give the full description of an event when the user move the
// mouse over it.
// Thanks to Klaus Knopper (www.knoppix.com) for this script.
// It has been modified to work with the existing WebCalendar
// architecture on 02/25/2005
//
// 03/05/2005 Prevent popup from going off screen by setting
// maximum width, which is cnfigurable
//
// Bubblehelp infoboxes, (C) 2002 Klaus Knopper <infobox@knopper.net>
// You can copy/modify and distribute this code under the conditions
// of the GNU GENERAL PUBLIC LICENSE Version 2.
//
var ns4            // Are we using Netscape4?
var ie4            // Are we using Internet Explorer Version 4?
var ie5            // Are we using Internet Explorer Version 5 and up?
var kon            // Are we using KDE Konqueror?
var x,y,winW,winH  // Current help position and main window size
var idiv=null      // Pointer to infodiv container
var px="px"        // position suffix with "px" in some cases
var popupW         // width of popup
var popupH         // height of popup
var xoffset = 8    // popup distance from cursor x coordinate
var yoffset = 12   // popup distance from cursor y coordinate
var followMe = 1   // allow popup to follow cursor...turn off for better performance
var maxwidth = 300 // maximum width of popup window

function nsfix(){setTimeout("window.onresize = rebrowse", 2000);}

function rebrowse(){window.location.reload();}

function infoinit(){
  ns4=(document.layers)?true:false, ie4=(document.all)?true:false;
  ie5=((ie4)&&((navigator.userAgent.indexOf('MSIE 5')>0)||(navigator.userAgent.indexOf('MSIE 6')>0)))?true:false;
  kon=(navigator.userAgent.indexOf('konqueror')>0)?true:false;
  x=0;y=0;winW=800;winH=600;
  idiv=null;
  if (followMe) {
    document.onmousemove = mousemove;
    if(ns4&&document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
  }
  // Workaround for just another netscape bug: Fix browser confusion on resize
  // obviously conqueror has a similar problem :-(
  if(ns4||kon){ nsfix() }
  if(ns4) { px=""; }
}

function hide(name){
  idiv.visibility=ns4?"hide":"hidden";
  idiv=null;
}

function gettip(name){return (document.layers&&document.layers[name])?document.layers[name]:(document.all&&document.all[name]&&document.all[name].style)?document.all[name].style:document[name]?document[name]:(document.getElementById(name)?document.getElementById(name).style:0);}

function show(evt, name){
  if(idiv) hide(name);
  idiv=gettip(name);
  if(idiv){
   scrollX =0; scrollY=0;
   winW=(window.innerWidth)? window.innerWidth+window.pageXOffset-16:document.body.offsetWidth-20;
   winH=(window.innerHeight)?window.innerHeight+window.pageYOffset  :document.body.offsetHeight;
   scrollX=(typeof window.pageXOffset == "number")? window.pageXOffset:(document.documentElement && document.documentElement.scrollLeft)?document.documentElement.scrollLeft:(document.body && document.body.scrollLeft)?document.body.scrollLeft:window.scrollX;
   scrollY=(typeof window.pageYOffset == "number")? window.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:(document.body && document.body.scrollTop)?document.body.scrollTop:window.scrollY;
   popupW = document.getElementById(name).offsetWidth;
   popupH = document.getElementById(name).offsetHeight;

   showtip(evt);
  }
}

function showtip(e){
  e = e? e: window.event;
  if(idiv) {
    if(e)   {
      x=e.pageX?e.pageX:e.clientX?e.clientX + scrollX:0;
      y=e.pageY?e.pageY:e.clientY?e.clientY + scrollY:0;
    }
    else {
      x=0; y=0;
    }
    // MAke sure we don't go off screen
    if ( popupW > maxwidth ) {
      popupW = maxwidth;
      idiv.width = maxwidth + px;
    }
    idiv.left=(((x + popupW + xoffset)>winW)?x - popupW - xoffset:x + xoffset)+px;
    if ((popupH + yoffset)>winH) {
      idiv.top= yoffset + px;
    } else {
      idiv.top=(((y + popupH + yoffset)>winH)?winH - popupH - yoffset:y + yoffset)+px;
    }
    idiv.visibility=ns4?"show":"visible";
    }

}

function mousemove(e){
  showtip(e);
}
// Initialize after loading the page
window.onload=infoinit;

function show_div(item)
{
item=document.getElementById(item);
if (!item)
{
return;
}
if(item.style.display != "none"){
        item.style.display = "none";
        }else{
        item.style.display = ""
        }
}
function showthisTab(tab){

//		tab = document.getElementById(tab);
		for(i=1;i<4;i++){
			obj = document.getElementById(i);
			if(obj.style.display == 'block'){
				obj.style.display = 'none';
				break;
			}
		}
		obj = document.getElementById(tab);
		obj.style.display = 'block';
	
}

