
function fiGetSubStringPos(asString, asSubString, abCaseSensitive)
{
// Return: 0 - n, -1 = not found
var lsString
var lsSubString

	lsString = new String(asString)
	lsSubString = new String(asSubString)

	if(!abCaseSensitive)
	{
		lsString = lsString.toLowerCase()
		lsSubString = lsSubString.toLowerCase()
	}

	return lsString.indexOf(lsSubString)
}

// Get the current logged in account
// See also: fsGetType()
function fsGetAccount()
{
var lsAccount = fsGetURLItem(0)

	// Default to
	if(!lsAccount){lsAccount = "demo"}
	return lsAccount
}

// Get the type for Poll (such as Guest book)
// This should be a value passed along with the URL
// See also: fsGetTypePrompt
function fsGetType()
{
var lsType = fsGetURLItem(1)

	// Default to
	if(!lsType){lsType = "Poll"}
	return lsType
}

function fsGetTypePrompt()
{
var lsType = fsGetType()
	if(lsType == "Poll"){ return "Poll / Survey"}
	if(lsType == "FormMail"){ return "Form Mail"}
	if(lsType == "GuestBook"){ return "Guest Book"}
	return lsType;
}

function fsGetId()
{
var lsValue = fsGetURLItem(2)

	// Default to
	if(!lsValue){lsValue = "demo_1"}
	return lsValue
}

function fsGetSection()
{
var lsValue = fsGetURLItem(3)

	// Default to, invalid anyway
	if(!lsValue){lsValue = "0"}
	return lsValue
}



/* To generete the logic to execute a pl script
Rules:
- Use xxx/d.pl as the door script
- Take optional parms
* * * 
- asList: such as c_TNMBZ_exist
*/

function fsGetPerl(asPerl, asParms)
{
var lsPerl = "https://wolf.liquidweb.com/~tnm/d/d.pl?" + asPerl

	if(asParms != undefined){lsPerl += "_TNMBZ_" + asParms}
	return "<script src='" + lsPerl + "'></script>"
}

// Assign the index for the dropdown
function fvSetDropdownDefaultValue(lobj, asValue)
{
//var lobj = document.frm_Form.cmb_EmailInterval
var liTh = 0
var liTotal = lobj.length



	for(liTh = 0; liTh < liTotal; liTh ++)
	{
		if(lobj.options[liTh].value == asValue)
		{
			lobj.selectedIndex = liTh
			return
		}

	}

}

// Get the value from the list
function fsGetDropdownValue(lobj)
{

	// Return "" instead of "undefined"
	if(!lobj){return ""}
	if(lobj.selectedIndex < 0){return ""}
	return lobj.options[lobj.selectedIndex].value
}

/* Extract the account and type
- aiIndex: 0 - account, 1 - type (for poll)
 e.g.: https://wolf.liquidweb.com/~tnm/d/d.pl?p_TNMBZ_editwebpage_TNMBZ_test&demo_1&*demo_2
*/
function fsGetURLItem(aiIndex)
{
var lsURL = fsProperURL()

// 1) Get rid of anything before all _TNMBZ_ if 
// the url is a pl call via d.pl
// such as"https://wolf.liquidweb.com/~tnm/d/d.pl?p_TNMBZ_editwebpage_TNMBZ_test&demo_1&*demo_2"
// 2) Otherwise use the URL as is (such as tnmbiz_Online_HitCount.htm?account&xxxx
var lsDelimitor = lsURL.indexOf("_TNMBZ_") >= 0 ? "_TNMBZ_" : "?"

	// Remove caller= clause first
	// Otherwise get id, etc. won't work
var lsArray = lsURL.split("&caller=")
	if(!lsArray){return ""}
	lsURL = lsArray[0]


	lsArray = lsURL.split(lsDelimitor)

	if(!lsArray){return ""}
	// No parm
	if(lsArray.length <= 1){return ""}

	lsURL = lsArray[lsArray.length - 1]

	// %26 -> &, global
	lsURL = fsProperURL(lsURL)
	lsArray = lsURL.split("&")

	return aiIndex < lsArray.length ? lsArray[aiIndex] : ""
}

/* To convert URL to proper format
*/
function fsProperURL(asURL)
{
	// Default
	if(!asURL){asURL = document.location.href}

	// %26 -> &, global
	return asURL.replace(/%26/g, "&")
}



