//trim functions
function trim(value)
{
        return LTrim(RTrim(value));
}

// Removes leading whitespaces
function LTrim(value)
{
        var re = /\s*((\S+\s*)*)/;
        return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value)
{
        var re = /((\s*\S+)*)\s*/;
        return value.replace(re, "$1");
}
// validate website URL
function validateWebsiteURL(url)
{
	var tomatch=/(http:\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/;
	if(url!='')
	{
	    if (tomatch.test(url))
		{
			return true;
        }
		else
		{
			return false
		}
    }
}
// check image exyension
function validateImageExtension(imgStr)
{
	var imagePath = imgStr;
	var pathLength = imagePath.length;
	var lastDot = imagePath.lastIndexOf(".");
	var fileType = imagePath.substring(lastDot,pathLength);
	if((fileType == ".gif") || (fileType == ".jpg") || (fileType == ".jpeg") ||(fileType == ".png") || (fileType == ".GIF") || (fileType == ".JPG") || (fileType == ".JPEG") ||(fileType == ".PNG") || (fileType == ".bmp") ||(fileType == ".BMP"))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//check video file extension
function validateVideoExtension(videoStr)
{
	var videoPath = videoStr;
	var pathLength = videoPath.length;
	var lastDot = videoPath.lastIndexOf(".");
	var fileType = videoPath.substring(lastDot,pathLength);
	if((fileType == ".swf") || (fileType == ".flv") || (fileType == ".FLV") || (fileType == ".wma") ||(fileType == ".SWF") || (fileType == ".WMV") ||(fileType == ".AVI" || (fileType == ".WMA")))
	{
		return true;
	}
	else
	{
		return false;
	}
}
// check email address
function validateEmailAddress(emailStr)
{
	var tomatch=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,50})+$/;
	if(emailStr!='')
	{
	    if (tomatch.test(emailStr))
		{
			return true;
        }
		else
		{
			return false
		}
    }
}

// check only charater values
function validateCharacters(charStr)
{
	//var tomatch=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,50})+$/;
	var tomatch=/^([a-zA-Z_\.])+$/;
	if(charStr!='')
	{
	    if (tomatch.test(charStr))
		{
			return true;
        }
		else
		{
			return false
		}
    }
}
//adjust popup window
function adjust()
{
    width  =  0;
    height = 0;
    if( navigator.appName.indexOf("Microsoft") != -1 ){ // Microsoft
        width  = document.body.scrollWidth+50;
        height = document.body.scrollHeight+80;
    }else{
        width  = document.width+50;
        height = document.height+60;
    }

    // Put condition here
    width  = ( width  > screen.availWidth  ? screen.availWidth  : width );
    height = ( height > screen.availHeight ? screen.availHeight : height );

    x = (screen.availWidth/2)  - (width/2);
    y = (screen.availHeight/2) - (height/2);

    window.moveTo( x, y );
    window.resizeTo( width, height );
}

function popUpWindow(url, width, height, windName)
{
//alert(url+"=="+width+"=="+height+"=="+windName);
    wind = window.open(url, (windName ? windName : "_sameWindow"), "width=" +width+ ",height=" +height+ ",scrollbars=yes");
    x = (screen.availWidth/2)  - (width/2);
    y = (screen.availHeight/2) - (height/2);
    wind.moveTo(x, y);
    wind.resizeTo( width, (height+23) );
    wind.focus();
}
function popUpWindow_new(url, width, height, windName)
{
    windName="Commssion"
    wind = window.open(url, (windName ? windName : "_sameWindow"), "width=" +width+ ",height=" +height+ ",scrollbars=yes");
    x = (screen.availWidth/2)  - (width/2);
    y = (screen.availHeight/2) - (height/2);
    wind.moveTo(x, y);
    wind.resizeTo( width, (height+23) );
    wind.focus();
}
function moveOptions( objSource, objDestination )
{
    if( objSource.selectedIndex != -1)
    {
        for( mo_Ctr=(objSource.options.length-1); mo_Ctr>=0; mo_Ctr-- )
        {
            if( objSource.options[mo_Ctr].selected )
            {
                t_Opt  = objSource.options[mo_Ctr];
                option = new Option( t_Opt.text, t_Opt.value);
               
                objDestination.options.length++;
                objDestination.options[objDestination.options.length-1] = option;
                objSource.options[mo_Ctr] = null;
            }
        }
    }
    else
    {
        alert( "Please select an option.");
        objSource.focus();
    }
}
//function for date validation
/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    validateDate
    Purpose        :    This function checks for valid date.
    Usage        :    validateDate("22/09/2001") checks within range of 1900-2099
    Arguments    :    dateVal    -    String. A String representing a date in whichever locale according to need.
    Return        :    true/false.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

