// JavaScript Document
//********************************************************
//* Purpose: To popup a confirm box making sure that they want to delete the file
//* Precondition: message must be a string and url should be a valid url
//* Postcondition: If true then the redirect occurs
//*******************************************************
function ConfirmDelete(message, url){ if(confirm(message)) location.href = url; }

//*******************************************************
//* Purpose: THis function calls the Progress Bar page and submit's the form to the upload page, on the submitting form set the action page ie Process.asp
//* Precondition: objFormIn be a form object and intProgressIDIn be a progress ID from Dundas.UploadProgress object
//* Postcondition: Progress bar window is called and the Page that uploads the file has the info submitted to it
//***********************************************************************
function Upload(objFormIn, intProgressIDIn)
//this javascript function runs when the user clicks on the form's button.
//it opens the progressbar.asp window and then submits the form data to process.asp
{
var intProgressID;
intProgressID=intProgressIDIn;
if (intProgressID != -1)
	{
	//only open progressbar.asp window if there is a valid id. We will center this progress bar as well.
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=no,MENUBAR=no,WIDTH=400,HEIGHT=100";
	Param += ",TOP=" + String(window.screen.Height/2 - 50);
	Param += ",LEFT=" + String(window.screen.Width/2 - 200);
	//note that we pass the Progress ID to progressbar.asp
	window.open("includes/Upload-ProgressBar.asp?ProgressID="+intProgressID, null, Param);
	}

//now that progress bar window is open submit the form data to Process.asp, passing
// the ProgressID as a querystring parameter
objFormIn.action = objFormIn.action + "&ProgressID=" + intProgressID
objFormIn.submit();
}

//**************************************************************************
//* Purpose: Takes in an email and checks that it's a valid format
//* Precondition: strEmailIn be a string
//* Postcondition: if email is invalid false is return and an alert is poped up
//**************************************************************************
function ValidateEmail(strEmailIn)
{
	//validate email with regexp
	var str = strEmailIn.value; 
	var msg = "";
	//delete error message if ther is one
	try{
		document.getElementById(strEmailIn.id).parentNode.removeChild(document.getElementById("error" + strEmailIn.id));
	}
	catch(err){}
	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i  
	if (str != "") {
		if (filter.test(str)) 
		{

			return true;
		}
		else {
			msg+= "  Please enter a valid email address.\n"
		}
	}
	else if (str == "") {
			msg+= "  Please enter an email address.\n"
	}
	if (msg != "")
	{
  		add(strEmailIn.id, msg);
		return false;
	}
}

function add(theId, msg) 
{   
   var node = document.getElementById(theId);
   var elem = document.createElement('DIV');
   elem.setAttribute("style","color:red");
   elem.setAttribute("id","error" + theId);
   elem.innerHTML = '*' + msg;
   node.parentNode.insertBefore(elem, node);
}


//**************************************************************************
//* Purpose: Moves an item from one Select Box to Another
//* Precondition: fbox and tbox are valid boxes on the form
//* Postcondition: the entry from fbox is now in tbox
//**************************************************************************
function move(fbox, tbox) 
{
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) 
	{
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) 
	{
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") 
		{
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}
		else
		{
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}
	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) 
	{
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	for(c = 0; c < arrTbox.length; c++) 
	{
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
  }
}