// To replace the StarMark part with a new one along with & (regular parm separator)
// A bookmark # can not used since perl uses it 
// See also: fsGetDefault
function fsReplaceURLStarMark(asURL, asStarMark)
{
var liPos = fiGetSubStringPos(asURL, "&\*", true)
var lsBefore, lsAfter

	if(liPos >= 0)
	{
		lsBefore = asURL.substr(0, liPos)
	
		// Stuff after this tag
		lsAfter = asURL.substr(liPos + 2)
		if(lsAfter)
		{
			liPos = fiGetSubStringPos(lsAfter, "&", true)
			lsAfter = liPos >= 0 ? lsAfter.substr(liPos + 1) : "" 
		}
	}
	// Add to the end
	else
	{
		lsBefore = asURL
		lsAfter = ""
	}

	asURL = lsBefore + "&*" + asStarMark
	if(lsAfter){asURL += "&" + lsAfter}

	return asURL

}



// Get the default value in between &* and &
function fsGetDefault()
{
var lsURL = fsProperURL()

var lArray = lsURL.split("&*")
	if(lArray.length < 2){return}

	lArray = lArray[1].split("&")

	// Avoid word undefined
	return lArray[0] ? lArray[0] : ""		
}

// Remove all the spaces (including the ones inside of a string
function fsRemoveSpaces(asStr)
{
	if(!asStr){return}
	return asStr.replace(/ /g, "");	
}



/* Assign a value to a control
This function is used to restore user input from the server.  
It works for both IE and NC
Input:
- asObj: 
	* Radio Button: Use the prefix to decide the type only
	*    and use the value to match
	* Others: Its name
- asValue:
	* Checkbox: Not in use
	* Others: set as-is
Form:
- frm_Form is required


Samples:
fvSetControlValue("cmb_Display", "2d8")
fvSetControlValue("cx_Hide")
fvSetControlValue("rb_Dowload", "DOWNLOAD")
fvSetControlValue("txt_SourceId", "Cool Id...")

*/
function fvSetControlValue(asObj, asValue)
{

/* 	cmb_: Dropdown combobox
	cx_: Check box
	txt_: Text input
	rb_: Radio button
*/	
var lsType = asObj.substr(0, 2)
var lObj

	// No name: Value Not equired for cx 
	if(!asObj){return}
	if(lsType != "cx" && !asValue){return}
	
	//lObj = document.all.item(asObj)
	lObj = fobjGetFormObject(asObj, asValue)

	// No object
	if(!lObj){return}

	if(lsType == "cx" || lsType == "rb")
	{
		lObj.checked = true
	}
	else if(lsType == "cm")
	{
		fvSetDropdownDefaultValue(lObj, asValue)
	}
	else
	{
		lObj.value = asValue
	}


}

/* To scan the form frm_Form to extract an obj:
- Radio button: By value
- Others: By name
*/
function fobjGetFormObject(asObj, asValue)
{
	if(!asObj){return}
var lsType = asObj.substr(0, 2)

var lobjForm = document.forms["frm_Form"]
	if(!lobjForm){return}

var liTotal = lobjForm.elements.length
var liTh
var lbIs

	for(liTh = 0; liTh < liTotal; liTh++)
	{
		if(lsType == "rb")
		{
			lbIs = lobjForm.elements[liTh].value == asValue	
		}
		else
		{
			lbIs = lobjForm.elements[liTh].name == asObj					}	
	
		if(lbIs){return lobjForm.elements[liTh]}
	}


}

/* Select/unselect all checkboxes */
function fvSelectAll(afrmForm, aName, abUnselect)
{
var lObjects = afrmForm.elements[aName]
var liTotal = lObjects.length
var liTh

	for(liTh = 0; liTh < liTotal; liTh++)
	{
		lObjects[liTh].checked = !abUnselect
	}

	return
}

/* Any selection in a group of checkboxes? */
function fbAnySelection(afrmForm, aName)
{
var lObjects = afrmForm.elements[aName]
var liTotal = lObjects.length
var liTh

	for(liTh = 0; liTh < liTotal; liTh++)
	{
		if(lObjects[liTh].checked){return true}
	}

}
