/*	FUNCTION ALLTRIM 	*/
/*	Usage -> alltrim(string)	*/
function alltrim(para){
var word, temp, chr, flag;

word = para;
word = rtrim(ltrim(word))
return word;
}


/*	FUNCTION LTRIM 		*/
/*	Usage -> ltrim(string)	*/
function ltrim(para){
var word, temp, chr, flag;
word = para;
if (word.length>0){
	flag = false;
	temp = '';
	for (i=0;i<word.length;i++){
		chr = word.charAt(i);
		if (chr!=' '){
			flag = true;
		}
		if (flag==true){
			temp = temp + chr;
		}
	}
	word = temp;
}
return word;
}


/*	FUNCTION RTRIM 		*/
/*	Usage -> rtrim(string)	*/
function rtrim(para){
var word, temp, chr, flag;
word = para;
if (word.length>0){
	flag = false;
	temp = '';
	for (i=word.length - 1;i>=0;i--){
		chr = word.charAt(i);
		if (chr!=' '){
			flag = true;
		}
		if (flag==true){
			temp = chr + temp;
		}
	}
	word = temp;
}
return word;
}

/*	FUNCTION ISDIGIT	*/
/*	Usage -> isDigit(string, integer [Optional])	*/
function isDigit(para) {
	var validchars = "0123456789";
	var parsed = true;
	for (var i=0; i < para.length; i++) {
		var letter = para.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
			continue;
		if (isDigit.arguments.length==2){
			if (isDigit.arguments[1] == 0){
				alert(letter + " is not a digit. Please enter a digit.");
			}
		}else{
			alert(letter + " is not a digit. Please enter a digit.");
		}
		parsed = false;
		break;
	}
	return parsed;
}

/*	FUNCTION ISALPHA	*/
/*	Usage -> isAlpha(string)	*/
function isAlpha(para) {
	var validchars = "abcdefghijklmnopqrstuvwxyz";
	var parsed = true;
		var letter = para.toLowerCase();
		if (validchars.indexOf(letter) == -1)		
			parsed = false;
	return parsed;
}

/*	FUNCTION BLOCKCHARS	*/
/*	Usage -> blockChars(Checking String, Blocking Character List, integer [Optional])	*/
function blockChars(string, para) {
	var validchars = para;
	var parsed = true;
	for (var i=0; i < string.length; i++) {
		var letter = string.charAt(i);
		if (validchars.indexOf(letter) == -1)
			continue;
		if (blockChars.arguments.length==3){
			if (blockChars.arguments[2] == 0){
				alert(letter + " is not allowed.");
			}
		}else{
			alert(letter + " is not allowed.");
		}
		parsed = false;
		break;
	}
	return parsed;
}

/*	FUNCTION AllOWLOGINCHARS	*/
/*	Usage -> allowLoginChars(Checking String, integer [Optional])	*/
function allowLoginChars(string) {
	var parsed = true;
	var blockCharacters = "~`!@#$%^&*()-+=|\\{[}]:;\"'<,>.?/ ";
	parsed = blockChars(string, blockCharacters,1);
	if (parsed==false){
		if ((allowLoginChars.arguments.length==1) || 
		(allowLoginChars.arguments.length==2 && allowLoginChars.arguments[1] == 0)){
			alert("Invalid Character(s) Used!!!\nAllowed Characters: [A-Z], [a-z], [0-9], _");
		}
	}
	return parsed;
}


/*	FUNCTION AllOWSKUCHARS	*/
/*	Usage -> allowSkuChars(Checking String, integer [Optional])	*/
function allowSkuChars(string) {
	var parsed = true;
	var blockCharacters = "~`!@#$%^&*()_+=|\\{[}]:;\"'<,>.?/ ";
	parsed = blockChars(string, blockCharacters,1);
	if (parsed==false){
		if ((allowSkuChars.arguments.length==1) || 
		(allowSkuChars.arguments.length==2 && allowSkuChars.arguments[1] == 0)){
			alert("Invalid Character(s) Used!!!\nAllowed Characters: [A-Z], [a-z], [0-9], -");
		}
	}
	return parsed;
}

/*	FUNCTION ISNUMBER	*/
/*	Usage -> isNumber(string)	*/
function isNumber(para) {
	if (isNaN(para)==true)
		return false;
	else
		return true;

}