function validateDate(dateString)
{
	//alert(dateString);
	/*if(dateString == "__/__/____")
	{
		return true;
	}*/
	var delimeter = "/";
    var dayStr;
    var monthStr;
    var yearStr;
    var    strDateArray;

    if (dateString.indexOf(delimeter) != -1)
    {
        strDateArray = dateString.split(delimeter);
        if (strDateArray.length != 3)
            return false;
        else
        {
            dayStr      = strDateArray[0];
            monthStr    = strDateArray[1];
            yearStr     = strDateArray[2];
        }
    }
    else
        return false;

    if(dayStr.length == 0 || monthStr.length == 0 || yearStr.length != 4 )
        return false;
    if(isNaN(dayStr))
        return false;
    if(isNaN(monthStr))
        return false;
    if(isNaN(yearStr))
        return false;
    // SR#1 Start
    // Convert strings to ints .
    var day   = parseInt(dayStr,10);
    var month = parseInt(monthStr,10);
    var year  = parseInt(yearStr,10);
    // SR#1 End
    return getDateStatus(month,day,year);

}
/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    getDateStatus
    Purpose        :    This function returns the no of days in particular month of particular year.
    Usage        :    getDateStatus(2,2,2002)
    Arguments    :    month,year
    Return        :    true/false
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
    function getDateStatus(month,day,year)
    {
    	if(year<0 )
            return false;
        else
        {
            switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                {
                    if (day <1 || day > 31)
                        return false;
                    else
                        return true;
                }
                case 4:
                case 6:
                case 9:
                case 11:
                {
                    if (day <1 || day > 30)
                        return false;
                    else
                        return true;
                }
                case 2:
                {
                    if(((year%4==0)&&(year%100!=0))||(year%400==0))
                    {
                        if(day<0 || day>29)
                            return false;
                        else
                            return true;
                    }
                    else
                    {
                        if(day < 0 || day>28)
                            return false;
                        else
                            return true;
                    }
                }
                default :
                    return false;
            }
        }
    }
function IsNumeric(sText)
{
   //alert('sssssssssss');
   //alert(sText);
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    checkNumeric
    Purpose     :    Use the onBlur() method to validate numeric fields with this checkNumeric() javascript function.
					 Optionally pass in a period, comma, or hypen to indicate allowed values. Set the maximum and minimum value allowed.
					 If your allowed values always include period, comma, or hyphen, or never include those,
					 you may easily edit the function to remove the parameter values from the onBlur() and function.
					 You may also use a variation of this routine to check for any combination of valid values.
					 You don't have to use "0123456789" - you may instead use something else as your check string.
    Usage       :    checkNumeric(this,'0','9000000000000','','.','')
    Arguments   :    objName->fieldName,minval,maxval,comma,period,hyphen
    Return      :    true/false
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";

	for (i = 0;  i < checkStr.value.length;  i++)
	{
		ch = checkStr.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
		allValid = false;
		break;
		}
		if (ch != ",")
		allNum += ch;
	}
	if (!allValid)
	{	
	alertsay = "Please enter only these values \""
	alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
	alert(alertsay);
	return (false);
	}

	// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
	{
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
}

