//*********************************************************************************
// *     Filename: JSValidateFunctions.php
// *	 Proj: imomarket.ro
// *	 Ver:  1.1
// *     Date: 10.10.2006
// *	 Author: www.klsolution.com
// *     Copyirght (c) 2006 KL SOFTWARE
// *********************************************************************************/
// Functions: 
// 1. IsNumeric(strInput)
// 2. IsAlpha(strInput)
// 3. IsAlphaNumeric(strInput)
// 4. IsDOB(mm,dd,yyyy)
// 5. IsChecked(obj)
// 6. IsTime(strInput)
// 7. IsEmail(InString)
// 8. GetRadioValue(obj)
// 9. ObjectExists(strObjName)
//10. IsCurrency(strInput)
//11. IsEmailList(strInput, strSeparator)
//12. LTrim(strInput)
//13. RTrim(strInput)
//14. Trim(strInput)
//15. IsPhone(strInput)
//16. IsDate( strInput ) 'mm/dd/yyyy'
//17. Privacy( file) 

function Privacy(p_file){
	
	 var winl = (screen.width - 500) / 2;
  	 var wint = (screen.height - 510) / 2;

	 var wndSend=window.open(p_file, "privacy", "toolbar=no,location=no,resizable=no,status=no,scrollbars=yes,width=500,height=510,top=" + wint + ",left=" + winl );
	 if (wndSend==null)
		alert("The security setings of your browser not allow to open the popup window!");
	 else
	 	wndSend.focus();
}

function IsNumeric(strInput)
{
	var i
	var c
	
	if (strInput == "")
	{
		return false;
	}
	
	for (i = 0; i < strInput.length; i++)
	{
		c = strInput.charAt(i);
		if (c < "0" || c > "9")
		{
			
			if (c !=".")
			return false;
		}
	}
	return true;
}

function IsAlpha(strInput)
{

	var i
	var c
	if (strInput == "")
                 return false;
	for (i = 0; i < strInput.length; i++)
	{
		c = strInput.charAt(i);
 		if ((c < "a"  || c > "z") && (c < "A" || c > "Z")&&(c!=" ")&&(c!="-")&&(c!="."))
		  {
                   return false;
                  }
	}
       
    return true;

}

function IsAlphaNumeric(strInput)
{
	var i
	var c
	
	if (strInput == "")
	{
		return false;
	}
	
	for (i = 0; i < strInput.length; i++)
	{
		c = strInput.charAt(i);
		if ((c < "a" || c>"z") && (c <"A" || c>"Z") && (c < "0" || c > "9"))

		{
			return false;
		}
	}
	return true;
}

function IsDOB(mm,dd,yyyy)
{
	if(mm.value=="-1")
		return false;
		
	if(dd.value=="-1")
		return false;
		
	if(yyyy=="-1")
		return false;
return true;
}


//verify if one radio button is checked
function IsChecked(obj) 
{ 
 var i
 for(i=0; i<obj.length; i++)
	if(obj[i].checked) return true;
  return false;
}  


function IsTime(strInput){	
		var delim=strInput.indexOf(":");
		if(strInput.length>6 && strInput.length>3)
		{
			if((delim!=-1) && IsNumeric(strInput.substring(0,delim)) && IsNumeric(strInput.substring(delim+1,strInput.length)))
				{
					var hh = parseInt(strInput.substring(0,delim),10);
					var mm = parseInt(strInput.substring(delim+1,strInput.length),10);
					if(hh<24 && mm<59)
						return true
					else{
						return false;
						}
				}else {
						return false;
						}
		}else {	
				return false;
				}
}

function isDecimal( InString )
{
  var left, right;
  
  if ( InString.length == 0 ) return (false);
  for (Count = 0; Count < InString.length; Count++)
  {
    TempChar = InString.substring (Count, Count+1);
	  if ("1234567890.".indexOf (TempChar, 0) == -1) return (false);
  }
    return (true);
}

function isEmail( InString )
{
  var left, right;
  
  if ( InString.length == 0 ) return (false);
  for (Count = 0; Count < InString.length; Count++)
  {
    TempChar = InString.substring (Count, Count+1);
	  if ("-1234567890abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ.@_".indexOf (TempChar, 0) == -1) return (false);
  }
  if ( InString.indexOf('@') < 1 ) return (false); //if it begins with or doesn't have a '@'
  if ( InString.lastIndexOf('@') != InString.indexOf('@') ) return (false); //if it has more than one '@'
  
  left  = InString.substring( 0 , InString.indexOf('@') );
  right = InString.substring( InString.indexOf('@') + 1, InString.length );
  
  if ( (!isDotExpression( left, 0 )) || (!isDotExpression( right , 1 )) ) return (false);
  return (true);
}

