// JavaScript Document

var allowedExtensions = "exe";

var iChars = "`~!@#$%^&*()+=[]\;,./{}|\"<>?'"; 
  
var uploadFailureError = "Error File Uploading.\nPlease Check the File Size or Type. Maximum of 4 MB file can be uploaded.";

var ajaxResponseTextSeparator = "~*~";

//files with .exe extension cannot be uploaded


function uploadDoc(){

	docName = document.getElementById("docname").value;
	
	docNameArray = docName.split( "." );
	docNameArrayLength = docNameArray.length;
	docExtn = docNameArray[ docNameArrayLength - 1 ]
	

	docfirst = docName.replace("."+docExtn," ");
	//alert( docfirst );
	
	if( allowedExtensions.indexOf( docExtn ) != -1 ){
		alert( "The following file extensions are not allowed \n\n"+ allowedExtensions );
		return false;
	}
	
	count=docfirst.length; 
	for (var i = 0; i < count; i++) 
		{ 
		//	alert(docfirst.charAt(i));
			if (iChars.indexOf(docfirst.charAt(i)) != -1) 
			{ 
				alert("Containts special characters. \n These are not allowed.\n Please remove them and try again."); return false; 
			} 
		} 
	JsHttpRequest.query('documentupload.php',{	'docName': document.getElementById("docname")	},

		function(result, errors) {

			
			output = errors.split( ajaxResponseTextSeparator );	

			uploadedDocName = output[ 0 ];		
			uploadedDocExtn = output[ 1 ];
			uploadedDocPath = output[ 2 ];	
			fileuploadedpath = output[ 3 ];	
			
			filepaths = fileuploadedpath.split(".");
			filefullpath = filepaths[2].substring(1,filepaths[2].length)+"."+filepaths[3];
			
			fileuploaded =uploadedDocPath.split("|");
			fileuploadedname = fileuploaded[0];


			//alert(uploadedDocName+"----"+fileuploadedname+"-----------"+filefullpath);
			
			//modUploadeddocname = uploadedDocName.replace(' ',"-");
			
			//alert(uploadedDocName+"----"+modUploadeddocname);
			
			if( uploadedDocName.indexOf( "Error File Uploading" ) != -1 || uploadedDocName == "" ){
				
				document.getElementById( "outputShore" ).innerHTML = uploadFailureError;
				document.getElementById( "outputShore" ).className = "show";
				
				return false;
			}
		
		document.getElementById( "outputShore" ).innerHTML = "";
		document.getElementById( "outputShore" ).className ="hide";
		document.getElementById('filename').value+= uploadedDocPath ;
		document.getElementById('filesuploaded').innerHTML += uploadedDocName;
		document.getElementById('docname').value = "";
		document.forms[0].docname.outerHTML = "<input type='file' name='docname' id='docname' value='' />";
		document.getElementById('filesuploaded').innerHTML+="&nbsp;&nbsp;&nbsp;<a href='javascript:;' name="+fileuploadedname+" id="+filefullpath+" onclick=\"filedelete(this.id,this.name);\">Delete</a><br/>";
		
		},

		false

	);
}


