/******** Lifeaxis.com CLIENT SIDE DHTML HELPERS & ROUTINES ***************************
	libsrc.js: Misc. utilities - include this script in all DHTML (non-static) pages.
	Methods:
		methods to handle cookies & DHTML components.
***************************************************************************************/


var True  = true;
var False = false;
var TRUE  = true;
var FALSE = false;
var newLineChar = String.fromCharCode(10);
var char10 = String.fromCharCode(10);
var char34 = String.fromCharCode(34);

var imgLookupInsert = new Image(); 
imgLookupInsert.src = "../images/iclipinsert.gif";  
var imgLookupClear  = new Image(); 
imgLookupClear.src  = "../images/iclipclear.gif";  

function _P(strText)
{
document.writeln(strText);
}

function setCookie(name, value, expire)
{
document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function saveCookie(name, value)
{
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*30)
setCookie(name, value, expires)
}

function getCookie(Name)
{
var search = Name + "=";
var cookieVal=null;
if (document.cookie.length > 0) {
	offset = document.cookie.indexOf(search);
	if (offset != -1) { 
		offset += search.length;
		end = document.cookie.indexOf(";", offset);
		if (end == -1)
			end = document.cookie.length;
		cookieVal = document.cookie.substring(offset, end);
		if (cookieVal!=null) cookieVal = unescape(cookieVal);
	}
}
return(cookieVal);
}

function registerCookie(name, value)
{
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(name, value, expires)
}

function clearFields(frmName, elemNames, elemIndex, readOnlyNames)
{
var objFrm = document.forms[frmName];
if (objFrm==null) return;

if (elemIndex==null) elemIndex=0;

var arElemName  = elemNames.split("|");

var elemName, elemValue="";

for (var i=0;i<arElemName.length;i++) {
	elemName  = arElemName[i];
	if (elemName==null) break;
	SetElement(objFrm, elemName, elemIndex, elemValue);
}

if (readOnlyNames!=null) {
	var arReadOnlyName = readOnlyNames.split("|");

	for (var i=0;i<arReadOnlyName.length;i++) {
		elemName  = arReadOnlyName[i];
		if (elemName==null) break;
		SetElementProperty(objFrm, elemName, elemIndex, null, true);
	}
}

}


function pasteLookup(imgObj, lookupName, frmName, elemNames, elemIndex, readOnlyNames)
{
var bClearFields=false;
if ((imgObj!=null)&&(imgObj.src.indexOf("clipclear")>=0)) bClearFields=true;

if ((document.images)&&(imgObj!=null)&&(bClearFields==true)) {
	clearFields(frmName, elemNames, elemIndex, readOnlyNames);
	imgObj.src = imgLookupInsert.src;
	imgObj.alt = "insert lookup values";
	return;
}

var objFrame = window.frames["frameLookup"+lookupName];
if (objFrame==null) return;
var objFrmLookup = objFrame.document.forms["frmLookup"];
if (objFrmLookup==null) return;
var objElem = objFrmLookup.elements["lookup_list"];
if ((objElem==null)||(objElem.options.length<=0)||(objElem.value=="")) return;

var objFrm = document.forms[frmName];
if (objFrm==null) return;

if (elemIndex==null) elemIndex=0;

var arElemName  = elemNames.split("|");
var arElemValue = objElem.value.split("|");

var elemName, elemValue;

for (var i=0;i<arElemName.length;i++) {
	elemName  = arElemName[i];
	elemValue = arElemValue[i];
	if ((elemName==null)||(elemValue==null)) break;
	SetElement(objFrm, elemName, elemIndex, elemValue);
}

if (readOnlyNames!=null) {
	var arReadOnlyName = readOnlyNames.split("|");

	for (var i=0;i<arReadOnlyName.length;i++) {
		elemName  = arReadOnlyName[i];
		if (elemName==null) break;
		SetElementProperty(objFrm, elemName, elemIndex, null, false);
	}
}

if ((document.images)&&(imgObj!=null)) {
	imgObj.src = imgLookupClear.src;
	imgObj.alt = "clear field values";
}

}

