var dir = "../"
var features = "top=100,left=100,width=550,height=560,status=yes,resizable=yes,scrollbars=yes";
var features1 = "top=100,left=100,width=80,height=60,status=yes,resizable=yes,scrollbars=yes";
var features2 = "top=100,left=100,width=800,height=552,status=yes,resizable=yes,scrollbars=yes";

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function OpenFiles(URL) {
  AddComment(URL);
}

function getIDsForm(FORM, Item, TypeOfRecord) {
  //window.alert(FORM)
  var UserIDs = eval('FORM.' + Item);
  if (! UserIDs) return '';
  var IDs = '';
  if (UserIDs.length) {
    for (var i = 0; i < UserIDs.length; i++) {
      if (UserIDs[i].checked) {
        if (IDs != '') IDs += ","
        IDs = IDs + UserIDs[i].value;
      }
    }
  } else {
    if (UserIDs.checked) IDs = UserIDs.value;
  }
  if (IDs == '') {
  //window.alert('Please select a ' + TypeOfRecord + ' for action');
  }
  return IDs;
}

function TimeLine() {
  //-- get the selected IDS in tasks;
  var FORM = document.forms['qListForm'];
  if(!FORM) {
   window.location.href = '../tasks/timeline.asp';
   return false;
  }
  
  //-- find is any of the one record is selected
  var TaskIDs = FORM.TaskID;
  if(!TaskIDs) {
   window.location.href = '../tasks/timeline.asp';
   return false;
  }  
  
  if(!TaskIDs.length) {
   //-- if only one selected, select the one;
   TaskIDs.checked = true;
  } else {   
   var Checked = 0;
   // find the number of checked items
   for(i=0; i< TaskIDs.length; i++) {
     if(TaskIDs[i].checked) Checked++;
   }
   // if nothing is checked, select all
   if(Checked = 0)  for(i=0; i< TaskIDs.length; i++) {
     TaskIDs[i].checked = true;
   }   
  }
  
  //-- set the form action to submit to timeline.
  FORM.action = '../tasks/timeline.asp';
  FORM.submit();  
}


function AddComment(URL)
{
  switch (window.name) {
  case 'NewWin1': 
    OpenWinWith('NewWin2',URL, 30);
    break;
  case 'NewWin2':
    OpenWinWith('NewWin3',URL, 60);
    break;    
  default:
    OpenWinWith('NewWin1',URL, 0);
    break;
  }
} 

var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
query = query.toLowerCase();
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
   var pos = parms[i].indexOf('=');
   if (pos > 0) {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qsParm[key] = val;
      }
   }
}

function OpenWinWith(winName, URL, Padding) {
  var Features = TopLeftShift(620,428, Padding) + ',status=yes,scrollbars=no,titlebar=no,fullscreen=no';  
  var Win = window.open(URL, winName , Features)    
  Win.resizeTo(620,428);
  Win.focus();
}

function AddComment2(URL) {
  OpenWinWith('NewName2', URL, 60)
}
function AddComment3(URL) {
  OpenWinWith('NewName3', URL, 60);
}

function setPos(Win) {
	var TopWin = Win;
  var Top = 300;
  var Left = 200;
 	if(!TopWin.opener) return;
 	if(!TopWin.opener.opener) return;
 	
	while(TopWin.opener) {
		TopWin = TopWin.opener;
		Top+=30;
		Left+=30;
	}
	var TopWin = Win;
	while(TopWin.opener) {
		TopWin = TopWin.opener;
		if(TopWin.opener) {
			TopWin.moveTo(Top,Left);
			Top-=30;
			Left-=30;		
		}
	}
	Win.moveTo(Top-30,Left-30);
	Win.status = 'Top=' + Top + ', Left=' + Left
	
} 
 
function TopLeft(Width, Height) {
  return TopLeftShift(Width, Height, 0)
}

function TopLeftShift(Width, Height, ShiftOf) {
  var winWidth, winHeight;
  winWidth  = (screen.availwidth ? screen.availwidth : screen.availWidth )
  winHeight = (screen.availheight ? screen.availheight : screen.availHeight)
  winWidth  =  (winWidth - Width)/2
  winHeight =  (winHeight - Height)/2

  var T =  "top=" + (winHeight+ShiftOf) + ",left=" + (winWidth+ShiftOf) + 
  ",width=" + Width + ",height=" + Height;
  return T;
}


var M_Array;
function setActiveMenu(MenuID)
{
  M_Array = eval('M_Array' + MenuID);
}