/*	FUNCTION CHECKEMAIL	*/
/*	Usage -> checkEmail(string)	*/
function checkEmail (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("The email address doesn't seem to be valid.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

return true;
}

/*	FUNCTION STRCMP 	*/
/*	Usage -> strcmp(string1,string2)	*/
function strcmp(para1, para2){
var i, flag;
if (para1.length != para2.length){
	return false;
}

flag = true;
for(i=0;i<para1.length;i++){
	if (para1.substring(i,i+1) == para2.substring(i,i+1)){
		continue;
	}
	flag = false;
	break;
}
return flag;
}


/*	FUNCTION ICAP 	*/
/*	Usage -> iCap(string1)	*/
function iCap(para){
var i, c, flag;
var newstring = '';
flag = true;
for(i=0;i<para.length;i++){
	c = para.substring(i,i+1);
	if (flag==true){
		c = c.toUpperCase();
		flag = false;
	}
	else{
		c = c.toLowerCase();
	}
	if (c == ' '){
		flag = true;
	}
	newstring = newstring + c;
}
return newstring;
}



/*      FUNCTION DATECHECK      */
/*      Usage -> dateCheck(textbox object) */
function dateCheck(obj){
var dd, mm, yy;
var ddstr, mmstr, yystr;
var mmname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
obj.value = alltrim(obj.value);

	if (obj.value.length > 0){
		if (obj.value.length < 10){
			alert('Invalid date format !!!\n\n' + 'Correct Format:\n' + 'mm/dd/yyyy');
			obj.select();
			obj.focus();
			return false;
		}else{
			mmstr = obj.value.substring(0,2);
			ddstr = obj.value.substring(3,5);
			yystr = obj.value.substring(6,10);

			if ((isNaN(ddstr)==true) || (isNaN(mmstr)==true) || (isNaN(yystr)==true)){
				alert('Invalid Characters');
				obj.select();
				obj.focus();
				return false;
			}

			if ((obj.value.substring(2,3)!='/') || (obj.value.substring(5,6)!='/')){
				obj.value = mmstr + '/' + ddstr + '/' + yystr;
			}

/* Converting to Numeric Date */
			dd = parseInt(ddstr);
			mm = parseInt(mmstr);
			yy = parseInt(yystr);

			if (mm >12){
				alert('Invalid Month !!!');
				obj.select();
				obj.focus();
				return false;
			}

/* Check For the Month of February */
			if ((mm == 2) && (yy%4 ==0) && (dd>29)){
				alert('Invalid Date !!!\nFebruary has 29 days in ' + yystr);
				obj.select();
				obj.focus();
				return false;
			} else if ((mm == 2) && (yy%4 !=0) && (dd>28)){
				alert('Invalid Date !!!\nFebruary has 28 days in ' + yystr);
				obj.select();
				obj.focus();
				return false;
			}

/* Check For the Months having 30 days */
			if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11))&& (dd>30)){
				alert('Invalid Date !!!\n' + mmname[mm-1] + ' has 30 days.');
				obj.select();
				obj.focus();
				return false;
			}
			
/* Check For the Month having 31 days */
			if (((mm == 1) || (mm == 3) || (mm == 5) || (mm == 7) || (mm == 8) || (mm == 10) || (mm == 12))&& (dd>31)){
				alert('Invalid Date !!!\n' + mmname[mm-1] + ' has 31 days.');
				obj.select();
				obj.focus();
				return false;
			}
		}
	}
return true;
}


/* FUNCTION DROPDOWN_DATECHECK */
/* Usage -> dropdown_datecheck(date dropdown Object, month dropdown Object, year dropdown Object) */
function dropdown_datecheck(dateObj,monthObj,yearObj)
{
	var dd;
	var mm;
	var yy;
	var mmname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	dd=parseInt(dateObj[dateObj.selectedIndex].value);
	mm=parseInt(monthObj[monthObj.selectedIndex].value);
	yy=parseInt(yearObj[yearObj.selectedIndex].value);
	if ((mm == 2) && (yy%4 ==0) && (dd>29))
	{
		alert('Invalid Date !!!\nFebruary has 29 days in ' + yy);
		//dateObj.select();
		dateObj.focus();
		return false;
	}
	else if ((mm == 2) && (yy%4 !=0) && (dd>28))
	{
		alert('Invalid Date !!!\nFebruary has 28 days in ' + yy);
		//dateObj.select();
		dateObj.focus();
		return false;
	}

	if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11))&& (dd>30))
	{
		alert('Invalid Date !!!\n' + mmname[mm-1] + ' has 30 days.');
		//dateObj.select();
		dateObj.focus();
		return false;
	}
	return true;
}