function isEmpty(sText)
{
if ((sText=="")) return(true);
else return(false);
}

function checkField(objElem, sFieldName)
{
if (sFieldName==null) sFieldName="(" + objElem.name + ")";
if (isEmpty(objElem.value)) {
	alert("Please enter value in '" + sFieldName + "' field!");
	return(true);
} else return(false);
}

function checkEmailField(objElem, sFieldName)
{
if (sFieldName==null) sFieldName="(" + objElem.name + ")";
if (objElem.value.indexOf("@") <0) {
	alert("Please enter valid email id in '" + sFieldName + "' field!");
	return(true);
} else return(false);
}


function SetElement(objFrm, elemName, elemIndex, elemValue, elemClass, bReadOnly, bError)
{
if (objFrm==null) return;
var objElem = objFrm.elements[elemName];
if (objElem==null) return;

var objLen = objElem.length;

if ((objLen>1)&&((objElem[0].type=="radio")||(objElem[0].type=="checkbox"))) {
	for (var i = 0; i < objElem.length; i++) {
		if (objElem[i].value==elemValue) objElem[i].checked = true;
		if (bReadOnly==true) {
			objElem[i].tabIndex=-1;
		} 
	}
	return;
}

if ((objLen>1)&&(objElem.type==null)) {
	if (elemIndex==null) elemIndex=0;
	objElem = objFrm.elements[elemName][elemIndex];
	if (objElem==null) return;
	objLen = objElem.length;
}

if ((objLen>1)&&(objElem.type=="select-one")) {
	var bFound=false;
	var objList=objElem;
	for (var i = 0; i < objList.options.length; i++) {
		if (objList.options[i].value==elemValue) {
			objList.options[i].selected = true;
			bFound=true;
		}
	}
	if (bFound==false) {
		objList.options[0].selected = true;
	}
	if (bReadOnly==true) {
		objElem.tabIndex=-1;
	} 
	return;
}


if (objElem.type=="checkbox") {
	if (objElem.value==elemValue) objElem.checked = true;
	else objElem.checked = false;
	return;
}

if ((elemValue!=null)&&(elemValue!="null")) objElem.value=elemValue;
if (elemClass!=null) objElem.className=elemClass;

if (bError==true) objElem.style.backgroundColor="#ffc0e0";
if (bReadOnly==true) {
	objElem.readOnly=true;
	objElem.tabIndex=-1;
	objElem.style.backgroundColor="#f4fcff";
} else if ((bReadOnly==false)&&(objElem.readOnly==true)) {
	objElem.readOnly=false;
	objElem.tabIndex=0;
	objElem.style.backgroundColor="#ffffff";
}

}

function SetElementProperty(objFrm, elemName, elemIndex, elemClass, bReadOnly, bError)
{
if (objFrm==null) return;
var objElem = objFrm.elements[elemName];
if (objElem==null) return;

var objLen = objElem.length;

if ((objLen>1)&&((objElem[0].type=="radio")||(objElem[0].type=="checkbox"))) {
	for (var i = 0; i < objElem.length; i++) {
		if (bReadOnly==true) {
			objElem[i].tabIndex=-1;
		} 
	}
	return;
}

if ((objLen>1)&&(objElem.type==null)) {
	if (elemIndex==null) elemIndex=0;
	objElem = objFrm.elements[elemName][elemIndex];
	if (objElem==null) return;
	objLen = objElem.length;
}

if ((objLen>1)&&(objElem.type=="select-one")) {
	var bFound=false;
	var objList=objElem;
	if (bReadOnly==true) {
		objElem.tabIndex=-1;
	} 
	return;
}


if (elemClass!=null) objElem.className=elemClass;

if (bError==true) objElem.style.backgroundColor="#ffc0e0";
if (bReadOnly==true) {
	objElem.readOnly=true;
	objElem.tabIndex=-1;
	objElem.style.backgroundColor="#f4fcff";
} else if ((bReadOnly==false)&&(objElem.readOnly==true)) {
	objElem.readOnly=false;
	objElem.tabIndex=0;
	objElem.style.backgroundColor="#ffffff";
}

}

