// ZSite CMS
// JavaScript server filename string validator
// /xxx/xxx/somefile paths are valid
// (c) Zagorodnikov Anton 2007

function zsite_validate_filename(text)
{
    if(text.length < 1)
    {
	zsite_validation_error = "empty_string";
	return false;
    }
    
    if((text.length == 1)&&(text[0] == "/"))
    {
	return true;
    }
    
    if(text[0] == "/")
    {
	text = text.substr(1,text.length);
    }
    else
    {
	zsite_validation_error = ZLOCALE("path must begin with /");
	return false;
    }

    //now get all path components
    var paths = Array();
    var counter = 0;
    var dotposition = text.search("[/]");


    while(dotposition > 0)
    {
	paths[counter] = text.substring(0,dotposition);
	text = text.substr(dotposition+1,text.length);
	dotposition = text.search("[/]");
	counter++;
    }

    var filename = text;
    if(filename.length < 1)
    {
	    zsite_validation_error = ZLOCALE("filename not specified");
	    return false;
    }
        
    //now check path components
    var c = 0;
    for(c = 0; c < counter; c++)
    {
	if(paths[c].length < 1)
	{
	    zsite_validation_error = ZLOCALE("path cannot contain //");
	    return false;
	}
	if(paths[c].search("[\\\\?\\\\*\\\\]") > 0)
	{
	    zsite_validation_error = ZLOCALE("path cannot contain *,? and backslash");
	    return false;
	}
    }
    
    if(filename.search("[\\\\?\\\\*\\\\]") > 0)
    {
	    zsite_validation_error = ZLOCALE("incorrect file name");
	    return false;
    }
    
    return true;
}