function validateEmail(email)
{
    invalidChars = " /:,;'\"!#&*()"
    if(email.value== ""){                 //email cannot be empty
        return false;
    }

    for(i=0; i<invalidChars.length; i++)
        { //check for invalid characters
           badChar = invalidChars.charAt(i);
            if(email.value.indexOf(badChar,0) != -1)
            {alert("Invalid Email Address");
		        	email.focus();
               return false;
            } 
        }

    atPos = email.value.indexOf("@",1);         //there must be one "@" symbol
    if(atPos == -1)
     {
    	  alert("Invalid Email Address");
		    email.focus();
        return false;
     }
    if(email.value.indexOf("@",atPos+1) != -1)
     { //check to make sure only one "@" symbol
        alert("Invalid Email Address");
		    email.focus();
        return false;
     }

    periodPos = email.value.indexOf(".",atPos);
    if (periodPos == -1)
     { // make sure there is one "." after the "@"
        alert("Invalid Email Address");
		    email.focus();    
        return false;
    }

    if(periodPos+3 > email.value.length)
     { // must be at least 2 chars after the "."
       alert("Invalid Email Address");
		   email.focus();    
       return false;
     }
  //  return true;
}
function validateDoubles(formObj, arg_DataType)
{
    vDSResult = true;
    arg_DataType = arg_DataType.toLowerCase();
    var vMissingInfo= "";
    var vFocusObject="";

    for(vDSCounter=2; vDSCounter<arguments.length; vDSCounter++)
    {
        vDSObj = formObj[arguments[vDSCounter]];
        if(vDSObj)
        {
            for(fvDSCounter=0; fvDSCounter<vDSObj.value.length; fvDSCounter++)
            {
                fObjVal = vDSObj.value.charAt(fvDSCounter);
                if(!(fObjVal >= '0' && fObjVal <= '9'))
                { // If not in between 0-9
                    if(arg_DataType == "dec" && fObjVal != ".")
                    {
                        vDSResult = false;
                        vMissingInfo += "\n     -  "+convertVariable(vDSObj.name);
                        if(vFocusObject=="")
                            vFocusObject=vDSObj;
                        break;
                    }

                    if(arg_DataType == "int")
                    {
                        vDSResult = false;
                        vMissingInfo += "\n     -  "+convertVariable(vDSObj.name);
                        if(vFocusObject=="")
                            vFocusObject=vDSObj;
                        break;
                    }
                }
            }
        }
    }
    if (vMissingInfo != "")
    {
        vMissingInfo ="_________________________________\n" + "You failed to fill correct values:\n" +
        vMissingInfo +"\n_________________________________" + "\n Please re-enter and submit again!";
        alert(vMissingInfo);
        vFocusObject.focus();
        return false;
    }
    return true;
}

//  Code for checking valid user entries a-z,A-Z,0-9,.,
function validateText(text)
 { 
   invalidChars = "/\:,;'*!#&*()><{}[]^`~="
       for(i=0; i<text.value.length; i++)
        { 
        	//check for invalid characters 	
          for(j=0;j<invalidChars.length;j++)
          { 
              if(text.value.charAt(i)==invalidChars.charAt(j))
               { 
     	           alert("Special Characters Not Allowed"); 
     	           text.focus();
     	           return false;
     	           break;
               }
              else
               {
            	   continue;
               } 
          }  
       } 
     
} 

function validateNumeric(num)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789-";
	var strChar;
	var blnResult = true;
	if (num.value.length == 0) 
	return false;
	
	//  test num consists of valid characters listed above
	for (i = 0; i < num.value.length && blnResult == true; i++)
	  {
		 strChar = num.value.charAt(i);
		 //alert(strValidChars.indexOf(strChar));
		 if (strValidChars.indexOf(strChar) == -1)
		   {
			alert("Only Numeric Characters Are Allowed");
			num.focus();
			blnResult = false;
			break;
		   }
	  }
//   /return blnResult;
}




/* Comare Two dates
This function is used to validate Appointment Date, should be greater than Current date

Parameter gdate---> Greater date
		  sdate---> Small date	
*/

function fnc_dateMatch(gdate,sdate)
{
	var cdate			=	sdate.split("/");
	var CurTotalDate	=	parseInt( (cdate[2]*10000) + (cdate[1]*100) + (cdate[0]) );
	//alert("Cur Date="+CurTotalDate);		
	/*
	// get current date 
	var today	=	new Date();
	var curdate	=	today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getYear();
	var curdate=today.getDay();
	alert(curdate);
	*/
			
	var adate			=	gdate.split("/");
	var AppTotalDate	=	parseInt( (adate[2]*10000) + (adate[1]*100) + (adate[0]) );
	//alert("App Date="+AppTotalDate);
	
	if(AppTotalDate < CurTotalDate)
	{
		return false;
	}
	return true;
}

//function to check password length
function passwordStrength(strPwd)
{
	if(strPwd.length<5)
	{
		return false;
	}
	else
	{
		return true;
	}
}
