/*================================================================================================
/ Name					: SetSelectedIndex
/ Description			: Set the index for given combo box value. -1 return if combo 
/						  box is empty or value not found.
/ Inputs (Parameters)	: strCombo - Combo name.
/						  strValue - Compare value.
/ Outputs (Parameters)	: None.
/
/================================================================================================*/
function SetSelectedIndex(strCombo, strValue)
{
	var intSelectedIndex = -1;
	var intCounter;
	var strCompareValue = strValue;
	
	// Create the control reference.
	var strThis = strCombo;
	
	// Get the length of combo.
	var intElements = strThis.options.length;
	
	// Check for not empty combo.
	if (intElements > 0)
	{
		for (intCounter=0; intCounter < intElements; intCounter++)
		{
			if(strThis.options[intCounter].value == strCompareValue)
			{
				strThis.selectedIndex = intCounter;
				intSelectedIndex = intCounter;
				break;
			}
		}
	}
	
	return intSelectedIndex;
}

//*************************************************************************
//	Function	:	IsStringOnly
//	Description	:	Check the input and if it has numbers return false else true
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsStringOnly(string){
var pattern = /^[a-zA-Z\s]*$/i;
	
	var status = true;
	
	if ( string ) {
		if (string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsNumeric
//	Description	:	Check the input and if it's other then numbers return false else true
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsNumeric(string){
	var pattern = (/^[0-9]([0-9]*|([0-9]*\.[0-9]{1,2}))$/i);
	var status = true;
	if ( string ) {
		if (string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsString
//	Description	:	Check the input and if it contains a-z A-z or 0-9 But this
//					String can't have Numbers at the begining of the string ( first letter )
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsString(string) {
	var pattern = /^[a-z]{1,}(\w|\s)*$/i;
	var status = true;
	
	if ( string ) {
		if ( string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsAlpha
//	Description	:	Check the input and if it contains a-z A-z or 0-9
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsAlpha(string) {
	var pattern = /^[\w| |-|&]*$/i;
	var status = true;
	if ( string ) {
		if (string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsEmail
//	Description	:	Check the input is a valid Email address it check for "@" and "."
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsEmail(string) {
var pattern = (/^[a-z0-9](\.\w|\w|\w-\w|-\w)*@[a-z0-9](\.\w|\w|\w-\w|-\w)*\.[a-z]{2,4}$/i);
var status = true;
	if ( string ) {
		if (!pattern.test(string)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsDate
//	Description	:	Check the input is a according to the format
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************

function IsDate(string,format){
var pattern1 = /(\w+)-(\w+)-(\w+)/;
var pattern2 = /(\w+)\/(\w+)\/(\w+)/;
var patternd = /^\d{2}/g;
var patternd4 = /^\d{4}/g;
var patternw = /[^a-zA-Z]/;
var format_pattern = /(\W)/;
var status = true;
var patt = format.match(format_pattern);
var sp = patt[1];
var strMonthArray = new Array(12);
strMonthArray["JAN"] = "1";
strMonthArray["FEB"] = "2";
strMonthArray["MAR"] = "3";
strMonthArray["APR"] = "4";
strMonthArray["MAY"] = "5";
strMonthArray["JUN"] = "6";
strMonthArray["JUL"] = "7";
strMonthArray["AUG"] = "8";
strMonthArray["SEP"] = "9";
strMonthArray["OCT"] = "10";
strMonthArray["NOV"] = "11";
strMonthArray["DEC"] = "12";

	if ( ( string ) || ( format ) ) {
		if ( sp != null ) {
			if ( format == "mm" + sp + "dd" + sp + "yyyy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( (result == "") || (result.length != 3) ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 12) ) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[1].length != 2) || (result[1].search(patternd)) || (result[1] > 31)) {
						status = false;
					}
					if ( (result[2].length != 4) || (result[2].search(patternd4)) ) {
						//alert(result[3]);
						status = false;
					}
				}
				if ( status ) {
					status = DateValidate(result[0],result[1],result[2]);
				}
			}
			if ( format == "dd" + sp + "mmm" + sp + "yyyy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( result.length != 3 ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 31) ) {
						//alert(result[0]);
						status = false;
					}
					if ( (result[1].length != 3) || (result[1].match(patternw)) ) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[2].length != 4) || (result[2].search(patternd4)) ) {
						//alert(result[2]);
						status = false;
					}
					
				}
				if ( status ) {
					status = DateValidate(strMonthArray[result[1].toUpperCase()],result[0],result[2]);
					//alert(status);
				}				
			}
			if ( format == "dd" + sp + "mm" + sp + "yyyy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( result.length != 3 ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 31) ) {
						//alert(result[0]);
						status = false;
					}
					if ( (result[1].length != 2) || (result[1].search(patternd)) || (result[1] > 12) ) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[2].length != 4) || (result[2].search(patternd)) ) {
						//alert(result[2]);
						status = false;
					}
				}
				if ( status ) {
					status = DateValidate(result[1],result[0],result[2]);
				}				
			}
			if ( format == "mm" + sp + "dd" + sp + "yy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( result.length != 3 ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 12) ) {
						//alert(result[0]);
						status = false;
					}
					if ( (result[1].length != 3) || (result[1].search(patternd)) || (result[1] > 31) ) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[2].length != 2) || (result[2].search(patternd)) ) {
						//alert(result[2]);
						status = false;
					}
				}
				if ( status ) {
					status = DateValidate(result[0],result[1],result[2]);
				}
			}
			if ( format == "dd" + sp + "mmm" + sp + "yy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( result.length != 3 ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 31) ) {
						//alert(result[0]);
						status = false;
					}
					if ( (result[1].length != 3) || (result[1].match(patternw)) ) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[2].length != 2) || (result[2].search(patternd)) ) {
						//alert(result[2]);
						status = false;
					}
				}
				if ( status ) {
					status = DateValidate(strMonthArray[result[1].toUpperCase()],result[0],result[2]);
				}				
				
			}
			if ( format == "dd" + sp + "mm" + sp + "yy" ) {
				if ( sp == "-" ) {
					var result = string.split("-");
				} else if ( sp == "/" ) {
					var result = string.split('/');
				}
				if ( result.length != 3 ) {
					status = false;
				} else {
					if ( (result[0].length != 2) || (result[0].search(patternd)) || (result[0] > 31) ) {
						//alert(result[0]);
						status = false;
					}
					if ( (result[1].length != 2) || (result[1].search(patternd)) || (result[1] > 12)) {
						//alert(result[1]);
						status = false;
					}
					if ( (result[2].length != 2) || (result[2].search(patternd)) ) {
						//alert(result[2]);
						status = false;
					}
				}
				if ( status ) {
					status = DateValidate(result[1],result[0],result[2]);
				}				
			}
		} else {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

function DateValidate(mm,dd,yyyy) {
var status = true;

	if (mm<1 || mm>12) status = false
	if (dd<1 || dd>31) status = false
	if (yyyy<0 || yyyy>9999) status = false
	if (mm==4 || mm==6 || mm==9 || mm==11){
		if (dd==31) { status = false; }
	}
	if ( mm == 2 ) {
		if ( LeapYear(yyyy) ) {
			if ( dd > 29 ) {
				status = false;
			}
		} else {
			if ( dd > 28 ) {
				status = false;
			}
		}
	}
	return status;
}

function LeapYear(intYear) {

	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

//*************************************************************************
//	Function	:	blnIsPhone
//	Description	:	This function check the phone number according to the min and Max Length.
//	Pre			:	Nothing
//  Input		:	Phone Number and min and the Max sizes.
//	Returns		:	True or False according to the input
//************************************************************************

function IsPhone( str, intMinLength, intMaxLength){
var intPos;
var pattern = /^([\(\)\s0-9+-]*|n\\a|n\/a|-)$/i;
var blnRetVal = true;
 
	intPos = str.search(pattern);
	if (( intPos == -1) || (str.length < intMinLength ) || (str.length > intMaxLength ))
	{
		blnRetVal = false;
	}
	return blnRetVal;
}

//*************************************************************************
//	Function	:	IsUserName
//	Description	:	This function check if it's ok for user name.
//	Pre			:	Nothing
//  Input		:	User Name and min and the Max sizes.
//	Returns		:	True or False according to the input
//************************************************************************

function IsUserName( str, intMinLength, intMaxLength) {
var intPos;
var blnRetVal = true;
  
	intPos = str.search(/^[a-z][a-z0-9_]*$/i);

	if (( intPos == -1) || (str.length < intMinLength ) || (str.length > intMaxLength ))
	{
		blnRetVal = false;
	}
	return blnRetVal;
}

function IsAddress(string) {
	var pattern = /[^a-zA-Z0-9_ \\n]/;
	var pattern1 = /\d/;
	var status = true;
	if ( string ) {
		if (pattern.test(string)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsSqlFree
//	Description	:	Check the input for ( #,",' ) if found return false.
//	Pre			:	Nothing
//	Returns		:	True or False according to the input
//************************************************************************
function IsSqlFree(string){
var status = true;
var pattern = /^[#"]*$/i;

	if ( string ) {
		if (string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;

}

//*************************************************************************
//	Function	:	IsSpaces
//	Description	:	Check One or more spaces.
//	Pre			:	Nothing
//	Returns		:	False if the string is all spacess or True if is not
//************************************************************************
function IsSpaces(string){
var status = true;
var pattern = /^[\s]*$/ig;

	if ( string ) {
		if (!string.search(pattern)) {
			status = false;
		}
	} else {
		status = false;
	}
	return status;
}

//*************************************************************************
//	Function	:	IsIp
//	Description	:	Check the valid IP address.
//	Pre			:	Nothing
//	Returns		:	false if the IP is invalid
//************************************************************************
function IsIp(string1)
{
//Creating a new regular expression object and assign the pattern
var reg = new RegExp(/^[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}$/i);
var ObjStr; // Variable for the String Object
var IP;
	if (reg.test(string1) == true)
	{
		//Using the split command to split the string to create an array
		ObjStr = new String(string1);
		IP = ObjStr.split('.');
		if ((IP[0] > 255) || (IP[1] > 255) || (IP[2] > 255) || (IP[3] > 255))
			return false;
		else
			return true;
	}
	return false;
}

//*************************************************************************
//	Function	:	IsWWW
//	Description	:	Checking for a vaild web site address.
//	Pre			:	Nothing
//	Returns		:	false if the url does not meet the criteria
//************************************************************************
function IsWWW(string)
{
	//Creating a new regular expression object and assign the pattern
	var reg = new RegExp(/^[a-z](\.\w|\w|-)*\.[a-z-]{2,3}$/i);
	return reg.test(string);
}

//*************************************************************************
//	Function	:	IsWWWDir
//	Description	:	Checking for a vaild directory names in a url
//	Pre			:	Nothing
//	Returns		:	false if the url does not meet the criteria. Allows (_,/,\,0-9,a-z,A-Z)
//************************************************************************
function IsWWWDir(string)
{
var status = true;
//var pattern = (/^[A-Z,a-z,0-9,_,/,\\,.]*$/i);
var pattern = (/^[\w,/,\\,.]*$/i);

	if ( string ) {
		if (!string.search(pattern)) 
		{
			status = false;
		}
	} 
	else {
		status = false;
	}
	return status;
}
//format of the telephone number shoulb be 11-11-11-11-11
function IsTelephone(string){
	var patern=/^[0-9]{2}( [0-9]{2}){4}$/;
	if (patern.test(string))
		return true;
	else
		return false;
}

//format of the telephone number shoulb be 11-11
function IsOffTelephone(string){
	var patern=/^[0-9]{2}( [0-9]{2})$/;
	if (patern.test(string))
		return true;
	else
		return false;
}

var Digits_TO_Leave = "1234567890"

function StripCharsMethod(num){

            var formatted_numbers = '';

            for (var i=0; i<num.length; i++){

                        for(var c=0; c<Digits_TO_Leave.length;c++){

                                    if (Digits_TO_Leave.charAt(c)==num.charAt(i))

                                                formatted_numbers = formatted_numbers + num.charAt(i);

                        }

            }

            return formatted_numbers;

}

function TeleFormat(telephone){

            var newstr='';

            str = StripCharsMethod(telephone);

            len = parseInt(str.length/2);

            for (i=0;i<len+1;i++){

                        newstr = newstr + str.substring(0,2);

                        str = str.substring(2,str.length);

                        if (str.length>0)

                                    newstr = newstr + ' ';

            }

            return newstr;
}

//remove characters from a number field
var numbers = "1234567890- "
function stripChars(num)
{   formatted_numbers = '';
    for (var i=0; i<num.length; i++)
		{for(var c=0; c<numbers.length;c++)
			{if (numbers.charAt(c)==num.charAt(i)) 
				formatted_numbers = formatted_numbers + num.charAt(i)}
	    }
    return formatted_numbers;}