// ZSite CMS
// JavaScript FTP URL validator
// variable name must be like "ftp://username:password@somedomain.com/some/path"
// (c) Zagorodnikov Anton 2007

function zsite_validate_ftpurl(text)
{
    if(text.substr(0,6) != "ftp://")
    {
	zsite_validation_error = ZLOCALE("this field must begin with ftp://");
	return false;
    }

    //break path into components containing username, password, server and path
    var charpos = text.search("[:]");
    if(charpos < 0)
    {
	zsite_validation_error = ZLOCALE("username and password must be supplied after ftp://");
	return false;
    }
    
    var username = text.substr(6,charpos);
    text = text.substr(charpos+1,text.length);
    
    charpos = text.search("[@]");
    if(charpos < 0)
    {
	zsite_validation_error = ZLOCALE("username and password must be supplied");
	return false;
    }
    
    var password = text.substr(0,charpos);
    text = text.substr(charpos+1,text.length);
    
    charpos = text.search("[/]");
    if(charpos < 0)
    {
	zsite_validation_error = ZLOCALE("server path must be at least /");
	return false;
    }
    
    var server = text.substr(0,charpos);
    var serverpath = text.substr(charpos, text.length);    
    
    //now check theese components

    //username must be valid variable
    if((!zsite_validate_variablename(username))||(!zsite_validate_nonempty(username)))
    {
	zsite_validation_error = ZLOCALE("username must be valid variable");
	return false;	
    }

    //password can be anything
    
    //server must be domain name or IP adress
    if(((!zsite_validate_domainname(server))&&(!zsite_validate_ipadress(server)))||(!zsite_validate_nonempty(server)))
    {
	zsite_validation_error = ZLOCALE("server must be either domain name or IP adress");
	return false;	
    }
    
    //server path must be valid path
    if((!zsite_validate_path(serverpath))&&(!zsite_validate_filename(serverpath)))
    {
	zsite_validation_error = ZLOCALE("server path must be either / or valid path");
	return false;	
    }

    return true;
}
