//*****************************************************************************
// Copyright (C) 2001 Microsoft Corporation. All rights reserved.
//
// File:   /SCAApp/Shared/Shared.js
// Author: Microsoft Corporation
//
// Description:
//   JS file that provides common use utilities
//
//*****************************************************************************

// UI Constants

var IDS_ERROR_MESSAGE_EMPTY = "Please enter cache size in this field."


//General config
var IDS_ERROR_MESSAGE_NONENUMERIC = "Non-integer values are not allowed in the \"Time Between Attempt (days)\" text field";
var IDS_NOTALLOWED_EMPTY = "Please enter a value in the \"Time Between Attempt (days)\" text field .";
var IDS_DAY_RANGE = "The value of the \"Time Between Attempt (days)\" text field must be a positive integer \n(i.e. it must be greater than or equal to one).";

//Cache config
var IDS_NOTALLOWED_EMPTY_FORCOOKIETIME = "Please enter cookie life time in minutes."
var IDS_MESSAGE_UNEXPECTED_ERROR = "Unexpected error!";

//Customize message for max cache size
//field name
var IDS_MESSAGE_MUST_ENTER_CACHESIZE_GLOBAL_MAX = "Maximum Disk Cache Size (MB)(Use Global Default)";
var IDS_MESSAGE_MUST_ENTER_CACHESIZE_LOCAL_MAX =  "Maximum Disk Cache Size (MB)(Use Local Override)";
var IDS_MESSAGE_MUST_ENTER_CACHESIZE_GLOBAL_NODE = "Maximum Nodes in Memory Cache(Use Global Default)";
var IDS_MESSAGE_MUST_ENTER_CACHESIZE_LOCAL_NODE = "Maximum Nodes in Memory Cache(Use Local Override)";

//Message content

var IDS_MESSAGE_PRE_MUST_ENTER = "Please enter a value in the \"";
var IDS_MESSAGE_AFT_MUST_ENTER = "\" text field.";

var IDS_MESSAGE_PRE_MUST_INTEGER = "Non-integer values are not allowed in the \"";
var IDS_MESSAGE_AFT_MUST_INTEGER = "\" text field.";

var IDS_MESSAGE_PRE_MUST_LARGERTHAN = "The value of the \"";
var IDS_MESSAGE_AFT_MUST_LARGERTHAN = "\" text field must be a positive integer \n(i.e. it must be greater than or equal to one).";
var IDS_MESSAGE_AFT_MUST_LARGERTHAN50MB = "\" text field must be 50 or larger.";

var IDS_MESSAGE_MUST_ENTER_LOCATION = "Please enter a value in the \"Local Disk Cache Location\" text field.";
var IDS_ERROR_MESSAGE_FOR_FOLDER = "Please choose a cache folder!"
var IDS_ERROR_MESSAGE_FOR_SELECT = "The computer name cannot be chosen as a cache folder.  Please select another folder."
var IDS_MESSAGE_SPACE_LARGER_THAN_DRIVER = "Maximum cache size cannot be larger than the free space available on the selected drive.";
var IDS_MESSAGE_NO_DRIVER_EXIST ="The specified disk cache location does not exist or is not a local folder on the Microsoft Content Management Server machine. Please enter or browse for an existing local folder.";
var IDS_MESSAGE_DRIVER_FORMAT = "The format of the string found in the \"Local Disk Cache Location\" text field is incorrect.  Please correct the format or use the appropriate browser to select a value."

//Security config

var IDS_NOTALLOWED_ERROR_FORMAT = "The format of the string found in the \"Windows NT User Account:\"  or \" Guest Login Account\" text field is incorrect.  Please correct the format or use the appropriate browser to select a value."