function setActiveCatgMenu(CategoryCode,ParentID)
{
M_Array = [
'../admin/library_categories_edit.asp?From=category_mgmt.asp&action=new&ParentID=' + CategoryCode,
'../admin/library_categories_edit.asp?From=category_mgmt.asp&action=edit&CategoryCode=' + CategoryCode + '&Parent=' + ParentID +'',
'javascript:Wizard("../admin/UploadCategoryImages.asp?CategoryCode=' + CategoryCode + '");',
'javascript:VerifyCategoryURL("inc_verifycategorydelete.asp?sID=' + CategoryCode + '");'
]
}

//--Delete Conformation
function ConfirmDel(NavURL) {
  if(window.confirm("You are about to delete the selected record permenently\n\n"+
  "Are you Sure?"))
    window.location.href = NavURL;
  return false;
}
function ConfirmDelTree(NavURL) {
  if(window.confirm("You are about to delete the selected record and its sub records permenently\n\n"+
  "Are you Sure?"))
    window.location.href = NavURL;
  return false;
}
//--Delete Conformation
function ConfirmRemove(NavURL) {
  if(window.confirm("You are about to Remove the record\n\n"+
  "Are you Sure?"))
    window.location.href = NavURL;
  return false;
}

function HM_f_PopUp(){return false};
function HM_f_PopDown(){return false};
popUp = HM_f_PopUp;
popDown = HM_f_PopDown;


function ShowMenu(HM_MenuID, Menu_ID) {
  var MyURL = M_Array[Menu_ID];
  var jScript = /^JavaScript:/i
  if(MyURL.search(jScript) >= 0 ) {
    eval(MyURL)
  } else {
    document.location.href = MyURL;
  }
}

var xpos = 0;
var ypos = 0;

//---------------------------------------------------------------------
//Setting up the X location of
//---------------------------------------------------------------------
function setX(Item) {
  if (document.all) {
    xpos = getOffsetLeftX(document.images[Item]);
  } else {
       xpos = document.images[Item].x;
  }
  xpos = xpos + 7 ;//'+ 35;
}

//---------------------------------------------------------------------
// Setting up Y location
//---------------------------------------------------------------------
function setY(Item) {
  if (document.all) {
    ypos = getOffsetTopX(document.images[Item]) ;
  } else {
    ypos = document.images[Item].y;
  }
  ypos = ypos + document.images[Item].height;
}

//---------------------------------------------------------------------
// Reading the X Position
//---------------------------------------------------------------------
function getX() {
  return  xpos;
}

//---------------------------------------------------------------------
// Reading the Y Position
//---------------------------------------------------------------------
function getY() {
  return ypos;
}

//---------------------------------------------------------------------
//Brower setup for Netscape
//---------------------------------------------------------------------
function getOffsetLeftX (el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null)
        ol += el.offsetLeft;
    return ol;
}
function getOffsetTopX (el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null)
        ot += el.offsetTop;
    return ot;
}
/*
function ShowHide(IDName) {
  if (!document.all) return;
  var TR = eval(IDName);
  if (!TR) return;

  var Style = TR.style.display;
  if (Style == "block") {
    Style = "none"
  } else {
    Style = "block"
  }
  TR.style.display = Style;
}
*/
function ShowHide(IDName) {
try { 
  var TR = eval(IDName);
  var Img = eval('document.images["' + IDName +'_img"]');

  if (!TR) return;

  var Style = TR.style.display;
  if (Style == "block" || Style == "table-row") {
    Style = "none"
    if (Img) Img.src = '../images/icon_maximize.gif'
  } else {
    Style = document.all ? "block" : "table-row";
    if (Img) Img.src = '../images/icon_minimize.gif'    
  }
  TR.style.display = Style;
} 
catch (e) {}  
}
function Wizard(URL) {
  var Features = TopLeft(730,500) + ',status=yes,scrollbars=no,titlebar=no,fullscreen=no';  
  var winName = 'NewWin'
  var Win = window.open(URL, winName , Features)    
  Win.resizeTo(730,550);
  Win.focus();
}

function Wizard_ImageSet(URL) {
  var Features = TopLeft(650,650) + ',status=yes,scrollbars=no,titlebar=no,fullscreen=no';  
  var winName = 'NewWin'
  var Win = window.open(URL, winName , Features)    
  Win.resizeTo(750,650);
  Win.focus();
}


