// Global variables
var GFilenameMaxLength = 50
//*******************************************************************************************************

// Extracts the filename from a filepath
function getFilenameFromPath(filepath){
var temp = ""
var filename = ""

for (var i=filepath.length-1; i>=0; i--){
	if(filepath.charAt(i)!="\\"){
		temp+=filepath.charAt(i)
	}
	else{
		break 
	}
}
// Reverse string
for(var t=temp.length-1; t>=0; t--){
	filename += temp.charAt(t)
}

return filename
}

// Extracts the name for the file from the filename
function getNameFromFilename(filename){
var filebody = ""
var extindex = 0

for(var v=0; v<filename.length; v++){
	if(filename.charAt(v)!="."){
		filebody += filename.charAt(v)
	}
	else{
		extindex = v+1
		break
	}
}
return filebody
}

// Extracts the extension from a filename
function getExtensionFromFilename(filename){
var fileext = ""
var extindex = filename.indexOf(".")+1
for(var u=extindex; u<filename.length; u++){
	if(filename.charAt(u)!="."){
		fileext += filename.charAt(u)
	}
	else{
		break
	}
}
return fileext
}

// Check that a string has invalid characters
function hasInvalidCharacters(str){
var validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-."
var isInvalid = false
for (var i=0; i<str.length; i++){
	if (validchars.indexOf(str.charAt(i))<0){
		isInvalid = true
		break
	}
}
return isInvalid
}

// Check that a string has one '.'
function hasOneDot(str){
var dotcount = 0
for (var a=0; a<str.length; a++){
	if (str.charAt(a)=="."){
		dotcount++
	}
}
return dotcount==1? true:false
}

// Searchs through an array of filenames for a given filename
function fileExists(filesArray,filename){
var foundfile = false
for (var i=0; i<filesArray.length; i++){
	if (filename==filesArray[i]){
		//alert(filename+" "+filesArray[i])
		foundfile = true
		break
	}
}
return foundfile
}

// Trims white space from both sides of a string and returns the trimmed string
function trim(text){
	//l = text.length;
	//alert(text);
	var start=0
	var leftTrim=""
	var fullTrim=""
	var end=0
	var isAllSpaces
	
	// Check if entire string is spaces
	for(w=0;w<text.length;w++){
		if(text.charAt(w)==" "){
			isAllSpaces=true
		}
		else{
			isAllSpaces=false
			break
			}
	}
	
	// If string is all spaces return zero length string 
	if (isAllSpaces==true){
		var x=""
		return x
		}

	// If the string is not all spaces then perform trim of white space
	 
	// Perform a left trim
	// Find position first real char appears in string
	for(i=0;i<text.length;i++){
		if(text.charAt(i)!=" "){
			start=i
			break
		}
	}

	// Extract the string to the right of start
	for(i=start;i<text.length;i++){
		leftTrim+=text.charAt(i)
	}
	
	//perform right trim
	for(i=leftTrim.length-1;i>=0;i--){          // -1 is for end of string
		if(leftTrim.charAt(i)!=" "){
			end=i
			break
		}
	}

	// Extract the string to the left of start
	for(i=0;i<=end;i++){
		fullTrim+=leftTrim.charAt(i)
	}
	return fullTrim
}

//*******************************************************************************************************
// Checks if a string is alphabetic, returns true or false 
function isAlpha(text){
	var t=trim(text)
	var isLetter=true
	t=t.toLowerCase()

    var alpha="abcdefghijklmnopqrstuvwxyz "
	
	// compare each letter of string with letters of alphabet, set flag 
	for (i=0;i<t.length;i++){
		for(j=0;j<alpha.length;j++){
			alphaChar=alpha.charAt(j);
			//alert("!"+t.charAt(i)+"!"+alphaChar+"!")
			if (t.charAt(i)==alphaChar){
			    isLetter=true
				break;
			}
			else{
				isLetter=false
			}
		}
		if(isLetter==false)
		 	return false
	}
	return isLetter
}
//*******************************************************************************************************
// Checks if a string is alphanumeric, returns true or false 
function isAlphaNumeric(text){
	var t=trim(text)
	//if (t=="")
		//return false
		
	var isLetter=true
	t=t.toLowerCase()

    var alpha="abcdefghijklmnopqrstuvwxyz1234567890"
	
	// compare each letter of string with letters of alphabet, set flag 
	for (i=0;i<t.length;i++){
		for(j=0;j<alpha.length;j++){
			alphaChar=alpha.charAt(j);
			if (t.charAt(i)==alphaChar){
			    isLetter=true
				break;
			}
			else{
				isLetter=false
			}
		}
		if(isLetter==false)
		 	return false
	}
	return isLetter
}

//*******************************************************************************************************
// Checks if a string is doesnt contain any illegal characters, returns true or false 
function isVaild(text){
	var t=trim(text)
	//if (t=="")
		//return false
		
	var isLetter=true
	//t=t.toLowerCase()

    var alpha="|"
	 //var alpha="abcdefghijklmnopqrstuvwxyz1234567890"
	// compare each letter of string with letters of alphabet, set flag 
	for (i=0;i<t.length;i++){
		for(j=0;j<alpha.length;j++){
			alphaChar=alpha.charAt(j);
			if (t.charAt(i)==alphaChar){
			    isLetter=false
				break;
			}
			else{
				isLetter=true
			}
		}
		if(isLetter==false)
		 	return false
	}
	return isLetter
}

//*******************************************************************************************************

// Checks if a string is alphanumeric, returns true or false 
function isNumeric(text){
	var t=trim(text)
	//if (t=="")
		//return false
		
	var isNumber=true
	t=t.toLowerCase()

    var alpha="1234567890"
	
	// compare each letter of string with letters of alphabet, set flag 
	for (i=0;i<t.length;i++){
		for(j=0;j<alpha.length;j++){
			alphaChar=alpha.charAt(j);
			if (t.charAt(i)==alphaChar){
			    isNumber=true
				break;
			}
			else{
				isNumber=false
			}
		}
		if(isNumber==false)
		 	return false
	}
	return isNumber
}
//test that string has no whitespace
function noWhite(text){
    var alpha=" ";
	var l = text.length;
	if (l > 0){
		// compare each letter of string with letters of alphabet, set flag 
		for (i=0;i<l;i++){
	
				if (text.charAt(i)==alpha){
					//alert(text.charAt(i));
					space=false;
					break;
				}
				else
					space=true;
		}
	}//end if length gt 0
	//alert(isLetter);
	if (space==false){
		alert("Your CODE cannot contain any whitepsace");
	}
	return space;
}

currentID=1;
//
function showDiv(){	
	var n = showDiv.arguments[0];
	//if second paramter has been passed and is equal to false, means that the display should only show the current selection
	var display = true;
	if((showDiv.arguments[1]!=null) && (showDiv.arguments[1]==false)){
		display = false;
	}
	//if false has been passed, turn off last layer.
	if(!display){
		old = document.getElementById("layer"+currentID)
		if(old!=null){
			old.style.display='none';
		}
	}
	//turn on content layer
	var target = document.getElementById("layer"+n);	
	if(target!=null){
		target.style.display='block';
		target.style.visibility='visible';
	}
	
	//
	currentID=n;
	//
}

