
/*****************************************************************************
* Site: J_functions.js
******************************************************************************
* collection of global functions
******************************************************************************
*	'-------------------------------------------------------------------------*
*	' Write By		: yossi Aharoni & ilan mazuz										  *
*	' Company		: Scepia LTD															  *
*	' Date			: Febuary 2001															  *
*	' updated at	:18/06/2001																  *
******************************************************************************
* Contents:
*
* - checkForFlash()    							: for natscape
* - changeLinkColor(obj,mouseOver)			:
* - changeLinkColor(obj,mouseOver)			:
* - cleanCData(str)								: cleans the <![CDATA[]]> tags from a string .
* - checkForm(whatToCheck,theAction)		:
* - checkMultiForm(theForm,theAction)		:
* - checkSearch(url)    :		
* - checkEmail(email)							: email validation
* - doKeyup(this)									: Move to the next TabIndex As needed			
* - divOnOff(div,cancelBubble,on)			:
* - dbg2File(msg, fname)						: print 2 file 'msg'
* - enumForm()										: enumerate the form objects with the "field" prefix + its tab index
* - GetCookie(sName)								: Chack if there is a Cookie and return true/false
* - go(address)									: nevigate to 'address'
* - hasVScroll()									: check for vertical scroll bars
* - hasHScroll()									: check for horisontal scroll bars
* - hidemenus(i)									:
* - img_on_off(imgNum,on)						:
* - imgOnOff(img,on)								:
* - isnull()										: Tests whether the given argument is empty or null
* - iif(q,truePart,falsePart)					: the clasic iif of vb
* - NavRollOver(obj,bgColor,fontColor)		:
* - NavRollOut(obj,bgColor,fontColor)		:
* - nz(param,val)									: if the param is null or 0 return val or "" if val not sopplied
* - openWindow(fName)							: open Window with the specific location
* - replaceBlankChar(str) 						: replace the (chr(10) and the chr(13)) with <br>
* - SetCookie(sName, sValue)					: update a user coocie 
* - setSession(sName, doc_id)					: Cahck if there is asession and set one
* - showHideDiv(obj,dontCancelBubble,colaps,e)								: 
* - trim(str)										: trim string
* - validForm(obj)								: Cahck if Input date are correct
********************************************************************************/
var dbgString = "";//global var for debuging 2 file in the system
var _menusCreated = false;

// initilize the navigator param
var ns = (navigator.appName=="Netscape")? true : false
var ie = (navigator.appName!="Netscape")? true : false

//*******************************************************************

//initilize java var for upper bar-only for home page used
var IsFlashMode=true;

//*******************************************************************
function checkForFlash(){
    var FlashMode = 0;
	 	
    if ((navigator.appName == "Netscape" && navigator.plugins)){
        // Tests for Netscape Navigator (all versions)
        numPlugins = navigator.plugins.length;
        if (numPlugins > 0){
            for (i = 0; i < numPlugins; i++){
                plugin = navigator.plugins[i];
                numTypes = plugin.length;
                for (j = 0; j < numTypes; j++){
                    mimetype = plugin[j];
                    if (mimetype){
                        if (mimetype.enabledPlugin && (mimetype.suffixes.indexOf("swf") != -1))
                            FlashMode = 1;

                        if (navigator.mimeTypes["application/x-shockwave-flash"] == null)
                            FlashMode = 0;
                    }
                 }
             }
         }
     }

    if (FlashMode){
		IsFlashMode=true
    }
    else if (navigator.appName.indexOf("Microsoft") == -1){
        IsFlashMode=false
    }
}
//*******************************************************************

//*******************************************************************
function changeLinkColor(obj,mouseOver) {
    if (ie) {
	    if (mouseOver)
	        obj.style.color = '#3B5D8A';//#ff7800';
	    else
	        obj.style.color = '';
	}
}
//*******************************************************************

/////////////////////////////////////////////////////////////////////////////////////////
// cleans the <![CDATA[]]> tags from a string .
function cleanCData(str){
	if(str.indexOf("<![CDATA[")==0){
		return (str.substring(9,str.length-9-3));
	}
	return str;
}


//*******************************************************************
function imgOnOff(img,on){
imgSrc=img.src
imgSrc = imgSrc.toLowerCase( )
	if(on){
		imageOn=imgSrc.substring(0,imgSrc.indexOf("off.gif")) + "on.gif"
		document.images[img.name].src =imageOn
	}
	else{
		imageOff=imgSrc.substring(0,imgSrc.indexOf("on.gif")) + "off.gif"
		document.images[img.name].src =imageOff
	}
}
//*******************************************************************