var IDS_MESSAGE_ENTER_VALUE_BEFORE = "Please enter a value in the \"";
var IDS_MESSAGE_ENTER_VALUE_AFTER = "\" text field .";
var IDS_MESSAGE_FIELD_ACCOUNT_PASSWORD = "Account Password";
var IDS_MESSAGE_FIELD_NTUSERACCOUNT = "Windows NT User Account"
var IDS_MESSAGE_FIELD_GUEST_ACCOUNT = "Guest Login Account";
var IDS_MESSAGE_FIELD_COOKIE_LIFETIME = "Cookie Lifetime";
var IDS_ERROR_MESSAGE_NON_INTEGER =  "Non-integer values are not allowed in the \"Cookie Lifetime\" text field";
var IDS_ERROR_MESSAGE_LESSTHANRANGE = "The value of the \"Cookie Lifetime \" text field must be a positive integer (i.e. it must be greater than or equal to one).";
var IDS_ERROR_MESSAGE_SELECT_DOMAIN = "Please choose both a Windows NT domain and a Windows NT user account, and try again.";
var IDS_ERROR_MESSAGE_SELECTUSER = "Please choose both a Windows NT domain and a Windows NT user account, and try again."

//License config
var IDS_MESSAGE_MUST_ENTERCOMPANYNAME = "Please enter a value in the \"Company Name\" text field .";
var IDS_MESSAGE_MUST_INLENGTHRANGE = "Company Name must be longer than 5 characters and shorter than 80 characters."
var IDS_MESSAGE_MUST_ENTERLICENSEKEY = "You must enter at least one license key value to continue.";
var IDS_MESSAGE_MUST_ENTERRIGHTKEY = "A license key consists of 16 alphanumeric characters, in four groups of four.";
var IDS_MESSAGE_MUST_ENTERRIGHTCHARACTERS = "There is an invalid character in this license key."

//Web Server config
var IDS_MESSAGE_MOVEORADD_ENTRYPOINTS =  "You are adding or removing one or more Microsoft Content Management Server Web Entry Points.  The Server Configuration Application will stop and restart the affected web sites to perform this operation. \n Click OK to continue."

var IDS_MESSAGE_ONLY_ENTRY_POINT_1 = "The Microsoft Content Management Server must always have at least one Web Entry Point assigned to it. Unfortunately,";
var IDS_MESSAGE_ONLY_ENTRY_POINT_2 = "is the only Web Entry Point assigned to Microsoft Content Management Server.\n\nIf you do not wish to keep";
var IDS_MESSAGE_ONLY_ENTRY_POINT_3 = "as a Microsoft Content Management Server Web Entry Point, you must assign another Web Server to the Microsoft Content Management Server first."


// Programming Constants
var IDS_VALID_NUMBERS = "-.0123456789";
var IDS_WEBSERVER = "WebServer"
var IDS_CONFIG_WEB_SERVER_FORM ="SCA_frm_ConfigWebServer"
var IDS_CONFIG_WEB_SERVER_WINDOW_NAME = "WebServerWindow"
var IDS_GLOBAL_FALSE = "GlobalFalse"
var IDS_GLOBAL_TRUE = "GlobalTrue"
var IDS_LOCAL_FALSE = "LocalFalse"
var IDS_LOCAL_TRUE = "LocalTrue"

var IDS_ASPPAGE_SYSTEM_ACCOUNT_DLG = "SystemAccountConfigDlg.asp"
var IDS_ASPPAGE_SECURITY_DLG = "SecurityConfigDlg.asp"
var IDS_ASPPAGE_CACHE_DLG = "CacheConfigDlg.asp"

var IDS_ASPPAGE_NTUserBrowse ="NTUserBrowseDlg.asp"
var IDS_CONFIG_UPDATE_PAGE = "ConfigProcess.asp"

var IsWindowOpenBefore = false;
var ChangedSelectedValue = "None"; 
var strLastWebServerName; 

//---- [Public] Open a browser window and post form content to it
function SCA_openWindowAndSubmit( strFormName, strActionURL, strWinName, strWinFeatures)
{
	// open a window and submit content to it
	window.top.open('', strWinName, strWinFeatures);

	var pForm = window.top.document.forms[strFormName];
	pForm.target = strWinName;
	pForm.action = strActionURL;
	pForm.submit();
}

//---- [Private]Get text value from selected box 
function GetSelectedboxValue(GlobalCleanSelectbox)
{
	 var TextValue;
	 for (i=0; i<= GlobalCleanSelectbox.length; i++)
	 {
	 	if (GlobalCleanSelectbox.options[i].selected == true)
	 	{
			TextValue = GlobalCleanSelectbox.options[i].value;
			alert(TextValue);
			break;
	 	}
		return TextValue;	 
	}
}

