/*
Tools.js - generic functions useful to various applications (see appportal.js for stuff specific to AppPortal)
*/


function resetDotNetScrollPosition() {
	$("#__SCROLLPOSITIONX").val("0");
	$("#__SCROLLPOSITIONY").val("0");
	
	/*
	var scrollX = document.getElementById('__SCROLLPOSITIONX');
	var scrollY = document.getElementById('__SCROLLPOSITIONY');

	if (scrollX != null && scrollY != null) {
		scrollX.value = 0;
		scrollY.value = 0;
	}
	*/
}

function GetAt(InputString, Delimiter, Index) {
	var array = InputString.split(Delimiter);
	return array[Index];
}

// highlight any fields that are invalid by looking for the ASP.NET Valitor's "!"
// for some reason, span:not(:visible) selects the visible validators - maybe
// because their css visibility is not specified? (the hidden ones have visibility:hidden in their style attribute).
function HighlightFailedValidators() {
	//alert("hi");
	//$("span:contains('!')").not("[style*='color:Red;visibility:hidden;']").parent().addClass("invalid");
	// $("span:not(:visible)").parent().addClass("invalid");
    //  :contains('!') [id*='Validator']
}

/*
Multiline TextBoxes ignore MaxLength (as they render to Html <textarea>s). So you need this. 
You have to put it on the onblur event and the onkeypress event, so they can't paste or type 
beyond the given limit.

Usage example: 
$("textarea").bind("blur", function(e) {
		KeepToMaxLength(this, false);
	});
	$("textarea").bind("keypress", function(e) {
		KeepToMaxLength(this, true);
	});
*/
function KeepToMaxLength(TextArea, IsKeyPress, MaxLength) {
	var limit = 99999;

	// if max length not explicitly specified, find it using attributes
	if (MaxLength == null || MaxLength < 1) {
		jo = $('#' + TextArea.id);
		if (jo.attr("MaxLength") != null && jo.attr("MaxLength") > 0) {
			limit = jo.attr("MaxLength");
		}
		else if (jo.attr("MaxChars") != null && jo.attr("MaxChars") > 0) {
			limit = jo.attr("MaxChars");
		}
	}
	else {
		limit = MaxLength;
	}

	// for some reason, with keypress event, need to set to one less:
	if (IsKeyPress)
	{
		limit = limit - 1;
	}

	// Cut off any chars above limit
	if (TextArea.value.length >= limit) {
		TextArea.value = jQuery.trim(TextArea.value.substring(0, limit));
	}
}


function CreateElement(Parent, TagName, Id, InnerHtml) {
	elem = document.createElement(TagName);
	
	elem.setAttribute('id', Id);
	elem.setAttribute('name', Id);
	elem.innerHTML = InnerHtml;
	
	Parent.appendChild(elem);
	return elem;
}

// Adds .left(n) substring function to string prototype, do you can do this:
//		"ControlNumberOne".left(7)			// this returns "One"
// n is the number of chars to return
String.prototype.left = function(n) {
	LeftOrRight(this, true, n);
}

// Adds .right(n) substring function to string prototype, do you can do this:
//		"ControlNumberOne".right(3)			// this returns "One"
// n is the number of chars to return
String.prototype.right = function(n) {
	LeftOrRight(this, false, n);
}

// return the specified number of characters (NumChars) from 
// the left (if DoLeft is true, otherwise the right) of the given string (Source)
function LeftOrRight(InString, DoLeft, NumChars) {
	
	// check for negative/too-long numchars
	if (NumChars <= 0) {
		return "";
	}
	else if (NumChars > InString.length) {
		return InString;
	}
	else {
		if (DoLeft) {
			// left
			return InString.substring(0, NumChars);
		}
		else {
			// right
			var len = InString.length;
			return InString.substring(len, len - NumChars);
		}
	}
}

// Adds .trimLeftUpToLast(n) substring function to string prototype, do you can do this:
//		"ctl100_control_SomeButtonId".trimLeftUpToLast("_")			// this returns "SomeButtonId"
// n is the char just before the substring to return (if there are multiple, it uses the right-most)
String.prototype.trimLeftUpToLast = function(token) {
	var lastTokenIndex = this.lastIndexOf(token);
	//var lengthToReturn = this.length - lastTokenIndex;
	return this.slice(lastTokenIndex + 1);
}

// Adds .splitAndReturn(delimiter, index, countFromRight) substring function to string prototype, do you can do this:
//		"Hey_you_mofo".splitAndReturn("_", 1, false)			// this returns "you"
//		"Hey_you_mofo".splitAndReturn("_", 0, true)				// this returns "mofo"
String.prototype.splitAndReturn = function(delimiter, index, countFromRight) {

	var arr = this.split(delimiter);

	// figure out which element to return depending on whether counting from left (start of array) or right (end of array)
	var i;
	if (countFromRight) {
		i = (arr.length - 1) - index;
	}
	else {
		i = index;
	}

	return arr[i];
}


function EnableTextArea(TextAreaID, ValidatorID) 
{
    var obj = document.getElementById(TextAreaID);
    obj.readOnly = false;
    var validobj = document.getElementById(ValidatorID);
    ValidatorEnable(validobj, true); 

}

function DisableTextArea(TextAreaID, ValidatorID) 
{
    var obj = document.getElementById(TextAreaID);
    obj.readOnly = true;
    var validobj = document.getElementById(ValidatorID);
    ValidatorEnable(validobj, false); 
}
