function getTimestampString(time){

	var date = new Date();

	date.setTime(time);
	var y = date.getYear();
	if(navigator.appName=="Netscape"){
		y+=1900;
	}
	var m = date.getMonth()+1;
	var _date = y+"/"+m+"/"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
	return _date;
}

var sid = null;

function getStatus(_sid){
	sid = _sid;
	_getStatus(sid);
}

function _getStatus(){
	try{

	var fileuploadStatus = Jsonrpc1.FileUploadDownloadStatusRepository.get(sid);
	printFileuploadStatus(fileuploadStatus);

	if(fileuploadStatus.contentLength==0 && fileuploadStatus.readCount==5){
		$("message").innerHTML = "<font color='#FF0000'>The contents size was not able to be taken correctly.<br/>"+
		"A browser might not operate correctly when the file that exceeds 1GB is specified. <br/>"+
		"Rereading will be automatically crowded in ten seconds. </font>";
		setTimeout("location.reload()",10000);
	}

	if(fileuploadStatus.status==1){
		setTimeout(_getStatus, interval);
	}
	if(fileuploadStatus.status==2){
		$("fileq_uploadbutton").disabled=false;
		var limitDate = getTimestampString(fileuploadStatus.limitDate.time);
		$("message").innerHTML = "The date of the storage limitation when the file was kept is "+limitDate+",Thank you.";
	}

	if(fileuploadStatus.status<0){
		$("fileq_uploadbutton").disabled=false;
		var form = $("uploadForm");
		$("message").innerHTML = "<font color='#FF0000'>"+fileuploadStatus.message+"</font>";
		$("upload_progress").style.width = "0%";
		if(fileuploadStatus.status==-1||fileuploadStatus.status==-5){
			$("message").innerHTML = $("message").innerHTML +
				"<br/><font color='#FF0000'> Rereading will be automatically crowded in five seconds. </font>";
			setTimeout("location.reload()",5000);
		}
	}

	}catch(exception){
		alert(exception.message);
	}
}

function printFileuploadStatus(fileuploadStatus){
	if(fileuploadStatus.contentLength > 0){
		$("log").innerHTML = fileuploadStatus.bytesRead +"/"+ fileuploadStatus.contentLength;
		var a = Math.round(fileuploadStatus.bytesRead * 100 / fileuploadStatus.contentLength);
		$("upload_progress").style.width = a + "%";
	}
}

function createNewFileUploadInfomation(){
	return {
		sid:null,
		lang:"en",
		javaClass:"com.lisonal.fileupload.FileUploadInfomation",
		to_email:{list:[],"javaClass":"java.util.ArrayList"},
		from_email:null,
		mail_comment:null,
		mail_subject:null
	};
}

function mailaddressCheck(mailAddress){
	if (mailAddress.search(/^[\w\d\._-]+@[\w\d_-]+\.[\w\d._-]+$/)==0) {
		return 0;
	}
	return -1;
}

function formCheck(){
	if($("fileq_to_email1").value==""){
		alert("Receiver E-mail Address is an empty column.");
		return -1;
	}

	if(mailaddressCheck($("fileq_to_email1").value)==-1){
		alert("Receiver E-mail Address is not correct.");
		return -1;
	}

	if($("fileq_file1").value==""){
		alert("The transmitted file is not specified.");
		return -1;
	}
	return 0;
}

function go(){
	try{
		if(formCheck()==-1){
			return;
		}

		// var sid = setSid();

		var fileUploadInfomation = createNewFileUploadInfomation();
		fileUploadInfomation.to_email.list.push($("fileq_to_email1").value);
		// 追加アドレスも配列に入れる
		for(var i=2; i<=nowToAddress; i++){
			if($("fileq_to_email"+i).value){
				if(mailaddressCheck($("fileq_to_email"+i).value)==0){
					fileUploadInfomation.to_email.list.push($("fileq_to_email"+i).value);
				}
			}
		}

		fileUploadInfomation.from_email = $("fileq_from_email").value;
		fileUploadInfomation.from_name = $("fileq_from_name").value;
		try{
			fileUploadInfomation.mail_comment = $("fileq_mail_comment").value;
			fileUploadInfomation.mail_subject = $("fileq_mail_subject").value;
		}catch(exception){
		}
		// fileUploadInfomation.sid = sid;
		var sidnew = Jsonrpc1.FileUploadInfomationManager.setFileUploadInfomationNew(fileUploadInfomation);
		sid = setSidNew(sidnew);

		var form = $("uploadForm");
		form.target = 'upload_iframe';
		form.submit();

		//alert(fileUploadInfomation.files.from_email);
		//alert(fileUploadInfomation.files.from_name);

	    $("upload_pb").style.display="block";
		$("upload_progress").style.width = "0%";
		$("fileq_uploadbutton").disabled=true;
		$("log").innerHTML = "";
		$("message").innerHTML = "";
		getStatus(sidnew);
	}catch(exception){
		alert(exception.message);
	}
}

function addToAddressForm(){
	if(nowToAddress==maxToAddress){
		alert("E-mail Address that you can add is up to "+maxToAddress+".");
		return;
	}

	var ele = document.createElement("input");
	// <input type="input" size="40" maxlength="60" id="fileq_to_email1" name="fileq_to_email1">
	ele.size = 40;
	ele.maxLength = 60;
	nowToAddress++;
	ele.id = "fileq_to_email" + nowToAddress;
	ele.name = "fileq_to_email" + nowToAddress;
	$("form_to_email").appendChild(ele);
}

function delToAddressForm(){
	if(nowToAddress==1){
		alert("One or more addresses are necessary. ");
		return;
	}
	try{
		var id = "fileq_to_email" + nowToAddress;
		var ele = $(id);
		$("form_to_email").removeChild(ele);
		nowToAddress--;
	}catch(exception){}
}


/* ------------------------------------------------------------------------------- */
setSlider();