//---- [Public] transfer global value into html
function SCA_transferGlobalValue(TransferID,strChangedValue)
{
	document.all[TransferID].innerHTML = strChangedValue;
}  

//---- [Public] Change radio button value after user set global or local value
function SCA_checkRadioButtonForTextBox(FormName, strRadioButtonname,nIndex0,nIndex1,strTextFieldName)
{
	var bCheckedButton = document[FormName][strRadioButtonname];
	var bUnCheckedButton = document[FormName][strRadioButtonname];
	var strTextName = document[FormName][strTextFieldName];

	if (strTextName.value.length != 0) 
	{
		bCheckedButton[nIndex0].checked = true;
		bUnCheckedButton[nIndex1].checked = false; 
	}	
}

//---- [Public] Change radio button value after user set global or local value
function SCA_checkRadioButtonForSelectBox(FormName, strRadioButtonname,nIndex0,nIndex1,strTextFieldName)
{
	var bCheckedButton = document[FormName][strRadioButtonname];
	var bUnCheckedButton = document[FormName][strRadioButtonname];
	var strTextName = document[FormName][strTextFieldName];

		bCheckedButton[nIndex0].checked = true;
		bUnCheckedButton[nIndex1].checked = false; 
	
}

//---- [Public] Change image on mouse over event
function SCA_swapImage()
{
  	var i,j=0,x,a=SCA_swapImage.arguments; 
  	document.sr=new Array; 
  	for(i=0;i<(a.length-2);i+=3)
    if ((x=findObj(a[i]))!=null)
    	{
    	document.sr[j++]=x; 
    	if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
    	}
}
///---- [Private] find object 
function findObj(n, d) { 
 	 var p,i,x;  if(!d) d=document; 
 	 if((p=n.indexOf("?"))>0&&parent.frames.length) 
 	 {
    	d=parent.frames[n.substring(p+1)].document; 
    	n=n.substring(0,p);
     }
 		 if(!(x=d[n])&&d.all) x=d.all[n]; 
 		 for (i=0;!x&&i<d.forms.length;i++)
 		 	x=d.forms[i][n];
  			for(i=0;!x&&d.layers&&i<d.layers.length;i++)
  			x=findObj(n,d.layers[i].document);
  			return x;
}

//---- [Public] Restore image on mouse out event
function SCA_swapImgRestore()
{
	var i,x,a=document.sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
	x.src=x.oSrc; 
}

//----[Public] Open a popup window to configure
function SCA_openPopupWindow(strURL,nWidth,nHeight)
{
	
	var nWinLeft;
	var nWinTop;
	nWinLeft = (screen.width - nWidth) / 2;
	nWinTop = (screen.height - nHeight) /2;
	
	if (IsWindowOpenBefore == true)
    {			
		ConfigWindow.close();
	 }
	IsWindowOpenBefore = true;
	
	var strFeatures ="toolbar=no,location= no,directories = no,";
	strFeatures += "status = no, menubar = no,scrollbars = no,";
	strFeatures += " resizable = no,width= " + nWidth + ",left=" + nWinLeft;
    strFeatures += ", top =" + nWinTop + ", height=" + nHeight ;
	SCA_openPopup(strURL,strFeatures);	
}

//---- [Public] Submit a form
function SCA_submitForm(SubmitAction,SubmitFormName,SubmitTarget)
{  
	document[SubmitFormName].action = SubmitAction;
	if (SubmitTarget != "")
	{
		document[SubmitFormName].target = SubmitTarget;
	}
	document[SubmitFormName].submit();
}


//---- [Public] Validate text field empty
function SCA_validateTextFieldEmpty(TextFieldName)
{   
	 if ( TextFieldName.value == "" )
	   return false;
	 else
	   return true;   
}