function isDotExpression( InString , NeedsDot )
{
  var dots, index, tmpNeedDot;
  dots = 0;
  for (index = 0; index < InString.length; index++)
  {
    if ( InString.substring( index , index + 1) == "." )
    {
      if ( ( index == 0 ) || ( index == InString.length - 1 ) ) return (false);
      dots++;
      if ( dots > 1 ) tmpNeedDot = 1;
      else tmpNeedDot = 0;
      if ( !isDotExpression( InString.substring( 0 , index ) , tmpNeedDot ) ) return (false);
    }
  }
  if ( ( NeedsDot == 1 ) && ( dots < 1 ) ) return (false);
  if ( InString.length < dots * 2 + 1 ) return (false);
  return (true);
}

//verify if one radio button is checked and returns it's value
function GetRadioValue(obj) 
{ 
 var i;
 if (obj.length == null){
   return obj.value;
 }else{
   for(i=0; i<obj.length; i++) if(obj[i].checked) return obj[i].value;
 }
  return "";
}

function ObjectExists( strObjName, strFormName )
{
  var oForm = document.forms[strFormName];
  var i=0;
  var bExists = false;
  while( i<oForm.elements.length && bExists==false)
  {
    if( oForm.elements[i].name == strObjName ){ bExists = true;}
    i++;
  }
  return bExists;
}

function IsCurrency(strInput)
{
	var i
	var c
	
	if (strInput == "")
	{
		return false;
	}
	
	for (i = 0; i < strInput.length; i++)
	{
		c = strInput.charAt(i);
		if (( c < "0" || c > "9") && (c != ".") && (c != "-") && (c != "+"))
		{
			return false;
		}
	}
	return true;
}

function isEmailList( strInput, strSeparator )
{
  var index, arrayEmails;
  arrayEmails = strInput.split( strSeparator );
  
  for ( index = 0; index < arrayEmails.length; index++ ){
    if ( !isEmail( Trim( arrayEmails[ index ] ) ) ) return (false);
  }
  return (true);
}

function LTrim( strInput ){
  while ( ( strInput.charAt( 0 ) == " " ) && ( strInput.length >= 1 ) ){
    strInput = strInput.substr( 1 );
  }
  return strInput;
}

function RTrim( strInput ){
  while ( ( strInput.charAt( strInput.length - 1 ) == " " ) && ( strInput.length >= 1 ) ){
    strInput = strInput.substr(0, strInput.length - 1 );
  }
  return strInput;
}

function Trim( strInput ){
  return LTrim( RTrim( strInput ) );
}

function IsPhone( strInput ){
  var i;
  var c;
  
  if ( strInput == "" ){
	return false;
  }
  if ( strInput.length < 12){
    return false;
  }
	
  for ( i = 0; i < strInput.length; i++){
    c = strInput.charAt(i);
	if ( ( i != 3 && i != 7 ) && ( c < "0" || c > "9" )){
	  return false;
	}
	if ( ( i == 3 || i == 7 ) && c != "-" ){
	  return false;
	}
  }
  return true;
}

function IsDate(strInput){	
  var delim1, delim2;
  var mm, dd, yyyy;  
  if ( strInput == "" ){
    return false;
  }
  if ( strInput.length > 10 ){
    return false;
  }
  
  delim1 = strInput.indexOf( "/", 0 );
  if ( delim1 == -1 ) return false;
  mm = strInput.substring( 0, delim1 );
  if ( IsNumeric( mm ) ){
    mm = parseInt( mm );
    if ( mm < 0 || mm > 12 ) return false;
  } else return false;
  
  if ( mm.toString() != strInput.substring( 0, delim1 ) ){ return false; }
  
  delim2 = strInput.indexOf( "/", delim1 + 1 );
  if ( delim2 == -1 ) return false;
  dd = strInput.substring( delim1 + 1, delim2 );
  if ( IsNumeric( dd ) ){
    dd = parseInt( dd );
    if ( dd < 0 || dd > 31 ) return false;
  } else return false;
  if ( dd.toString() != strInput.substring( delim1 + 1, delim2 ) ){ return false; }

  yyyy = strInput.substring( delim2 + 1 );
  if ( IsNumeric( yyyy ) ){
    yyyy = parseInt( yyyy );
    if ( yyyy < 0 || yyyy > 2100 ) return false;
  } else return false;
  if ( yyyy.toString() != strInput.substring( delim2 + 1 ) ){ return false; }
  
  //alert( "|" + mm + "|" + dd + "|" + yyyy );
  
  return true;
}