﻿// JScript File
function CheckLength(src, args)
{
		if (src.value.length < 2)
		{
			args.IsValid = false;
		}else
			{
				args.IsValid = true;
			}
}

function textCounter(field,cntfield,maxlimit) 
{
	if (field.value.length > maxlimit)
	{ // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	}else{
		cntfield.value = maxlimit - field.value.length;
		}
}

function ResetForm(which)
{
    var pass = true;
    // Set the value to -1
    var firstElement = -1;
    if (document.images)
    {
        // Loop through the form
        for (var iCount = 0; iCount < which.length; iCount++)
        {
            var tempobj = which.elements[iCount];
            // If item is a textbox
            if (tempobj.type=="text")
            {
                // the text from it
                eval(tempobj.value = "");
                // Check to see if its the first element in the form
                if (firstElement == -1)
                {
                    // If it is then mark it as so
                    firstElement = iCount;
                }
            } 
            else if (tempobj.type == "select")
            {
                // Set its selectedIndex to 0
                eval(tempobj.selectedIndex = 0);
                // Check to see if its the first elemet in the form
                if (firstElement == -1)
                {
                    // Ifso then mark it as so
                    firstElement = iCount;
                }
            }
            else if (tempobj.col != "")
            {
	            // Empty the TextArea
	            eval(tempobj.value = "");
	            // Check to see if its the first elemet in the form
	            if (firstElement == -1)
	            {
	                // If so then mark it as so
	                firstElement = iCount;
	            }
            }
        }
    }
   // Set focus to the first item in the form
   which.elements[firstElement].focus();
   return false;
}

/// <summary>
/// JavaScript mthod for performing an auto-tab from
/// field to field
/// </summary>
/// <param name="fieldFrom">The field you're tabbing from</param>
/// <param name="fieldTo">The field you're tabbing to</param>
/// <param name="length">The length of the text in the fieldFrom field</param>
/// <param name="e">The event</param>
function AutoTab(fieldFrom, fieldTo, textLength, e) 
{
	if (e.which && e.which != 9 && e.which != 16) 
	{
		if (document.getElementById(fieldFrom).value.length == textLength) 
		{
			document.getElementById(fieldTo).focus();
		}
	} 
	else if (e.keyCode && e.keyCode != 9 && e.keyCode != 16) 
	{
		if (document.getElementById(fieldFrom).value.length == textLength) 
		{
			document.getElementById(fieldTo).focus();
		}
	}
}

/// <summary>
/// JavaScript method to check to see if an input value is a
///  a whole number or not
/// </summary>
/// <param name="inputStr">the value we're checking</param>
function isWholeNumber(inputStr) 
{
    //get the inputStr of the input string
	str = inputStr.toString();
	
	//now we need to loop through the length
	//of the input value
	for(var i=0; i < str.length; i++)
	{
	    //use charAt to get the current value
		var curValue = str.charAt(i);
		
		//check the value of the current value, if it's less
		//than 0 (zero) and more than 9 we know the value
		//isnt a whole number
		if(curValue < "0" || curValue > "9")
		{
			return false;	
		}
	}
	return true;
}

/// <summary>
/// JavaScript to validate an email address
/// </summary>
/// <param name="emailStr">the value we're checking</param>
function validateEmail(emailStr) 
{
    //check the lenfth of the input value, it it's
    //empty then return false as no value was provided
	if (emailStr.length <= 0) return false;
	
	//here we create an array using the match method
	//passing a regular expression
    var array = emailStr.match("^(.+)@(.+)$");
    
    //if th array is null, meaning a non-valid email was provided
    //then we return false as we dont have a valid email address
    if (array == null) return false;
    
    //no we work on the first index of our array
    //since we made it this far. First make sure it's
    //not null
    if (array[1] != null) 
    {
        //first part of a valid email address will be
        //the usernma (joe@example.com) So we determine this
        //using a regular expression
		var userName = /^\"?[\w-_\.]*\"?$/;
		
		//once again we use the match method, and if a match isnt found
		//we return false as it's not a valid email address
		if(array[1].match(userName) == null) return false;
    }
    
    //now we work on the second part of the array, the domain of the email address
    //if it's not null then we continue
    if (array[2] != null) 
    {
        //create a domain variable using a regular expression checking for
        //a valid domain name
		var domain = /^[\w-\.]+\.[A-Za-z]{2,4}$/;
		
		//here's where it can get tricky (and where most programmers miss this aspect)
		//we once again use the mztch method to see if a domain namne was found, then check
		//to see if it's null. But it it's null we take into account that a valid email
		//address can contain an IP address, not a domain
		if (array[2].match(domain) == null) 
		{
		    //since the domain portion was null we create an ipAddress
		    //variable using a regular expression pattern to validate a valid
		    //IP address range
			var ipAddress =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			
			//we then, once again, use match to see if there's a match. If no
			//match was found we return false, meaning this isnt a value email address
			if (array[2].match(ipAddress) == null) return false;
		}
		//since we've made it this far we must be deaing with a valid
		//email address so return true
		return true;
	}
	return false;
}

function setCookie(name, value) 
{
	if(value.length === 0) 
	{
		document.cookie = name  + "=;path=/;expires=Fri, 02-Jan-1970 00:00:00 GMT";
	} 
	else 
	{
		var expires = new Date(new Date().getTime() + (7 * 2 * 24) * 3600000);
		document.cookie = name + "=" + value.sort().join(",") +";path=/;expires=" + expires + ";";
	}
}

function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	if (start === null || start == -1) return null;
	var len = start + name.length + 1;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	{
	    var cookie = document.cookie.substring(len, end).split(",");
	}
	return cookie;
}

function setFormCookie(name, value, days) 
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (((((days * 24) * 60) * 60) * 1000)));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires="";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function getFormCookie(name) 
{
	var cookieName = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0;  i < ca.length; i++)
	{
		var chr=ca[i];
		while (chr.charAt(0)==' ') 
		{
		    chr  =c.substring(1,chr.length);
		}
		
		if (chr.indexOf(cookieName)==0) 
		{
		    return chr.substring(cookieName.length,chr.length);
		}
	}
	return null;
}