//---- [Public] Validate text field numeric
function SCA_validateTextFieldNumeric(TextFieldName)
{   
	var valid = IDS_VALID_NUMBERS 
	var ok = "yes";
	var temp;
	for (var i=0; i<TextFieldName.value.length; i++) 
	{
	temp = "" + TextFieldName.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1")
		{
		    ok = "no";
		    break;
	    }
	}
    //bug 6099   
   	if (TextFieldName.value.length==1 && (TextFieldName.value=="-" || TextFieldName.value ==".")  )
   	{
   		 ok = "no";
    }
	else
	{
	temp = "" + TextFieldName.value.substring(TextFieldName.value.length, i-1);
	if (temp=="-" )
		{	
		ok = "no";
		}
    }
    
    if (ok == "yes")
    	return true;
    else
    	return false;
}

//---- [Public] Validate text field number range
function SCA_validateTextFieldRange(TextFieldName,nMinRange)
{ 
   if  (TextFieldName.value < nMinRange)
   	   return false;
   else 
       return true;
}

//---- [Private] Open a popup window
function SCA_openPopup(strPageName,strModalFeatures)
{
   
   ConfigWindow = PopupWindow = window.open(strPageName,'',strModalFeatures);
   if (ConfigWindow.opener == null) ConfigWindow.opener = self;

}

//---- [Private] Open a modal window
function SCA_openModal(strPageName,strModalFeatures)
{
	strReturnValue = window.showModalDialog(strPageName, "", strModalFeatures);
    return strReturnValue;  
}

//---- [Public] Close popup window or showmodal window
function SCA_closeWindow()
{
    
    IsWindowOpenBefore = false;
    window.close();
}

// ---- [Public] Change mouse icon to Wait
function SCA_setMouseIconToWait( pWindow )
{
	pWindow.document.body.style.cursor = 'wait';
}

// ---- [Public] Change mouse icon to Default
function SCA_setMouseIconToDefault( pWindow )
{
	pWindow.document.body.style.cursor = 'default';
}

// ---- [Public] This function is probably not in use; but left here for safety sake
function SCA_changeMouseIconToWait( idElement )
{
	document.all[idElement].style.cursor = 'wait';
}


//---- [Public] Get backspace key and cancel it
function mykeyhandler() {
	
	document.onkeydown = mykeyhandler;
	if (window.event && window.event.keyCode == 8) 
	{
        // try to cancel the backspace
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        
        return false;
    }
}
// ---- [Public] change mouse icon and submit form for web server 
function SCA_submitWebServerForm(strURL,strFormName,DivisionID)
{
	SCA_changeMouseIconToWait(DivisionID);
	SCA_submitForm(strURL,strFormName,'');
	
}
//---- [Public] Warning user if user remove entry points or add entry points
function SCA_giveWarningMes(APIValue,SelectedValue, SelectedBoxName)
{
	
	
	var strSelectedBoxNumName = document["SCA_frm_ConfigWebServer"]["SCA_frm_Selectbox_number"]; 
	ChangedSelectedValue = APIValue;
	var SelectNoEntryPoint = 0;
	
	for (i=0;i< strSelectedBoxNumName.value; i++)
	{
		var strSelectedBoxName = document["SCA_frm_ConfigWebServer"]["SCA_frm_Select_EntryPoints" + i];
		var strWebServerName = document["SCA_frm_ConfigWebServer"]["SCA_frm_WebServer_Name"+i];
		
		if (strSelectedBoxName.value == "NoEntryPoints")
		{
			SelectNoEntryPoint = SelectNoEntryPoint+1;	
			
		}
		
		if (SelectedBoxName =="SCA_frm_Select_EntryPoints" + i)
		{
			
			strLastWebServerName=strWebServerName.value;
			
			
		}
		
	}
	
	if ( SelectNoEntryPoint== parseFloat(strSelectedBoxNumName.value))
	{
		var DropDownBox = document.forms[0][SelectedBoxName];
		var Message = IDS_MESSAGE_ONLY_ENTRY_POINT_1 + "\"" + strLastWebServerName + "\"" + IDS_MESSAGE_ONLY_ENTRY_POINT_2 + "\"" +  strLastWebServerName + "\"" + IDS_MESSAGE_ONLY_ENTRY_POINT_3;
		alert(Message);
		 for (var i=0; i<DropDownBox.options.length; i++) 
		{
			if (DropDownBox.options[i].value == "ReadAndWrite")
			{
				DropDownBox.options[i].selected = true;
			}
		 }	
	}
	else
	{
		if ((ChangedSelectedValue == "NoEntryPoints") || (SelectedValue == "NoEntryPoints")) 
		{
			if (confirm(IDS_MESSAGE_MOVEORADD_ENTRYPOINTS))
			{
				ChangedSelectedValue = SelectedValue;
			}
			else
			{

				 var DropDownBox = document.forms[0][SelectedBoxName];
				 for (var i=0; i<DropDownBox.options.length; i++) 
				 {	
					
					if ((DropDownBox.options[i].value == ChangedSelectedValue) && (ChangedSelectedValue !="NoEntryPoints"))
					{
						DropDownBox.options[i].selected = true;
					}

				 }	
			}
		}
	}
}