//*******************************************************************
//these two functions are to change the bckground of the open div's 
var currentFontColor = "";

function NavRollOver(obj,bgColor,fontColor) {
	currentFontColor = obj.style.color;

	if(ie){obj.style.cursor='hand'}
	if(NavRollOver.arguments.length==1){
		bgColor = "" 
		fontColor = ""
	}
	else{
		if(NavRollOver.arguments.length==2){
			bgColor = bgColor 
			fontColor = ""
		}
		else{
			if(NavRollOver.arguments.length==3){
				bgColor = bgColor 
				fontColor = fontColor
			}
		}
	}		
	 obj.bgColor=bgColor;
	 obj.style.color = fontColor
	 //obj.link.style.color = fontColor
}
		/*-------------------------------*/
//this function gets the color of the unique category to show
function NavRollOut(obj,bgColor,fontColor) {
	if(NavRollOut.arguments.length==1){
		bgColor	 = '' 
		fontColor = currentFontColor;
	}
	else{
		if(NavRollOut.arguments.length==2){
			bgColor	 = bgColor 
			fontColor = currentFontColor
		}
		else{
			if(NavRollOut.arguments.length==3){
				bgColor   = bgColor ;
				fontColor = fontColor;
			}
		}
	}		
	 obj.bgColor     = bgColor;
	 obj.style.color = fontColor
	 status = "Done"
}
//*******************************************************************

//*******************************************************************
function checkForm(whatToCheck,theAction) {
	//check only the sended object in the form
	if (whatToCheck.value=="" || whatToCheck.value=="0") return false;
	window.location.href=theAction + "&" + whatToCheck.name + "=" + whatToCheck.value;
	return false;
}

//*******************************************************************
function checkMultiForm(theForm,theAction) {
	//check all the object in the sended form
	var qString = "";
	for (var i=0;i<theForm.length;i++){
		if(theForm[i].value != "" && theForm[i].value != "0"){
			qString += "&" + theForm[i].name + "=" + theForm[i].value;
		}
	}
	if (qString == "") return false;
	window.location.href=theAction + qString;
	return false;
}

/**********************************************************/
function hidemenus(i){
	status = "Done"
	document.all["divSubmenu"+i].style.visibility="hidden"
}

/**********************************************************/
function img_on_off_old(imgNum,on){
	if(on){
		document["i" + imgNum].src = imgBarPath 
			+ barMenu.menus[imgNum].imgOn.src.substring(barMenu.menus[imgNum].imgOn.src.lastIndexOf("/")+1);
	}
	else{
		if (barMenu.groupSN != barMenu.menus[imgNum].groupSN){
			document["i" + imgNum].src = imgBarPath 
				+ barMenu.menus[imgNum].imgOff.src.substring(barMenu.menus[imgNum].imgOff.src.lastIndexOf("/")+1);
		}
	}
}

/**********************************************************/
function go(address) {
	//window.document.title="yossi"
	window.location=address
}

//*******************************************************************
function divOnOff(div,cancelBubble,on){
	if (cancelBubble) window.event.cancelBubble = true;
	var oDiv = document.all(div)
	if(isnull(oDiv)) return;
	if(on){
		oDiv.style.display = "";
		//alert(oDiv.id)
	}
	else{
		status = "Done"
		oDiv.style.display = "none";
	}
}
//*******************************************************************

