﻿

//DATE VALIDATION//
var dtCh= "/";
var MinYear=1900;
var MaxYear=2100;

function IsValidDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (month<1 || month>12){
		return false
	}
	if (day<1 || day>31 || (month==2 && day>DaysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<MinYear || year>MaxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || IsInteger(StripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	return true
}

function IsInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function StripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function DaysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}



// GOT TO THE BOTTOM PAGE
function ScrollToBottom(){
	setTimeout('Scrollit()',100);
}

function Scrollit(){
	var PageHeight = 0;
	
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    PageHeight = window.innerHeight;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    PageHeight = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    PageHeight = document.body.clientHeight;
  }
	window.scrollBy(0,Number(PageHeight));
}


// STRING MANIPULATION //

function LimitTextBox(ElementId,CounterDisplay,Limit,event)
{
	var txtValue = document.getElementById(ElementId).value;
	var txtLength = txtValue.length;
	var txtLimit = Limit;
	
	var txtCounterValue = document.getElementById(CounterDisplay).innerHTML;
	var LastCursorPosition = Number(cursorPosition(ElementId));
	
	if (txtLength > txtLimit)
	{	 
		 if (LastCursorPosition > txtLimit)
		 {
				txtValue = txtValue.substr(0, (txtLength -(txtLength - txtLimit)));
		 }
		 else if (LastCursorPosition >= 0 && LastCursorPosition < txtLimit)
		 {
				var FirstPart = txtValue.substr(0, LastCursorPosition - (txtLength - txtLimit));
				var LastPart = txtValue.substr(LastCursorPosition, txtLimit);
				txtValue = FirstPart + LastPart;			
		 }
	}
	document.getElementById(ElementId).value = txtValue;
	document.getElementById(ElementId).scrollTop = document.getElementById(ElementId).scrollHeight;
	
	
	if ((txtLimit - txtLength)>=0)
	{
		 txtCounterValue = txtLimit - txtLength;
	}
	else
	{
		txtCounterValue = 0;
	}
	document.getElementById(CounterDisplay).innerHTML = txtCounterValue;
 	
}

function setTextAreasOnFocus(ElementId) 
{

    var textAreas = document.getElementById(ElementId);

    for(var i = 0; i < textAreas.length; i++) {
        textAreas[i].onfocus = function() {
            setCaretPosition(ElementId, textAreas.value.length);
        }
    }
    textAreas = null;
}


function setCaretPosition(ElementId, caretPos) {
    var elem = document.getElementById(ElementId);

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}



function cursorPosition(ElementId){
	var textarea = document.getElementById(ElementId);
	var ReturnCursorPosition = 0;
	textarea.focus();
	
	// get selection in firefox, opera, ...

	if (typeof(textarea.selectionStart) == 'number')

	{	
		
		ReturnCursorPosition = textarea.selectionStart;
		//alert(textarea.selectionStart);
		
		
		//if (textarea.length > 0 && textarea.length <= 30)
		//	{ alert(textarea.selectionStart); }
		
	}
	else if(document.selection){
		var selection_range = document.selection.createRange().duplicate();

		if (selection_range.parentElement() == textarea) {    // Check that the selection is actually in our textarea
		// Create three ranges, one containing all the text before the selection,
		// one containing all the text in the selection (this already exists), and one containing all
		// the text after the selection.
		var before_range = document.body.createTextRange();
		before_range.moveToElementText(textarea);                    // Selects all the text
		before_range.setEndPoint("EndToStart", selection_range);     // Moves the end where we need it

		var after_range = document.body.createTextRange();
		after_range.moveToElementText(textarea);                     // Selects all the text
		after_range.setEndPoint("StartToEnd", selection_range);      // Moves the start where we need it

		var before_finished = false, selection_finished = false, after_finished = false;
		var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text;

		// Load the text values we need to compare
		before_text = untrimmed_before_text = before_range.text;
		selection_text = untrimmed_selection_text = selection_range.text;
		after_text = untrimmed_after_text = after_range.text;

		// Check each range for trimmed newlines by shrinking the range by 1 character and seeing
		// if the text property has changed.  If it has not changed then we know that IE has trimmed
		// a \r\n from the end.
		do {
		  if (!before_finished) {
		      if (before_range.compareEndPoints("StartToEnd", before_range) == 0) {
		          before_finished = true;
		      } else {
		          before_range.moveEnd("character", -1)
		          if (before_range.text == before_text) {
		              untrimmed_before_text += "\r\n";
		          } else {
		              before_finished = true;
		          }
		      }
		  }
		  if (!selection_finished) {
		      if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) {
		          selection_finished = true;
		      } else {
		          selection_range.moveEnd("character", -1)
		          if (selection_range.text == selection_text) {
		              untrimmed_selection_text += "\r\n";
		          } else {
		              selection_finished = true;
		          }
		      }
		  }
		  if (!after_finished) {
		      if (after_range.compareEndPoints("StartToEnd", after_range) == 0) {
		          after_finished = true;
		      } else {
		          after_range.moveEnd("character", -1)
		          if (after_range.text == after_text) {
		              untrimmed_after_text += "\r\n";
		          } else {
		              after_finished = true;
		          }
		      }
		  }

		} while ((!before_finished || !selection_finished || !after_finished));

		// Untrimmed success test to make sure our results match what is actually in the textarea
		// This can be removed once you're confident it's working correctly
		var untrimmed_text = untrimmed_before_text + untrimmed_selection_text + untrimmed_after_text;
		var untrimmed_successful = false;
		if (textarea.value == untrimmed_text) {
		  untrimmed_successful = true;
		}
		// ** END Untrimmed success test

		var startPoint = untrimmed_before_text.length;
		ReturnCursorPosition = untrimmed_before_text.length;
		//alert(startPoint);
		
		}
	}
	return ReturnCursorPosition;
}


function AlertInvalidSearch()
{
	var txtValue = document.getElementById('ctl00_txtSearch').value;
	var txtLength = txtValue.length;
	if (txtLength == 0)
	{
		alert('Please enter a search value.');
		document.getElementById('ctl00_txtSearch').focus();
	}
}
  