function SetCodeValues(objElement, pairName, frmName)
{
if (frmName==null) frmName=objElement.form.name;
if (pairName==null) {
	var objName=objElement.name;
	var objNameSuffix=objName.substr(objName.length-4);
	if (objNameSuffix=="name") pairName=objName.substring(0, objName.length-5);
	else pairName=objName + "_name";
} 

SetElement(objElement.form, pairName, 0, objElement.value);
}


function SetElementValue(objID, objFrm, sValue)
{
var objElem = document.forms[objFrm].elements[objID];
if (objElem!=null) objElem.value=sValue;
}

function setSelectedItem(objID, objFrm, sValue)
{
objList=document.forms[objFrm].elements[objID];
if (objList==null) return;

for (var i = 0; i < objList.options.length; i++) {
   if (objList.options[i].value==sValue)
	   objList.options[i].selected = true;
}
}

function setSelectedItemArray(objID, objIndex, objFrm, sValue)
{
objList=document.forms[objFrm].elements[objID].item[objIndex];
if (objList==null) return;

for (var i = 0; i < objList.options.length; i++) {
   if (objList.options[i].value==sValue)
	   objList.options[i].selected = true;
}
}

function updateSelectedList(objSelect, objElem)
{
if ((objSelect==null)||(objElem==null)) return;
if (objElem.checked==true) {
	var objOption = document.createElement("OPTION");
	objOption.value=objElem.value;
	objOption.text=objElem.value;
	objOption.selected=true;
	objSelect.options.add(objOption);
} else {
    for (i=0; i<objSelect.options.length; i++) {
        if (objSelect.options(i).value==objElem.value) { objSelect.options.remove(i); break; }
    }
}
}

function setChecked(objID, objFrm, sValue)
{
objList=document.forms[objFrm].elements[objID];
if (objList==null) return;
objList.checked=sValue;
}

function setCheckedItem(objID, objFrm, sValue)
{
objList=document.forms[objFrm].elements[objID];
if (objList==null) return;

for (var i = 0; i < objList.length; i++) {
   if (objList[i].value==sValue)
	   objList[i].checked = true;
}

}

function postForm(elemObj, sURL)
{
var objFrm;
var elemType=elemObj.type;
if (elemType!=null) objFrm=elemObj.form;

if (objFrm==null) objFrm=document.forms[elemObj];
if (objFrm==null) return false;
objFrm.action=sURL;
if (elemType!="submit") objFrm.submit();
}

function navigate(sURL)
{
if (sURL==null) return false;
window.location.assign(sURL);
}

function lookup(lookupName, sURL)
{
if (sURL==null) return false;
var lookupWin = window.open(sURL, "lookup_"+lookupName, "top=160,left=60,height=360,width=760,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes");
lookupWin.focus();
}

function printWindow(objWin)
{
if (objWin==null) objWin=window;
objWin.focus();
objWin.print();
}

function printDocument(objWin)
{
if (objWin==null) objWin=window;
var docTitle=objWin.document.title;
objWin.document.title="";
objWin.focus();
objWin.print();
objWin.document.title=docTitle;
}

function printWindowForeground(objWin)
{
if (objWin==null) objWin=window;
var docBGColor=objWin.document.body.style.backgroundColor;
objWin.document.body.style.backgroundColor="#ffffff";
objWin.focus();
objWin.print();
objWin.document.body.style.backgroundColor=docBGColor;
}

function focusField(frmName, fieldName)
{
var frmObj=document.forms[frmName];
if (frmObj==null) return;
var elemObj=frmObj.elements[fieldName];
if (elemObj==null) return;
elemObj.select();
}

function helpTopic(sTopic)
{
var numargs = helpTopic.arguments.length;
if (numargs<1) sTopic = "";
else sTopic = sTopic + ".htm";
var win = window.open("./help/" + sTopic, null, "top=60,left=20,height=520,width=360,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
win.focus();
}