function DoCheck(Item, Checked) {
	var FORM = document.forms['qListForm'];
	if(!FORM) return '';
	var UserIDs = FORM.item(Item);
	if(!UserIDs) return '';
  if(UserIDs.length) 
    for(var i=0; i < UserIDs.length; i++) {
      UserIDs[i].checked = Checked;
    }  
}

var IsShowMsg = true;
var Action_ItemValue = '';
function getIDs(Item, IsMulti, TypeOfRecord, ForceRedirect) {
  if(Action_ItemValue != '') {
  	return Action_ItemValue;
 	} 
	var FORM = document.forms['qListForm'];
	if(!FORM) return '';
	//var UserIDs = FORM.item(Item);
	var UserIDs = eval('FORM.' + Item);
	if(!UserIDs) return '';	
	var IDs = '';	
	if(UserIDs.length) {
		for(var i = 0; i < UserIDs.length; i++) {
			if(UserIDs[i].checked) {
				if(IDs != '') {
					if(IsMulti) {
						IDs  = IDs  + ',';
					} else {
						window.alert('Please select one ' + TypeOfRecord + ' for this action');
						return '';
					}
				}
				IDs = IDs  + UserIDs[i].value;
			}
		}
	} else {
		if(UserIDs.checked) IDs = UserIDs.value;
	}
	if( ForceRedirect == null) { ForceRedirect = false;}
	if(IDs == '' && !ForceRedirect) {
		if(IsShowMsg) window.alert('Please select a ' + TypeOfRecord + ' for action');
	}
	return IDs;
}


function getGroups(Item, IsMulti, TypeOfRecord, ForceRedirect) {
	var FORM = document.forms['qListFormG'];
	if(!FORM) return '';
	var UserIDs = FORM.item(Item);
	if(!UserIDs) return '';
	var IDs = '';
	if(UserIDs.length) {
		for(var i = 0; i < UserIDs.length; i++) {
			if(UserIDs[i].checked) {
				if(IDs != '') {
					if(IsMulti) {
						IDs  = IDs  + ',';
					} else {
						window.alert('Please select one ' + TypeOfRecord + ' for this action');
						return '';
					}
				}
				IDs = IDs  + UserIDs[i].value;
			}
		}
	} else {
		if(UserIDs.checked) IDs = UserIDs.value;
	}
	if( ForceRedirect == null) { ForceRedirect = false;}
	if(IDs == '' && !ForceRedirect) {
		if(IsShowMsg) window.alert('Please select a ' + TypeOfRecord + ' for action');
	}
	return IDs;
}


function getIDsByForm(FORM, Item, TypeOfRecord) {
	var UserIDs = FORM.item(Item);
	if(!UserIDs) return '';
	var IDs = '';
	if(UserIDs.length) {
		for(var i = 0; i < UserIDs.length; i++) {
			if(UserIDs[i].checked) {
				if(IDs != '') {
						window.alert('Please select one ' + TypeOfRecord + ' for this action');
						return '';
				}
				IDs = IDs  + UserIDs[i].value;
			}
		}
	} else {
		if(UserIDs.checked) IDs = UserIDs.value;
	}
	if(IDs == '') {
		window.alert('Please select a ' + TypeOfRecord + ' for action');
	}
	return IDs;
}

function checkpassword(formName)
{
	var pass, confirmpass
	pass = document.forms[formName].password.value
	confirmpass = document.forms[formName].confirm_password.value
	if(pass != confirmpass) 
	{
		window.alert('both passwords do not match.')
		return false;
	}
	return true;
}

function isNullForm(FormName){
  var i, formlength, field, c, InputFields
  c=0;
  InputFields = 0
  formlength = document.forms[FormName].length;
  for(i=0; i<formlength; i++) {
     if(document.forms[FormName][i].type=="text" && document.forms[FormName][i].value != ""){
     	c++;
     }
   }
   if(c==0){
   	window.alert('Please note that the fields marked * are mandatory')
   	return false;
   }
   return true;
}

function CheckEmail(TheForm, Checkemail) {
  //Checking for email address
 for(i=0; i<Checkemail.length; i++){
  //var Field = eval(TheForm+'.'+Checkemail[i][0]);
  var Field = eval("document.forms['" + TheForm + "']." + Checkemail[i][0]);
  var Email = Field.value
  //alert (Email)
  var posAt = Email.indexOf('@',0);
  if(Email.length > 0) {
   if ((posAt>0) && (Email.indexOf('.',posAt)>0)) {
      //return true;
   } else {
  	 window.alert("please enter a valid email address at "+Checkemail[i][1]);
     return false;
    }
  }
  }//for
  return true;
}

