﻿function getFolderName(strInput){
//    var strShortName='';
    strInput=trim(strInput.toLowerCase());
    strInput=toPlainText(strInput);
    strInput=toUnsingedText(strInput);
    strInput=toTitleCase(strInput);
    while(strInput.indexOf(' ')>-1){
        strInput=strInput.replace(' ', '-');
    }
    
//    for(i=0;i<strInput.length;i++){
//        var c=strInput.charAt(i);
//        strShortName+=c;
        
//        if(c=='-' || !isSpecialChars(c)){
//            strShortName+=c;
//        }
//    }
    
    while(strInput.indexOf('--')>-1){
        strInput=strInput.replace('--', '-');
    }    
    return strInput;    
}

function getShortString(src, maxLength){
    
    var output='';
	if(src.length>maxLength)
	{
		output=src.substring(0, maxLength);
		var spaceIndex=output.lastIndexOf(' ');
		if(spaceIndex>-1)
		{
			output=output.substring(0, spaceIndex);
		}
	}
	else
	{
        output=src;
	}

	return output;
}

function toTitleCase(strInput){
	var strArray=strInput.split(' ');
	var strOutput='';
	for(i=0;i<strArray.length;i++){
		var word=strArray[i];
		var c=word.charAt(0);
//		strOutput+=c.toUpperCase() + word.substring(1, word.length);

		if(!isSpecialChars(c)){
	        strOutput+=c.toUpperCase() + word.substring(1, word.length);
	    }
	    
	    else{
	        if(word.length>1){
	            strOutput+=c + word.charAt(1).toUpperCase() + word.substring(2, word.length);
            }else{
                strOutput+=c + word.substring(1, word.length);
            }
	    }

		if(i<strArray.length-1){
			strOutput+=' ';
		}		
        
	}

	return trim(strOutput);
}

function toUnsingedText(strInput){
	var strTemp='';
	
	for(i=0;i<strInput.length;i++){
		var c=strInput.charAt(i);

		switch(c){
			case 'é': case 'è': case 'ẹ': case 'ẽ': case 'ẻ':
				strTemp+='e';
				break;
				
			case 'ê': case 'ế': case 'ề': case 'ệ': case 'ễ': case 'ể':
				strTemp+='e';
				break;

				//y related chars
			case 'ý': case 'ỳ': case 'ỵ': case 'ỹ': case 'ỷ':
				strTemp+='y';
				break;

				//u related chars
			case 'ú': case 'ù': case 'ụ': case 'ũ': case 'ủ':
				strTemp+='u';
				break;
			case 'ư': case 'ứ': case 'ừ': case 'ự': case 'ữ': case 'ử':
				strTemp+='u';
				break;
			
				//y related chars
			case 'í': case 'ì': case 'ị': case 'ĩ': case 'ỉ':
				strTemp+='i';
				break;

				//o related chars
			case 'ó': case 'ò': case 'ọ': case 'õ': case 'ỏ':
				strTemp+='o';
				break;
			case 'ơ': case 'ớ': case 'ờ': case 'ợ': case 'ỡ': case 'ở':
				strTemp+='o';
				break;
			case 'ô': case 'ố': case 'ồ': case 'ộ': case 'ỗ': case 'ổ':
				strTemp+='o';
				break;

				//a related chars
			case 'á': case 'à': case 'ạ': case 'ã': case 'ả':
				strTemp+='a';
				break;

			case 'ă': case 'ắ': case 'ằ': case 'ặ': case 'ẵ': case 'ẳ':
				strTemp+='a';
				break;
			case 'â': case 'ấ': case 'ầ': case 'ậ': case 'ẫ': case 'ẩ':
				strTemp+='a';
				break;

				//đ related chars
			case 'đ':
				strTemp+='d';
				break;

			default:
			    if(allowChars(c)){
			        strTemp+=c;
			    }
				break;
		}
		
	}

	return strTemp;
}

function replaceString(strInput, searchString, replaceString){
    while(strInput.indexOf(searchString)>-1){
        strInput=strInput.replace(searchString, replaceString);
    }
    
    return strInput;
}

function toPlainText(strInput){
    var reg=/[-_=\+\\\|<>,.\?:;'‘’`“”~!\@#\$\%\^&*()\"\[\]\{\}\/\t\r\n–]+/g;
    strInput=strInput.replace(reg, ' ');
    while(strInput.indexOf('  ')>-1){
        strInput=strInput.replace('  ', ' ');
    }    

    return strInput;
}

function toText(strInput){
    var reg=/[‘’`“”~\^\t\r\n–]+/g;
    strInput=strInput.replace(reg, ' ');
    while(strInput.indexOf('  ')>-1){
        strInput=strInput.replace('  ', ' ');
    }    

    return strInput;
}

function trim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	while (sString.toUpperCase().substring(sString.length-4, sString.length) == '<BR>')
	{
		sString = sString.substring(0,sString.length-4);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}	
    while(sString.indexOf('  ')>-1){
        sString=sString.replace('  ', ' ');
    }
	return sString;
}

function isSpecialChars(c){
    var reg=/[^-_=\+\\\|<>,.\?:;'‘’`“”~!\@\#\$\%\^\&\*\(\)\""\[\]\{\}\t\r\n\x20\s ]+/;
	var foundChars=c.match(reg);

	if(foundChars!=null){
		return false;
	}
	else{
		return true;
	}    
}


function allowChars(c){
	var regSpecialChars=/[^a-zA-Z0-9 ]/g;
	var foundChars=c.match(regSpecialChars);
	if(foundChars!=null){
		return false;
	}
	else{
		return true;
	}
}
/*
function charIsValid(strInput){
	var reg=/[^a-zA-Z0-9_-]/g;
	var foundChars=strInput.match(reg);
	if(foundChars!=null){
		return false;
	}
	else{
		return true;
	}
}



function parseKeyword(strInput){
    var words=strInput.split(' ');
    var keyword='';
    
    if(words.length>0){
        var wordIndex=0;
        var word=checkKeyword(words[wordIndex]);

        while(wordIndex<words.length){
            if(keyword.indexOf(word)==-1){
                keyword+=word + ', ';
            }
            word=checkKeyword(words[++wordIndex]);
        }
    }
    while(keyword.indexOf(' ,')>-1){
        keyword=keyword.replace(' ,', ',');
    }
    while(keyword.indexOf(', ,')>-1){
        keyword=keyword.replace(', ,', ', ');
    }
        
    while(keyword.lastIndexOf(', ')==keyword.length-2){
        keyword=keyword.substring(0, keyword.length-2);
    }
    
    return keyword;
}

function checkKeyword(word){
    var specialChar='~!@#$%^&*()+=\|,./<>?;\':\"-“”';
    if(word!=null && word!=''){
	    for(i=0;i<specialChar.length;i++){
	        var c=specialChar.charAt(i);
	        while(word.indexOf(c)>-1){
	            word=word.replace(c, '');
            }
        }
    }
    
    return word + ' ';
}
*/

function postBackEncode(strInput){
    strInput=replaceString(strInput, '<', '[[[');
    strInput=replaceString(strInput, '>', ']]]');
    strInput=replaceString(strInput, '&', '###AND###');
    return strInput;
}

function postBackDecode(strInput){
    strInput=replaceString(strInput, '[[[', '<');
    strInput=replaceString(strInput, ']]]', '>');
    strInput=replaceString(strInput, '###AND###', '&');
    return strInput;
}