// Function to check if the given string is a null string
// or contains any numbers or other characters
// Returns True if only alphabets else returns False
function isAlpha(field){
  var num = parseInt(field.length);
  if (field  == ""){
	return false
  }
  else{
	for (i = 0; i < num; i++){
	  var str = field.substring(i,i+2);
	  if (!((str >= "A" && str <= "Z") || (str >= "a" && str <= "z") || (str==" "))){
		 return false
	  }
	}
  }
  return true
}
// Function to check if the given string is a null string
// or contains any alphabets or other characters
// Returns True if only numbers else returns False
function isNumber(field){
  var num = parseInt(field.length);
  if (field  == ""){
	return false
  }
  else{
	for (i = 0; i < num; i++){
	  var str = field.substring(i,i+2);
	  if (!(str >= "0" && str <= "9")){
		return false
	  }
	}
  }
  return true
}
// Function to Trim the given string of Leading and Trailing spaces
// Returns Trimmed string
function Trim(strInput){
  var i;
  i=0;
  while(i<strInput.length-1){
	if(strInput.charAt(i)==' ')
	  i++;			
	else
	  break;
  }
  strInput= strInput.substr(i);
  i=strInput.length-1;
  while(i>0){
    if(strInput.charAt(i)==' ')
	  i--;			
    else
	  break;
	}
  strInput = strInput.substr(0,i+1);
  return strInput
}

// Function to check if the given string is a null string
// Returns True if Empty else returns False
function isNull(field){
  if(Trim(field) == ""){
	return true
  }
  return false
}

// Function to check if the given string is a valid EMail address or not
// Returns True if valid else returns False
function validEmail(field){
  if(Trim(field) == ''){
	return false;
  }
// First char is @ or . Otherwise No @ or .
  if(field.indexOf("@") <= 0 || field.indexOf(".")<=0){
	return false;
  }
  else{
	var AtLocation;
	AtLocation = field.indexOf("@");
	if(field.indexOf("@",AtLocation+1)>=0){
	  return false;
	}
  }
// No @ or . as the last char
  if(field.indexOf("@") == field.length - 1){
	return false;
  }	           
  if(field.indexOf(".") == field.length - 1){
	return false;
  }	           
// No @ or . as the first char
  if(field.indexOf("@") == 0 || field.indexOf(".") == 0){
	return false;
  }	           
// No characters between @ and .
  if(field.indexOf("@") + 1 == field.lastIndexOf(".")){
	return false;
  }
// Position of @ is before position of .
  if(field.indexOf("@") > field.lastIndexOf(".")){
	return false;
  }
  return true;
}


// Function to check if the given date is a valid date or not
// Returns True if valid else returns False
function ValidDate(field)
{ 
    var dat = Trim(field);
    if  (dat == '') 
	{
	     return false;
	}  
//Date Validation
	
     var sl = dat.indexOf("/");
  
     if (sl != -1){
        var d = dat.substring(0,sl);
        d = Trim(d);
        if(d == "08") d = "8";if(d == "09") d = "9";
        if (isNumber(d) == false){
          return false;
        }
        d = parseInt(d);
         if(d > 31 || d == 0){
           return false;
        }
        
     }
     else  return false;    
// month validation	
	var sl2 = dat.indexOf("/",sl+1);   
    if  (sl2 != -1){ 
         var m = dat.substring(sl+1,sl2); 
         m = Trim(m);
         if (m == "08") m = "8";if (m == "09") m = "9";
         if  (isNumber(m) == false){ 
         return false;
         }
         m = parseInt(m);
         if(m > 12 ||m == 0){
           return false
         }
         if(validDate(d,m) == false){
		   return false;
        }
  }else  return false;
 
// validation for Year
     var y = dat.substring(sl2+1);
     y = Trim(y);   
	 if  (isNumber(y) == false){
       return false;
     }
     y = parseInt(y);
     if ( m == 2 && y % 4 != 0 && d > 28 ){
       return false;
     }
     if( y < 2000 || y > 9999){ 
       return false;
     }
    else return true;  
}
function validDate(dd,mm)
{
if ( mm == 1 && dd <=31 ) return true; if ( mm == 2 && dd <=29 ) return true;
if ( mm == 3 && dd <=31 ) return true; if ( mm == 4 && dd <=30 ) return true;
if ( mm == 5 && dd <=31 ) return true; if ( mm == 6 && dd <=30 ) return true;
if ( mm == 7 && dd <=31 ) return true; if ( mm == 8 && dd <=31 ) return true;
if ( mm == 9 && dd <=30 ) return true; if ( mm == 10 && dd <=31 ) return true;
if ( mm == 11 && dd <=30 ) return true; if ( mm == 12 && dd <=31 ) return true;
return false;
}  