//----[Public] Open a popup window to configure
function SCA_openPopupWindowForSecurity(strURL,nWidth,nHeight)
{
	
	 
	var nWinLeft;
	var nWinTop;
	nWinLeft = (screen.width - nWidth) / 2;
	nWinTop = (screen.height - nHeight) /2;
	var strFeatures ="toolbar=no,location= no,directories = no,";
	strFeatures += "status = no, menubar = no,scrollbars = no,";
	strFeatures += " resizable = no,width= " + nWidth + ",left=" + nWinLeft;
    strFeatures += ", top =" + nWinTop + ", height=" + nHeight ;
	SCA_openPopupForSecurity(strURL,strFeatures);	
}
//---- [Private] Open a popup window
function SCA_openPopupForSecurity(strPageName,strModalFeatures)
{
   
   SecurityWindow = window.open(strPageName,'BrowserWindow',strModalFeatures);
   if (SecurityWindow.opener == null) SecurityWindow.opener = self;

   
}

//----[Public] close message box for tab
function SCA_CloseTabMsgbox(MsgboxID)
{
	if (document.all[MsgboxID] != null) 
	{
		document.all[MsgboxID].style.visibility = 'hidden';
				
	}
}

var IDS_XML_CDATA_BEGIN = "<![CDATA[";
var IDS_XML_CDATA_END = "]]>";

/**
 *
 *	Wrap the value with XML tag. If bCData = true, then wrap with CDATA; 
 *	otherwise; don't wrap CDATA. By Wrapping CDATA with the value in XML,
 *	it instructs the XML DOM parser to ignore most markup characters.
 *	Therefore, the user can type in the text even though it is same as XML tag.
 *	For example, the user type "</EOP> this is my eop value <EOP>" into the 
 *	EOP input text area. Since this value is wrapped by CDATA. After submit 
 *	the form by XML and parsing the XML, any charater between the start of the
 *	CDATA section, <![CDATA[ and the end of the section, ]]> is passed directly
 *	to server, without interpretation.
 *
 *	@param strXMLTagName [in] - the XML Tag name to be used to wrap the value.
 *
 *	@param strXMLTagValue [in] - the value to be wrapped by the XML tag.
 *
 *	@param bCData [in] - whether or not to wrap the value as a CData section.
 *
 */
function SCA_wrapXMLTag(strXMLTagName, strXMLTagValue, bCData) {
	if (bCData) 
	{
		// wrap with CDATA
		return "<" + strXMLTagName + ">" + IDS_XML_CDATA_BEGIN + strXMLTagValue + IDS_XML_CDATA_END + "</" + strXMLTagName + ">";
	} 
	else 
	{
		// simply wrap the value with XML tag
		return "<" + strXMLTagName + ">" + strXMLTagValue + "</" + strXMLTagName + ">";
	}
}

var IDS_ID_BUTTON_ADD ="btn_NTAdd";
var IDS_ID_ACCESS_UPDATE = "AccessUpdate";
var IDS_ID_BUTTON_ADD_ACTIVEDIR = "btn_adContainerAdd";
/**
 *
 * [Public] Change Mouse icon to wait
 *
 */
function SCA_onClickAddChangeMouseIconToWait(strID1,strID2) 
{
	if ((document.all[strID1] != null) && (document.all[strID2] != null))
		{
			document.all[strID1].style.cursor = "wait";
			document.all[strID2].style.cursor = "wait";			
			
		}
}


 