//****************************************************************************************
//Common JavaScript
//Written by Arun Kumar Pal(Esolz Incorporation)
//Date: 03.05.2007
//****************************************************************************************


//****************************************************************************************
//1. IsValidEmail
//2. trim
//3. validateNumeric
//4. validateInteger
//5. validateUSPhone
//6. validateUSZip
//7. validateUSDate
//8. removeCurrency
//9. addCurrency
//10. removeCommas
//11. addCommas
//12. IsUserName
//13. IsAlfaNumeric
//14. isblank
//15. IsString
//
//
//
//****************************************************************************************



//****************************************************************************************
//Function Name:IsValidEmail
//Parameter:email address :: DataType:String
//Return Value:
//True:Is a valid email
//False:Not a valid email
//****************************************************************************************

function BookMark()
{
   window.external.AddFavorite('http://www.esolzbackoffice.com/','Romanticsingles');
}

function IsValidEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{		 
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{		   
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{		    
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{		   
		return false
	}
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{		
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{		
		return false
	}	
	if (str.indexOf(" ")!=-1)
	{		
		return false
	}
	 return true					
}
//****************************************************************************************
//Function Name:trim
//Parameter:email address :: DataType:String
//Return Value:
//True:Trimmed Value
//****************************************************************************************
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");	
}
// Removes ending whitespaces
function isblank( value ) 
{	
	if(trim(value).length==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//*****************************************************************
//DESCRIPTION: Validates that a string contains only valid numbers.
//PARAMETERS:  strValue - String to be tested for validity
//RETURNS: True if valid, otherwise false.
//******************************************************************
function  validateNumeric( strValue ) 
{
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  //check for numeric characters
  return objRegExp.test(strValue);
}
//************************************************
//DESCRIPTION: Validates that a string contains only valid integer number.
//PARAMETERS:strValue - String to be tested for validity
//RETURNS:True if valid, otherwise false.
//**************************************************
function validateInteger( strValue ) 
{
  var objRegExp  = /(^-?\d\d*$)/;
  //check for integer characters
  return objRegExp.test(strValue);
}
//************************************************
//DESCRIPTION: Validates that a string contains valid US phone pattern.  Ex. (999) 999-9999 or (999)999-9999
//PARAMETERS: strValue - String to be tested for validity
//RETURNS:  True if valid, otherwise false.
//*************************************************
function validateUSPhone( strValue ) 
{
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}
//************************************************
//DESCRIPTION: Validates that a string a United States zip code in 5 digit format or zip+4  format. 99999 or 99999-9999
//PARAMETERS:strValue - String to be tested for validity
//RETURNS:True if valid, otherwise false.
//*************************************************
function validateUSZip( strValue ) {

var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  //check for valid US Zipcode
  return objRegExp.test(strValue);
}
//************************************************
//DESCRIPTION: Validates that a string contains only valid dates with 2 digit month, 2 digit day,
//4 digit year. Date separator can be ., -, or /.Uses combination of regular expressions and
//string parsing to validate date. Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
//PARAMETERS:strValue - String to be tested for validity
//RETURNS:True if valid, otherwise false.
//REMARKS:Avoids some of the limitations of the Date.parse() method such as the date separator character.
//*************************************************
function validateUSDate( strValue ) 
{
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/ 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}
//************************************************
//DESCRIPTION: Removes currency formatting from source string.
//PARAMETERS: strValue - Source string from which currency formatting will be removed;
//RETURNS: Source string with commas removed.
//*************************************************
function removeCurrency( strValue ) 
{
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}
//************************************************
//DESCRIPTION:Checking UserName
//PARAMETERS: strValue - 
//*************************************************
function IsUserName(strValue) 
{ 
	var i;
	var isvalid;
	var a;
	a=0;
	isvalid=true;
	strValue=trim(strValue);
	for(i=0;i<=strValue.length-1;i++)
	{
		a=strValue.charCodeAt(i);		
		if(!((a>=97 && a<=122)||(a>=65 && a<=90)||a==95||a==39||a==34))
		{
			isvalid=false;	
			break;		
		}				
	}	
	return isvalid;
}
//************************************************
//DESCRIPTION:Checking string or not
//PARAMETERS: strValue - 
//*************************************************
function IsString(strValue) 
{ 
	var i;
	var isvalid;
	var a;
	a=0;
	isvalid=true;
	strValue=trim(strValue);
	for(i=0;i<=strValue.length-1;i++)
	{
		a=strValue.charCodeAt(i);
		if(!((a>=97 && a<=122)||(a>=65 && a<=90)))
		{
			isvalid=false;			
		}				
	}	
	return isvalid;
}
//************************************************
//DESCRIPTION:Checking for alphanumeric
//PARAMETERS: strValue - 
//*************************************************
function IsAlfaNumeric(strValue) 
{ 
	var i;
	var isvalid;
	var a;
	a=0;
	isvalid=true;
	strValue=trim(strValue);
	for(i=0;i<=strValue.length-1;i++)
	{
		a=strValue.charCodeAt(i);
		if(!((a>=97 && a<=122)||(a>=65 && a<=90)||(a>=48 && a<=57)))
		{
			isvalid=false;			
		}				
	}	
	return isvalid;
}
//************************************************
//DESCRIPTION: Formats a number as currency.
//PARAMETERS: strValue - Source string to be formatted
//REMARKS: Assumes number passed is a valid numeric value in the rounded to 2 decimal  places.  If not, returns original value.
//*************************************************
function addCurrency( strValue ) 
{
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;
    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else
      return strValue;
}
//************************************************
//DESCRIPTION: Removes commas from source string.
//PARAMETERS:  strValue - Source string from which commas will be removed;
//RETURNS: Source string with commas removed.
//*************************************************
function removeCommas( strValue ) 
{
  var objRegExp = /,/g; //search for commas globally
  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}
//************************************************
//DESCRIPTION: Inserts commas into numeric string.
//PARAMETERS:  strValue - source string containing commas.
//RETURNS: String modified with comma grouping if  source was all numeric, otherwise source is  returned.
//REMARKS: Used with integers or numbers with  2 or less decimal places.
//*************************************************
function addCommas( strValue ) 
{
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match,
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}
//************************************************
//DESCRIPTION: Open window.
//PARAMETERS:  
//url	   - source string containing url.
//name	   - source string containing name of the window.
//w		   - source string containing width.
//h		   - source string containing height.
//RETURNS: window
//*************************************************
function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}
//************************************************
//DESCRIPTION: closeModal.
//*************************************************
function closeModal() {
  if (window.dialogArguments && dialogArguments.location) 
  {  
    window.close();
  }
  
}
//************************************************
//DESCRIPTION: URL checking.
//Return: object
//*************************************************
function isUrl(s)
{
var regexp =/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&amp;%@!\-\/]))?/;
return regexp.test(s);
}