function validateTelephone(TheForm, CheckFields) {
try{
    for(i=0; i< CheckFields.length; i++) {
		//var Field = eval(TheForm +'.'+CheckFields[i][0]);
		var Field = eval("document.forms['" + TheForm + "']." + CheckFields[i][0]);
		var Value = Field.value;	
		//checking if the first character is + or a number		
		for(j=0; j<Value.length; j++) {
		  var Chr = Value.charAt(j)
  		   switch(Chr) {
	  	   case '0': case '1': case '2': case '3': case '4':case '5':
           case '6': case '7': case '8': case '9':
           case ' ': case '-': 
           break;
        default:
        if(j==0 && Chr == '+') break;        
        Field.focus();
        window.alert('please provide a valid entry for '+CheckFields[i][1]);        
        return false;
      } //switch
      Field.value = Field.value.replace(' ', '');
      Field.value = Field.value.replace('-', '');
     }//for				
	}
	return true;
}catch(e){return true;}
}	

function Img(image) {
	return '<img width="16" height="16" src="../images/' + image + '">';
}

function checkMandatoryFields(TheForm, CheckFields){
try{
	for(i=0; i<CheckFields.length; i++)
	{
		var Field = eval("document.forms['"+TheForm+"']."+CheckFields[i][0])
		var Value = Field.value;
		Value = Value.trim();
		if (Value=='' || Value == undefined)
		{
			window.alert('The field '+CheckFields[i][1]+' is mandatory.');
			Field.focus();
			return false;
		}//if
	}//for
	return true;
}catch(e){alert(e.message)}
}

function checkField(MaxChrs, TheField, e) {
  var L = TheField.value.length;
  if(L <= MaxChrs) return true;
  
  switch(e.keyCode) {
  case 17 : //control key
  case  8 : //back space
  case  9 : //tab key
  case 33 : //page up
  case 34 : //page down
  case 35 : //end
  case 36 : //home
  case 37 : //left arrow
  case 38 : //up arrow
  case 39 : //right arrow
  case 40 : //down arrow  
  case 45 : //insert
  case 46 : //delete key
    return true;
    break;
  default:
    if(L <= MaxChrs) return true;
    return false;
  }
}
function setFieldLength(MaxChrs, TheField) {
  if(TheField.value.length > MaxChrs) {
    TheField.value =  TheField.value.substring(0,MaxChrs);
  }
  return true;
}

function setFormState(FORM, theState) {
  for(var i = 0; i < FORM.length; i++) {
    switch(FORM[i].tagName) {
    case 'INPUT':
    case 'SELECT':
    case 'TEXTAREA':
    	FORM[i].disabled = theState;
    	break;
   	default:
   		window.alert(FORM[i].tagName);
    }    
  }
  FORM.disabled = theState;
}

function TopLeftPerc(Width, Height) {
  var winWidth, winHeight;
  var iWidth, iHeight
  iWidth  = parseInt(screen.availwidth * Width / 100);
  iHeight = parseInt(screen.availheight * Height / 100);
  
  winWidth = (screen.availwidth - iWidth)/2;
  winHeight = (screen.availheight  - iHeight)/2;
  var T =  "top=" + winHeight + ",left=" + winWidth + 
  ",width=" + iWidth + ",height=" + iHeight;

  return T;  
}

function OpenWindowView(URL, Width, Height) {
  var ImageView = window.open(URL, 'ImageView', TopLeft(Width, Height) + ",status=yes,resizable=yes,scrollbars=yes");
  if(ImageView) ImageView.focus();
}

function getMultiIDsByForm(FORM, Item, TypeOfRecord) {
	//var UserIDs = FORM.item(Item);
	var UserIDs = eval('FORM.' + Item);
	if(!UserIDs) return '';
	var IDs = '';
	if(UserIDs.length) {
		for(var i = 0; i < UserIDs.length; i++) {
			if(UserIDs[i].checked) {
			  if (IDs == '') 
			  IDs = UserIDs[i].value;
			  else			  
				IDs = IDs  + ',' + UserIDs[i].value;
			}
		}
	} else {
		if(UserIDs.checked) IDs = UserIDs.value;
	}
	if(IDs == '') {
		if(IsShowMsg) window.alert('Please select a ' + TypeOfRecord + ' for action');
	}
	return IDs;
}
//window.alert('Loaded New Version');