///////////////////////////////////////////////////////////////////////////////////////////////////
// Tests whether the given argument is empty or null
function isnull(arg) {
	arg = arg+'';
	return (arg == '' || arg == 'null' || arg == 'undefined');
}
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
function iif(q,truePart,falsePart){
	//the clasic iif of vb
	return (q) ? truePart : falsePart;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
function nz(param,val){
	//if the param is null or 0 return val or "" if val not sopplied
	return(
		isnull(param)
		? ((!isnull(val)) ? val : "") 
		: param
	)
}

///////////////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(email){
	
	var atPos = email.indexOf("@",1)
	var invalidChars = " /:;,"
	var illegal = "Illegal email";
	
	/*if(isnull(email)){ //email cannot be empty
		return("אנא הזן ערך בשדה דואר אלקטרוני.");
	}*/
	
	for(var i=0; i<invalidChars.length; i++){
		badChar = invalidChars.charAt(i)
		if(email.indexOf(badChar,0) != -1){ //email cannot be invalid chars
		
		//	return ("קיים תו בלתי חוקי בתוך כתובת הדואר שלך");
			return (illegal + " because it consist invalid chars.");
		}
	}
	if(email.indexOf("@") != email.lastIndexOf("@")){ //email must be one "@" symbol
	
	//	return("כתובת דואר האלקטרוני שלך מכיל יותר מ@ אחד.");
		return (illegal+" because it has more then one @-symbol");
	}
	
	if(email.indexOf("@") == -1){ //check if there is "@" symbol in the email
		//	return("כתובת דואר אלקטרוני שלך שגוייה אנא הוסף @")
		return (illegal+" because it mast include @-symbol.");
	}
	
	if((email.indexOf("@")) == 0){ //check if the "@" symbol is in the first place
	
		//	return("כתובת דואר שלך שגוייה הנקודה נמצאת במקום בלתי חוקי.");
		return (illegal+" because the dot is displaced.");
	}
	
	if((email.indexOf(".")) == -1){ //check if there is "." symbol in the email
	
		//	return("כתובת דואר האלקטרוני שלך שגוייה אנא הוסף נקודה.")
		return(illegal+" because the dot is absent.")
	}
	
	if((email.indexOf(".")) < atPos){ // check if the "." before the "@" symbol
	
		//	return("הנקודה נמצאת במקום בלתי חוקי בכתובת דואר אלקטרוני שלך.");
		return(illegal+" because the dot situated on invalid place.")
	}
	
	if((email.indexOf(".")) == atPos+1){ // check if the "." before the "@" symbol
	
		//	return("הנקודה נמצאת במקום בלתי חוקי בכתובת דואר אלקטרוני שלך.");
		return(illegal+" because the dot situated on invalid place.")
	}
	
	if(email.lastIndexOf(".") == (email.length -1)){ //check if the "." is the last in the email
	
		//	return("כתובת דואר האלקטרוני שלך שגוייה אנא הזן ערך לאחר הנקודה");
		return(illegal+" because the value after dots is absent.")
	}
	
	var dotCount = 0;
	for(var j=0; j<email.length; j++){
		if(email.substring(j,j+1) == ".") dotCount ++ // check if there isn't more then two dots
		if (dotCount  >2)	{
			//	return("כתובת הדואר האלקטרוני שלך שגוייה היא יכולה להכיל עד שני נקודות");
			return(illegal+" because consist's more then two dots.")
		}
	}
	
	if(email.indexOf("..") != -1){ // check if the isn't two dots in 
	
		//	return("כתובת הדואר אלקטרוני שלך שגוייה אין להזין שתי נקודות ברצף");
		return(illegal+" because consist's two dots going one after another.")
	}

	return true;
	
}

///////////////////////////////////////////////////////////////////////////////////
//check for vertical scroll bars
function hasVScroll(){
	return(screen.availHeight < document.body.scrollHeight)
}

///////////////////////////////////////////////////////////////////////////////////
//check for horisontal scroll bars
function hasHScroll(){
	return(screen.availWidth < document.body.scrollWidth)
}

////////////////////////////////////////////////////////////////////
function replaceBlankChar(str){
	if(isnull(str)) return "";
	str = str.replace(/<p>/gi,"").replace(/<\/p>/gi,"<BR>")
	var str2="";
	for (var i=0;i<str.length;i++){
		//alert(str.charCodeAt(i))
		//debugR(str.charAt(i) + ":" + str.charCodeAt(i))
		if(str.charCodeAt(i)==13 || str.charCodeAt(i)==10){
			str2+= "<BR>";
		}
		else{
			str2+=str.charAt(i);
		}
   }                    
	return(str2);
}

////////////////////////////////////////////////////////////////////
function SetCookie(sName, sValue){
	document.cookie = sName + "=" + escape(sValue) + ";expires=Mon, 31 Dec 2099 23:59:59 UTC;path=/"
}

////////////////////////////////////////////////////////////////////
// enumerate the form objects with the "field" prefix + its tab index
function enumForm() {
	for (var i=0; i < document.forms(0).elements.length; i++)
		document.forms(0).elements(i).id = "field" + document.forms(0).elements(i).tabIndex
	document.all['field1'].focus()
}


////////////////////////////////////////////////////////////////////
//Chack If there is a Cookie
function GetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split(";");

	for (var i=0; i < aCookie.length; i++){
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		
		if (sName == trim(aCrumb[0]))return unescape(aCrumb[1]);
	}

	// a cookie with the requested name does not exist
	return null;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Trim strings. 
// Removes white spaces from beginning and end of a string.
function trim(str)
{
	try {
		return str.replace(/^\s+|\s+$/g,'');
	}
	catch (e) {
		return str;
	}
}


/*****************MAIN*********************/
function changeClass(obj,clas){
	obj.className = clas;
}
		
function fontChange(obj,fontColor) {
	obj.style.color = fontColor
}