function uploadDocument( ){

	docName = document.getElementById("docname").value;
	
	docNameArray = docName.split( "." );
	docNameArrayLength = docNameArray.length;
	docExtn = docNameArray[ docNameArrayLength - 1 ]
	
	//alert(docNameArray[0] );
	docfirst = docName.replace("."+docExtn," ");
	//alert( docfirst );
	
	if( allowedExtensions.indexOf( docExtn ) != -1 ){
		alert( "The following file extensions are not allowed \n\n"+ allowedExtensions );
		return false;
	}
	
	count=docfirst.length; 
	for (var i = 0; i < count; i++) 
		{ 
			//alert(docfirst.charAt(i));
			if (iChars.indexOf(docfirst.charAt(i)) != -1) 
			{ 
				alert("Containts special characters. \n These are not allowed.\n Please remove them and try again."); return false; 
			} 
		} 
		
	JsHttpRequest.query('documentupload.php',{	'docName': document.getElementById("docname")	},

		function(result, errors) {

			
			output = errors.split( ajaxResponseTextSeparator );	

			uploadedDocName = output[ 0 ];		
			uploadedDocExtn = output[ 1 ];
			uploadedDocPath = output[ 2 ];	
			fileuploadedpath = output[ 3 ];	
			
			filepaths = fileuploadedpath.split(".");
			filefullpath = filepaths[2].substring(1,filepaths[2].length)+"."+filepaths[3];
			
			fileuploaded =uploadedDocPath.split("|");
			fileuploadedname = fileuploaded[0];
				

			//alert(uploadedDocName+" "+filefullpath);
			
			postedupload = uploadedDocName.replace(/ /g,"*");
			
			if( uploadedDocName.indexOf( "Error File Uploading" ) != -1 || uploadedDocName == "" ){
				
				document.getElementById( "outputShore" ).innerHTML = uploadFailureError;
				document.getElementById( "outputShore" ).className = "show";
				
				return false;
			}
		
		document.getElementById( "outputShore" ).innerHTML = "";
		document.getElementById( "outputShore" ).className ="hide";
		document.getElementById('filename').value+= uploadedDocPath ;
		document.getElementById('filesuploaded').innerHTML += uploadedDocName;
		
		document.getElementById('filesuploaded').innerHTML+="&nbsp;&nbsp;&nbsp;<a href='javascript:;' name="+postedupload+" id="+filefullpath+" onclick=\"filedeletereg(this.id,this.name);\">Delete</a><br/>";
		document.getElementById('docname').value = "";
		document.forms[2].docname.outerHTML = "<input type='file' name='docname' id='docname' value='' />";
		
		},

		false

	);
}



//Include the following code for uploading files from the upload question option
function uploadDocquest( ){
	
	
	docName = document.getElementById("docname").value;
	
	docNameArray = docName.split( "." );
	docNameArrayLength = docNameArray.length;
	docExtn = docNameArray[ docNameArrayLength - 1 ]
	

	docfirst = docName.replace("."+docExtn," ");
	//alert( docfirst );
	
	if( allowedExtensions.indexOf( docExtn ) != -1 ){
		alert( "The following file extensions are not allowed \n\n"+ allowedExtensions );
		return false;
	}
	
	count=docfirst.length; 
	for (var i = 0; i < count; i++) 
		{ 
		//	alert(docfirst.charAt(i));
			if (iChars.indexOf(docfirst.charAt(i)) != -1) 
			{ 
				alert("Containts special characters. \n These are not allowed.\n Please remove them and try again."); return false; 
			} 
		} 
	JsHttpRequest.query('documentupload.php?quest=1',{	'docName': document.getElementById("docname")	},

		function(result, errors) {

			
			output = errors.split( ajaxResponseTextSeparator );	

			uploadedDocName = output[ 0 ];		
			uploadedDocExtn = output[ 1 ];
			uploadedDocPath = output[ 2 ];	
			fileuploadedpath = output[ 3 ];	
			
			filepaths = fileuploadedpath.split(".");
			filefullpath = filepaths[2].substring(1,filepaths[2].length)+"."+filepaths[3];
			
			fileuploaded =uploadedDocPath.split("|");
			fileuploadedname = fileuploaded[0];


			//alert(uploadedDocName+"----"+fileuploadedname+"-----------"+filefullpath);
			
			//modUploadeddocname = uploadedDocName.replace(' ',"-");
			
			//alert(uploadedDocName+"----"+modUploadeddocname);
			
			if( uploadedDocName.indexOf( "Error File Uploading" ) != -1 || uploadedDocName == "" ){
				
				document.getElementById( "outputShore" ).innerHTML = uploadFailureError;
				document.getElementById( "outputShore" ).className = "show";
				
				return false;
			}
		
		document.getElementById( "outputShore" ).innerHTML = "";
		document.getElementById( "outputShore" ).className ="hide";
		document.getElementById('filename').value+= uploadedDocPath ;
		document.getElementById('filesuploaded').innerHTML += uploadedDocName;
		document.getElementById('docname').value = "";
		document.forms[0].docname.outerHTML = "<input type='file' name='docname' id='docname' value='' />";
		document.getElementById('filesuploaded').innerHTML+="&nbsp;&nbsp;&nbsp;<a href='javascript:;' name="+fileuploadedname+" id="+filefullpath+" onclick=\"filedelete(this.id,this.name);\">Delete</a><br/>";
		
		},

		false

	);

}