//************************************************
//* Purpose: Validates UserName
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  alert Message if not valid
//************************************************
function Check_UserName(objFieldIn)
{
	var ErrorMessage = "";
	if (objFieldIn.value.length < 4){
		ErrorMessage = ErrorMessage + "UserName must be at least 4 characters\n";
	}
	
	if (objFieldIn.value == ""){
		ErrorMessage = ErrorMessage + "Please Enter a UserName\n";		
	}
	else{
		if (!Validate_User(objFieldIn)){
			ErrorMessage = ErrorMessage + "Invalid entry!  Only characters and numbers are accepted!";
		}
	}
	if (ErrorMessage != ""){
		alert(ErrorMessage);
		return false;
	}
	else
		return true;
}
//************************************************
//* Purpose: Validates Password
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  alert Message if not valid
//************************************************
function Check_PWD(objFieldIn)
{
	var ErrorMessage = "";
	if (objFieldIn.value.length < 6){
		ErrorMessage = ErrorMessage + "Password must be at least 6 characters\n";
	}	
	if (objFieldIn.value == ""){
		ErrorMessage = ErrorMessage + "Please Enter a Password\n";
	}
	else{
		if (!Validate_User(objFieldIn)){
			ErrorMessage = ErrorMessage + "Invalid entry!  Only characters and numbers are accepted as a Password!";
		}
	}
	if (ErrorMessage != ""){
		alert(ErrorMessage);
		return false;
	}
	else
		return true;
}
//************************************************
//* Purpose: Validates Email
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  alert Message if not valid
//************************************************
function Check_Email(objFieldIn)
{
	if (objFieldIn.value != ""){
	  var regEmailPattern = /^(-|\w)+(\.(-|\w)+)*@(-|\w)+(\.(-|\w)+)+$/i
    if (!regEmailPattern.test(objFieldIn.value)){
			alert("Invalid Email Address");
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Checks for invalid characters
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Validate_User(objFieldIn)
{
	var valid = "abcdefghijklmnopqrstuvwxyz0123456789"
	var blnValid = true;
	var bytTemp;
	for (var i=0; i < objFieldIn.value.length; i++){
		bytTemp = "" + objFieldIn.value.toLowerCase().substring(i, i+1);
		if (valid.indexOf(bytTemp) == "-1") blnValid = false;
	}
	return blnValid;
}
//************************************************
//* Purpose: Checks for invalid characters
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Validate_Names(objFieldIn) 
{
	var valid = "abcdefghijklmnopqrstuvwxyz'-"
	var blnValid = true;
	var bytbytTemp;
	for (var i=0; i < objFieldIn.value.length; i++) 
	{
		bytTemp = "" + objFieldIn.value.toLowerCase().substring(i, i+1);
		if (valid.indexOf(bytTemp) == "-1") blnValid=false;
	}
	return blnValid;
}
//************************************************
//* Purpose: Validates Name Fields
//* Precondition:  objFieldIn is a valid txtBox, strWhatIn, is the name of the field
//*	PostCondition:  alert message if not valid
//************************************************
function Check_Name(objFieldIn, strWhatIn)
{	
	if (!objFieldIn.value){
		alert("Please Enter your "+ strWhatIn);
		return false;
	}
	else{
		if (!Validate_Names(objFieldIn)){
			alert("Invalid entry!  Only characters are accepted for "+ strWhatIn);
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Make sure the password was confirmed
//* Precondition: objField1 & objField2 have data
//*	PostCondition: alert message if not equal
//************************************************
function SamePWD(objField1, objField2)
{
	if (objField1.value != objField2.value){
		alert("Password was not confirmed");
		return false;
	}
	return true;
}
//************************************************
//* Purpose: Checks for invalid characters
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Validate_Text(objFieldIn)
{
	var pattern = /^((\w)|(\s)|(\/)|(\-)|(\?))*$/i 

	if (objFieldIn.value != ""){
		if (!pattern.test(objFieldIn.value.toUpperCase())){
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Validates Text Fields
//* Precondition:  objFieldIn is a valid txtBox, strWhatIn, is the name of the field
//*	PostCondition:  alert message if not valid
//************************************************
function Check_Text(objFieldIn, strWhatIn)
{	
	var blnValid = true;
	if (objFieldIn.value != ""){
		if (!Validate_Text(objFieldIn)){
			alert("Invalid entry!  Only characters are accepted for "+ strWhatIn +"!");
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Checks for valid PostalCode
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Check_PostalCode(objFieldIn)
{	
	var pattern = /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i
	if (objFieldIn.value != ""){
		if (!pattern.test(objFieldIn.value.toUpperCase())){
			alert("Please input a valid Postal Code");
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Checks for valid PhoneNumber
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Check_PhoneNumber(objFieldIn, strWhatIn)
{
	var regPhonePattern = /^(\d+(\s|-|\.))?((\d{3})|(\(\d{3}\))(\s|-|\.))?\d{3}(\s|-|\.)\d{4}$/i
	if (objFieldIn.value !=""){
		if (regPhonePattern.test(objFieldIn.value) == false){
			alert("Please Enter a valid " + strWhatIn +" with area code");
			return false;
		}
	}
	return true;
}
//************************************************
//* Purpose: Checks for valid Number
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Check_Number(objFieldIn, strWhatIn)
{
	if (isNaN(objFieldIn.value)){
		alert("Please Enter a valid number for "+ strWhatIn);
		return false;
	}
	return true;
}
//************************************************
//* Purpose: Checks for valid website address
//* Precondition:  objFieldIn is a valid txtBox
//*	PostCondition:  return false if invalid
//************************************************
function Check_Website(objFieldIn)
{
  var regWebsitePattern = /^((http(s)?|ftp):\/\/)?([A-Z]|\d|-)+(\.([A-Z]|\d|-)+)*(\/([A-Z]|\d|-)+(\.([A-Z]|\d|-)+))*(\/|(\?([A-Z]|\d)+=([A-Z]|\d)+(&([A-Z]|\d)+=([A-Z]|\d)+)*))?$/i
  
  if (objFieldIn.value != ''){
    if (!regWebsitePattern.test(objFieldIn.value)){
      alert('Website must be a valid website address.');      
      return false;
    }
  }
	return true;
}
//*************************************************************************
//* Purpose: Opens a pop-up window, with the given dimensions
//* Precondition: strURL be an actual url, intHeight and intWidth be integers
//* Postcondition: pop-up window is opened with the webpage
//**************************************************************************
var win= null;
function PopUpWindow(strURL, intHeight, intWidth, strScroll, strResize){
  //var h = 575;
  //var w = 750;
  var winl = (screen.width-intWidth)/2;
  var wint = (screen.height-intHeight)/2;
  var settings  ='height=' + intHeight + ',';
      settings += 'width=' + intWidth + ',';
      settings += 'top=' + wint + ',';
      settings += 'left=' + winl + ',';
	 		settings += 'scrollbars=' + strScroll + ',';
      settings += 'resizable=' + strResize;
  win=window.open(strURL,'_blank',settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}
//*************************************************************************
//* Purpose: converts a regular formatted date, into JS date type
//* Precondition: datDateIn is in format dd/mm/yyyy
//* Postcondition: a JS date type is returned
//**************************************************************************
function ConvertToDate(datDateIn){
	var datDates = datDateIn.split("/");
	var datDateOut = new Date(datDates[2], datDates[0]-1, datDates[1]);
	return datDateOut;
}
//********************************************************
//* Purpose: To create the querystring to call the move page and move the items to the appropriate list boxes
//* Precondition: strAction is a valid string, strList is a valid list box with select items, IDIn is a valid ID,
//*	 strClassIn is the name of the class
//* Postcondition: the selected items are moved to the appopriate listboxes, and the DB reflects those changes
//*******************************************************
function Update(objListIn, intIDIn, strAction, strClassIn)
{		
	var URL;
	var intTemp;
	URL = strClassIn + "-Move.asp?GroupID="+ intIDIn +"&Action=" + strAction ;
	for(intTemp=0; intTemp < objListIn.options.length; intTemp++){
		if (objListIn.options[intTemp].selected){
			URL = URL + "&ID=" + objListIn.options[intTemp].value;
		}
	}
	window.open (URL,"Move", "width=4,height=4,left=4000,top=4000")
}
//********************************************************
//* Purpose: To check if anything is selected in the box
//* Precondition: objListIn is a valid listbox on the form
//* Postcondition: return false if nothing is selected
//*******************************************************
function IsValid(objListIn)
{
	if (objListIn.value != "")
		return true;
	else
		return false;
}
//********************************************************
//* Purpose: submit the form on the current page
//* Precondition: form object, name of class, and ID are passed in
//* Postcondition: form is submitted and set back to edit state
//*******************************************************
function SubmitForm(objFormIn)
{
	objFormIn.action = objFormIn.action + '&CE=true'
	objFormIn.submit();
}

//*************************************************************************
//* Purpose: puts together an email and sets a mailto link
//* Precondition: strusername and strdomainname are valid
//* Postcondition: a mailto link is created
//**************************************************************************
function SendEmail(strUserNameIn, strDomainNameIn){
	var strEmail = strUserNameIn + "@" + strDomainNameIn;
	window.location = "mailto:" + strEmail;
}

//**************************************************************************
//* Purpose: To switch the arrow from being up or down and making the table display or hid
//* Precondition: strElementNameIn, image name must start with img and table name must start tbl
//* Postcondition: table and image are switched
//**************************************************************************
function SwitchOnOff(strElementNameIn)
{
	var objTable, objImage
	objTable = document.getElementById("tbl" + strElementNameIn)
	objImage = document.getElementById("img" + strElementNameIn)
	
	if (objTable.style.display == "block") 
	{
		objTable.style.display = "none";
		objImage.src = "images/Arrow-Down.jpg";
	}else{
		objTable.style.display = "block";
		objImage.src = "images/Arrow-UP.jpg";
	}

}

//**************************************************************************
//* Purpose: To switch the sign from being plus or minus and making the table display or hid
//* Precondition: strElementNameIn, image name must start with img and table name must start tbl
//* Postcondition: table and image are switched
//**************************************************************************
function SwitchSign(strElementNameIn)
{
	var objTable = document.getElementById('tbl' + strElementNameIn);
	var objImage = document.getElementById('img' + strElementNameIn);
	
	if (objTable.style.display == 'block') 
	{
		objTable.style.display = 'none';
		objImage.src = "images/button_plus.JPG";
	}
	else
	{
		objTable.style.display = 'block';
		objImage.src = "images/button_minus.JPG";
	}
}

//**************************************************************************
//* Purpose: To switch the sign from being plus or minus and making the table display or hid
//* Precondition: strElementNameIn, image name must start with img and table name must start tbl
//* Postcondition: table and image are switched
//**************************************************************************
function SwitchFolder(strElementNameIn)
{
	var objTable, objImage	
	objTable = document.getElementById("tbl" + strElementNameIn)
	objImage = document.getElementById("img" + strElementNameIn)
	
	if (objTable.style.display == "block") 
	{
		objTable.style.display = "none";
		objImage.src = "images/Closed.gif";
	}else{
		objTable.style.display = "block";
		objImage.src = "images/Open.gif";
	}

}

//***********************************************************************************
//* Purpose: Changes the bgcolor and text of objContent to whats in the arrIn, ArrX[0]=bgcolor, arrX[1]=text
//* Precondition: objContent be on the page somewhere
//* Postcondition: bgcolor and text are changed
//************************************************************************************
function ChangeDesc(arrIn, strContent)
{
	var objTemp = document.getElementById(strContent);
	
	objTemp.bgColor = arrIn[0];
	objTemp.innerHTML = arrIn[1];
	
}

//******************************************************************************************
//* Purpose: Redirects the page to the given url
//* Precondition: strURL be a valid url
//* Postcondition: page is redirected
//********************************************************************************************
function JumpToURL(strURL)
{
	if (strURL != 0)
	{
		location.href = strURL;
	}
}

//*********************************************************************************************
//* Purpose: Main function to NULL out a date field in iWeb, calls the popup window for the ASP script and clears out the text box
//* Precondition: objDateBox be a valid html text box, strPlugin and intID be valid fields
//* Postcondition: Date is Nulled out in the db and text box
//*************************************************************************************************
function NullDate(objDateBox, strPlugin, intID)
{
	objDateBox.value = "";
	
	//PopUpWindow("includes/DateNull.asp?Plugin=" + strPlugin + "&ID=" + intID, 0,0);
	if (intID > 0){
		window.open ("includes/DateNull.asp?Plugin=" + strPlugin + "&ID=" + intID,"", "width=4,height=4,left=4000,top=4000")
	}
}

//**************************************************************************************************
//* Purpose: To add a bookmark in the users browser for the current page, this works IE and Mozilla
//**************************************************************************************************
function AddBookmark()
{
	if( window.sidebar && window.sidebar.addPanel ) {
    //Gecko (Netscape 6 etc.) - add to Sidebar
    window.sidebar.addPanel(window.document.title, location.href, '' );		
	} else if( window.external && ( navigator.platform == 'Win32' ||
				( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) ) {
			//IE Win32 or iCab - checking for AddFavorite produces errors for no
			//good reason, so I use a platform and browser detect.
			//adds the current page page as a favourite; if this is unwanted,
			//simply write the desired page in here instead of 'location.href'
			window.external.AddFavorite( location.href, document.title );
	} else if( window.opera && window.print ) {
			//Opera 6+ - add as sidebar panel to Hotlist
			return true;
	} else if( document.layers ) {
			//NS4 & Escape - tell them how to add a bookmark quickly (adds current page,
			//not target page)
			window.alert( 'Please click OK then press Ctrl+D to create a bookmark' );
	} else {
			//other browsers - tell them to add a bookmark (adds current page, not target page)
			window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
	}
	return false;
}



