/***************************************************************************************/
// General functions
/***************************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a date entered is a valid date or not.
/////////////////////////////////////////////////////////////////////////////////////////
function checkDate(ddVal, mmVal, yyVal) {
	if(ddVal <= 0 || ddVal > 31 || mmVal <= 0 || mmVal > 12 || yyVal <= 0 || yyVal.length != 4) {
		alert("Please select a valid date of birth");
		return false;
	}
	monthArray = new Array("January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	if(mmVal == 2) {
		if(yyVal % 4 == 0) {
			if(ddVal > 29) {
				alert("February " + yyVal + " has only 29 days");
				return false;
			}
		}
		else {
			if(ddVal > 28) {
				alert("February " + yyVal + " has only 28 days");
				return false;
			}
		}
	}
	else {
		if(mmVal == 4 || mmVal == 6 || mmVal == 9 || mmVal == 11) {
			if(ddVal > 30) {
				alert(monthArray[mmVal-1] + " has only 30 days");
				return false;
			}
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
	splitVal = emailString.split('@');
	if(splitVal.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		alert("Please enter a valid email address");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

//**********************************************************************************
//       Check wether only the characters specified are there in the string
// Accepts the string and the character to be checked (it is a string)
//                                        A     -> for all alphabets (no case specification)
//                                        N     -> for all numbers
//                                        a - z     -> for all the lower case chars
//                                        other just specify it
//**********************************************************************************

function checkAllowedChars(strToCheck, allowedChars)
{
     var acLen     = allowedChars.length;
     var stcLen     = strToCheck.length;
     strToCheck     = strToCheck.toLowerCase();
     var i;
     var j;
     var rightCount = 0;
     for(i = 0; i < acLen; i++)
     {
          switch(allowedChars.charAt(i))
          {
          case 'A':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= 'a' && strToCheck.charAt(j) <= 'z';
               }
               break;
          case 'N':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= '0' && strToCheck.charAt(j) <= '9';
               }
               break;
		   case '_':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) == '_';
               }
               break;	   
          default:
               for(j = -1; -1 != (j = strToCheck.indexOf(allowedChars.charAt(i), j + 1)); rightCount++);
               break;
          }
     }
     if(rightCount == stcLen)
     {
          return true;
     }
     return false;
}

//**********************************************************************************
//       Check wether the characters specified are not there in the string
// Accepts the string and the character to be checked (it is a string)
//                                        A     -> for all alphabets (no case specification)
//                                        N     -> for all numbers
//                                        a - z     -> for all the lower case chars
//                                        other just specify it
//**********************************************************************************
function checkNotAllowedChars(strToCheck, unAllowedChars)
{
     var acLen     = unAllowedChars.length;
     var stcLen     = strToCheck.length;
     strToCheck     = strToCheck.toLowerCase();
     var i;
     var j;
     var rightCount = 0;
     for(i = 0; i < acLen; i++)
     {
          switch(unAllowedChars.charAt(i))
          {
          case 'A':
               for(j = 0; j< stcLen; j++)
               {
                    if(strToCheck.charAt(j) >= 'a' && strToCheck.charAt(j) <= 'z')
                    {
                         return false;
                    }
               }
               break;

          case 'N':
               for(j = 0; j< stcLen; j++)
               {
                    if(strToCheck.charAt(j) >= '0' && strToCheck.charAt(j) <= '9')
                    {
                         return false;
                    }
               }
               break;

          default:
               if(strToCheck.indexOf(unAllowedChars.charAt(i)) != -1)
               {
                    return false;
               }
               break;
          }
     }
     return true;
}
function openWindow(url,window_name,winWidth,winHeight,fscroll) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	

	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;
	if(fscroll == '') {fscroll = 0}
	window.open(url,window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable=0");
}
function isEmpty(formname, fieldname) {
        itemvalue = eval('document.' + formname + '.' + fieldname + '.value');
        if(itemvalue.length <= 0) {
                return true;
        }
        icount = 0;
        for(i=0;i<itemvalue.length;++i) {
                if(itemvalue.charAt(i) != ' ') {
                        ++icount;
                }
        }
        if(icount > 0) {
                return false;
        }
        else {
                return true;
        }
}