/*	FUNCTION FORMATDATE 	*/
/*	Usage -> formatDate(string [optional])	*/
function formatDate(){
today = new Date();
if (formatDate.arguments.length!=1){
	delim='-';
}
else{
	delim = alltrim(formatDate.arguments[0]);
}
return (today.getFullYear() + delim + Ctos(today.getMonth() + 1) + delim+Ctos(today.getDate()));
}


/*	FUNCTION CTOS 	*/
/*	Usage -> Ctos(string)	*/
function Ctos(para){
if (para<10){
	return ("0" + para);
}
	return para;
}

/*  CHECK DOMAINS */
/* Usage -> checkDomain(String domainname) */
function checkDomain(fieldName)
{

if( (fieldName == null) || 
(fieldName.length == 0 ) ||
(fieldName.indexOf("!") != -1)||
(fieldName.indexOf("@") != -1) ||
(fieldName.indexOf("#") != -1   )||
(fieldName.indexOf("$") != -1   )||
(fieldName.indexOf("%") != -1   )||
(fieldName.indexOf("^") != -1   )||
(fieldName.indexOf("*") != -1   )||
(fieldName.indexOf(",") != -1   )||
(fieldName.indexOf(".") != -1   )||
(fieldName.indexOf("_") != -1   )||
(fieldName.indexOf("+") != -1   )||
(fieldName.indexOf("=") != -1   )||
(fieldName.indexOf("{") != -1   )||
(fieldName.indexOf("}") != -1   )||
(fieldName.indexOf("[") != -1   )||
(fieldName.indexOf("]") != -1   )||
(fieldName.indexOf(":") != -1   )||
(fieldName.indexOf(";") != -1   )||
(fieldName.indexOf(">") != -1   )||
(fieldName.indexOf("|") != -1   )||
(fieldName.indexOf("<") != -1   )||
(fieldName.indexOf("&") != -1   )||
(fieldName.indexOf("(") != -1   )||
(fieldName.indexOf(")") != -1   )||
(fieldName.indexOf("\"") != -1   )||
(fieldName.indexOf("~") != -1   )||
(fieldName.indexOf("'") != -1   )||
(fieldName.indexOf(" ") != -1   )||
(fieldName.indexOf("www") != -1   )||
(fieldName.indexOf("?") != -1   )||
(fieldName.indexOf(".") != -1   ))
{
		alert("Please enter valid characters and do not leave field empty.");
		return false;
}
else
{
	
	return true;
}
}



/*	FUNCTION CBOXCHECKED 	*/
/*	Usage -> cboxChecked(checkbox Object)	*/
/*
---------------------------------------------------
This function can checkout if the given checkbox or 
atleast one option of checkbox array is checked.
Parameter checkbox -> Object reference variable of FORM CHECKBOX [Single or Array]
---------------------------------------------------
*/
function cboxChecked(obj){
	var option_selected = false;
	if (typeof obj.length =='undefined'){
		option_selected = obj.checked;
	}else{
		for(i=0;i<obj.length && option_selected==false;i++){
			option_selected = obj[i].checked;
		}
	}
	if (option_selected == true){
		return true;
	}else{
		return false;
	}
}


/*	FUNCTION KITKAT 	*/
/*	Usage -> KitKat(ParentPathName String, ObjName String, RelObjName String)	*/
/*
---------------------------------------------------
This function can checkout if the given checkbox or 
atleast one option of checkbox array is checked.
---------------------------------------------------
*/
function KitKat(ParentObjName, ObjName, RelObjName){
	var Obj = ParentObjName + "." + ObjName;
	var relObj = ParentObjName + "." + RelObjName;
	
	if (typeof eval(Obj).length == 'undefined'){
		eval(relObj).checked = !(eval(Obj).checked);
	}else{
		for(var i=0; i<eval(Obj).length;i++){
			tmpObj = eval(ParentObjName + "." + ObjName + "[" + i + "]");
			tmprelObj = eval(ParentObjName + "." + RelObjName + "[" + i + "]");
			tmprelObj.checked = !(tmpObj.checked);
		}
	}
}