var x_time
//*******************************************************************
function showHideDiv(obj,dontCancelBubble,colaps,e){
	var menu = obj;
	
	if(!_menusCreated){
		return
	 }
	if(!ns){
		if (isnull(dontCancelBubble)) window.event.cancelBubble = true;
	}
	
	obj=docObj(obj);

	//alert(obj.style.display)
	showHideDiv.theOpenSideBar = docObj(showHideDiv.theOpenSideBar);

	if(!isnull(showHideDiv.theOpenSideBar) && showHideDiv.theOpenSideBar != obj && colaps){
		var tempObj = showHideDiv.theOpenSideBar;
		showHideDiv.theOpenSideBar = "";
		showHideDiv(tempObj,dontCancelBubble,colaps,e);//hide the open div
	}
	else if(showHideDiv.theOpenSideBar == obj){
		showHideDiv.theOpenSideBar = null;
	}
			
	//the function itself
	if(visibleDivId==obj.id){
		s_flag=false;
		visibleDivId='';	
	}
	else{
		s_flag=true;
		hideMenusDiv();
		
	}
	
	if (obj.style.display == "none" && e.type != "mouseout"){
		if(menu == "menu55" || menu == "menu5"){
			hideElement("SELECT")
		}
		obj.style.display = "";
		if(String(obj.id).indexOf("sideBarDiv")>-1) showHideDiv.theOpenSideBar = obj;
	}
	if (obj.id!=visibleDivId && obj.style.display == "" && (e.type == "mouseout" || e.type=="mouseleave")){
		visibleDivId=obj.id;
		s_flag=true;
		x_time=setTimeout('hideMenusDiv()',10);
	}
}
showHideDiv.theOpenSideBar= null;

var visibleDivId='';
var s_flag=true;

function hideMenusDiv(){

 	try{
	if(s_flag){
		document.getElementById(visibleDivId).style.display = "none";
		showElement("SELECT")
	}
	}catch(e){}
	clearTimeout(x_time)


}
//*******************************************************************

function showHideTag(obj){
	obj = docObj(obj)
	if	(obj.style.display == "none")		obj.style.display = "";
	else 											obj.style.display = "none";
	
}

/*
var visibleDiv = ""

function showDiv(divId){
	if (visibleDiv != divId) {
		if (visibleDiv != "") {
			obj = docObj(visibleDiv);
			if (!isnull(obj)){
				//obj.style.display = "none";
				setTimeout('hideMenusDiv("'+obj.id+'")',1500)
			}
		}
		
		if (divId != "") {
			obj=docObj(divId);
			if (!isnull(obj))
				obj.style.display = "";
		}
		
		visibleDiv = divId
	}
}
*/

/**********************************************************/
function toggleImg(id,src){
	document.images[id].src = src;
}
	
//////////////////////////////////////////////////////////////////////////////////////////////////
//check if the param is an object or string,if string-return refernce to the object with this name
function docObj(obj){
	if(typeof(obj) != "object"){
		return(document.getElementById(obj));
	}
	return obj;
}

	
///////////////////////////////////////////////////////////
//window events to position the menus
window.onload	= menuPostions;
window.onresize=menuPostions;
function menuPostions(){
	try{
		menus.positionMenus();
	}
	catch(e){};
	
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//Set the Survey result



//End Of the result Survey Functions
/////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////
function openWindow(url,param,wName){
	wName = nz(wName,"defWin")
	if(isnull(param)) param = "scrollbars=yes,toolbar=no,resizable=0,left=100,top=100,width=420,height=420"

	var win = window.open(url, wName ,param);
	win.focus();
}

function checkSearch(url) { 	//check only the sended object in the form
	if(isnull(document.forms.searchForm.searchText.value)) return;
	go(url + "?pi=search&search=true&searchText=" + document.searchForm.searchText.value +"&p=1");
}

///////////////////////////////////////////////////////////////////////////////////////////
//Move to the next required Field
function doKeyup (elementParam) {
		
	if (elementParam.value.length < elementParam.size)
		return

	var newElement = elementParam.tabIndex + 1
	if (newElement == elementParam.form.elements.length)
		newElement = 0
	document.all['field'+newElement].focus()
}

///////////////////////////////////////////////////////////////////////////////////////////

function hideElement(elmID){
	for (i = 0; i < document.all.tags(elmID).length; i++)	{
		obj = document.all.tags(elmID)[i];
		if (document.all.tags(elmID)[i].name=="HideThis" 
				|| document.all.tags(elmID)[i].id=="HideThis"
				|| document.all.tags(elmID)[i].hide=="HideThis"){
			//alert(obj.left)
			obj.style.visibility = "hidden";
		}
	}
}

function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}