//Email Validation//
	function validEmail(email)
	{
		if(email==''){return true;}
			invalidChars = " /:,#'`$~!%^&*()+\"\;<>?\\|        "
			if (email == "")
			{	
				return false
			}
			for (i = 0; i < invalidChars.length; i++)
			{	
				badChar = invalidChars.charAt(i)
				if (email.indexOf(badChar,0) > -1)
				{	
					return false
				}
			}
			atPos = email.indexOf("@",1)
			
			if (atPos == -1)
			{	
				return false 
			}
			
			if (email.indexOf("@",atPos+1) != -1)
			{	
				return false
			}
			
			periodPos = email.indexOf(".",atPos)
			if (periodPos == -1)
			{	
				return false
			}
		 
			if (periodPos+3 > email.length)
			{	
				return false
			}

	}
	
	//website validation//
	function validwebsite(site)
	{
		if(site=='')
		{	return true;
			}
			
		invalidChars = " /:,#'`@$~!%^&*()+\"\;<>?\\|        "
			
		if (site == ""){return false}
			for (i = 0; i < invalidChars.length; i++)
			{
				badChar = invalidChars.charAt(i)
				if (site.indexOf(badChar,0) > -1){return false}
				}
			
		periodPos = site.indexOf(".",atPos)
			
		if (periodPos == -1)
		{	return false
			}
			
		if (periodPos+3 > site.length)
		{	return false
			}

	}

	
//field length check//
function lengthcheck(a)
{
	var temp = a;
	if ((a.length) < 6)
	{
		return false;
	}
	else
	{	
		return true;
	}
}


//////Validation for photo Extension//////

function validphoto(photo)
{
	if(photo==''){return true;}
	V=photo
	dPos=V.lastIndexOf(".")
	Ext=V.substring(dPos+1,V.length)
	Ext=Ext.toLowerCase()
		 	if((Ext=="doc")||(Ext=="pdf")||(Ext=="gif")||(Ext=="bmp")||(Ext=="jpg")||(Ext=="jpe")||(Ext=="jpeg")||(Ext=="tiff")||(Ext=="tif")||(Ext=="GIF")||(Ext=="BMP")||(Ext=="JPG")||(Ext=="JPE")||(Ext=="JPEG")||(Ext=="TIFF")||(Ext=="TIF"))
	 {
		return true
	 }
	 else
	 {
		return false
	 }
 }	

//////Validation for file Extension//////

function validFile(file)
{
	if(file==''){return false;}
	V=file
	dPos=V.lastIndexOf(".")
	Ext=V.substring(dPos+1,V.length)
	Ext=Ext.toLowerCase()
		 	if((Ext=="xls")||(Ext=="txt"))
	 {
		return true
	 }
	 else
	 {
		return false
	 }
 }	


//////Validation for Sound Track Extension//////

function validSoundTrack(SOund)
{
	if(SOund==''){return true;}
	V=SOund
	dPos=V.lastIndexOf(".")
	Ext=V.substring(dPos+1,V.length)
	Ext=Ext.toLowerCase()
	if((Ext=="mp3")||(Ext=="mid")||(Ext=="wav")||(Ext=="avi")||(Ext=="MID")||(Ext=="MP3")||(Ext=="WAV")||(Ext=="AVI"))
	 {
		return true
	 }
	 else
	 {
		return false
	 }
 }
 
 //////Validation for Video Track Extension//////

function validVideoTrack(Video)
{
	if(Video==''){return true;}
	V=Video
	dPos=V.lastIndexOf(".")
	Ext=V.substring(dPos+1,V.length)
	Ext=Ext.toLowerCase()
	if((Ext=="mpg")||(Ext=="mpeg")||(Ext=="wmv")||(Ext=="dat")||(Ext=="MPG")||(Ext=="MPEG")||(Ext=="WMV")||(Ext=="DAT"))
	 {
		return true
	 }
	 else
	 {
		return false
	 }
 }
 
  
//// Validation for Quotes & etc.////

function RTESafe(strText) 
{
	var num = parseInt(strText.length);
	tmpString = strText
	
  	for (i = 0; i < num; i++){
	  tmpString = tmpString.replace('#', "");
	  tmpString = tmpString.replace('`', "'");
	
	//convert all types of single quotes
	tmpString = tmpString.replace('‘', "'");
	tmpString = tmpString.replace('’', "'");
	tmpString = tmpString.replace('`', "'");

	//convert all types of double quotes
	tmpString = tmpString.replace('“', '"');
	tmpString = tmpString.replace('”', '"');
	}
	//tmpString = strText
	//tmpString = tmpString.replace('"', "'");

	return tmpString;
}

