﻿//////////////////////////////////////////////////////////
// Make sure file names do not contain illegal characters.
//
// You may choose to skip this check and let the 
// uploader simply 'normalize' the file name. 
//////////////////////////////////////////////////////////
function checkFileNameFormat(){
	if(check_file_name_format == false){ return false; }
	
	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			var string = document.form_upload.elements['upfile_' + i].value;
			var num_of_last_slash = string.lastIndexOf("\\");

			if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

			var file_name = string.slice(num_of_last_slash + 1, string.length);
			var re = /^[\w][\w\.\-]{1,32}$/i;   
				
			if(!re.test(file_name)){	
  				alert("Sorry, uploading files in this format is not allowed. Please ensure your file names follow this format. \n\n1. Entire file cannot exceed 32 characters\n2. Format should be filename.extention or filename\n3. Legal characters are 1-9, a-z, A-Z, '_', '-'\n");
  				return true;
  			}
  		}
  	}
	return false;
}
  
//////////////////////////////////////////////////////////////////////
// Disallow uploading files by extention (DO NOT REMOVE .sh OR .php)
//
// If you want prevent users from uploading a file based on extention
// simply modify the regular exression eg.
// var re = /(\.php)|(\.sh)|(\.gif)|(\.jpg)$/i; 
// would prevent anyone from uploading .php, .sh, .gif, .jpg files.
//////////////////////////////////////////////////////////////////////
function checkFileExtentions(){
	if(check_file_extensions == false){ return false; }
  	
  	var re = /(\.sh)|(\.php)|(\.php3)|(\.php4)|(\.php5)|(\.py)|(\.shtml)|(\.phtml)|(\.cgi)|(\.pl)|(\.htaccess)|(\.htpasswd)$/i;
  	
  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			if(document.form_upload.elements['upfile_' + i].value.match(re)){
  				var string = document.form_upload.elements['upfile_' + i].value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extention = file_name.slice(file_name.indexOf(".")).toLowerCase(); 
  				
  				alert('Sorry, uploading a file with the extention "' + file_extention + '" is not allowed.');
  				return true;
  			}
  		}
  	}
	return false;
}
  
///////////////////////////////////////////////////////
// Make sure user selected at least one file to upload
///////////////////////////////////////////////////////
function checkNullFileCount(){
  	if(check_null_file_count == false){ return false; }
  	
  	var null_file_count = 0;
  	
  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value == ""){ null_file_count++; }
  	}
  	
  	if(null_file_count == upload_range){
		alert("Please Choose A File To Upload.");
		return true;
  	}
  	else{ return false; }
}
  
////////////////////////////////////////////////////////
// Make sure user did not select duplicate file uploads
////////////////////////////////////////////////////////
function checkDuplicateFileCount(){
	if(check_duplicate_file_count == false){ return false; }
  	
	var duplicate_flag = false;
	var file_count = 0;
	var duplicate_msg = "Duplicate Upload Files Detected.\n\n";
	var file_name_array = new Array();
        
	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){
  			var string = document.form_upload.elements['upfile_' + i].value;
			var num_of_last_slash = string.lastIndexOf("\\");

			if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

			var file_name = string.slice(num_of_last_slash + 1, string.length);
			            
			file_name_array[i] = file_name;
  		}
  	}
       
	for(var i = 0; i < file_name_array.length; i++){
		for(var j = 0; j < file_name_array.length; j++){
			if(file_name_array[i] == file_name_array[j] && file_name_array[i] != null){ file_count++; }
		}
		if(file_count > 1){
			duplicate_msg += 'Duplicate file "' + file_name_array[i] + '" detected in slot ' + (i + 1) + ".\n";
			duplicate_flag = true;
		}
		file_count = 0;
	}
       
	if(duplicate_flag){ 
		alert(duplicate_msg);
		return true; 
	}
	else{ return false; }
}

//////////////////////
// Count file uploads
//////////////////////
function countUploads(){
	var total_uploads = 0;
	
	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){ total_uploads++; }
	}
	
	return total_uploads;

}
  
//////////////////////////////////////////////////////
// Check files, submit upload and pop up progress bar
//////////////////////////////////////////////////////
function uploadFiles(){
	if(!checkpost()){ return false; }
	//if(!document.progress_bar.terms.checked){ alert('You need to accept 123Lala terms of service!'); return false; }
	
	if(checkFileExtentions()){ return false; }
	//if(checkNullFileCount()){ return false; }
	if(checkDuplicateFileCount()){ return false; }
	if(checkFileNameFormat()){ return false; }
	
	// If opera browser force pop-up mode
	if(window.opera){ imbedded_progress_bar = false; }

	if(imbedded_progress_bar){ document.form_upload.imbedded_progress_bar.value = 1; }
	else{ document.form_upload.imbedded_progress_bar.value = 0; }
	
	document.form_upload.upload_range.value = upload_range;
	document.form_upload.submit();
	document.form_upload.submit.disabled = true;
	
	for(var i = 0; i < upload_range; i++){ document.form_upload.elements['upfile_' + i].disabled = true; }
	
	if(imbedded_progress_bar){
		blocking("browse");
		document.getElementById('browse').innerHTML = "<div><input type='button' name='clear_files' value='Reset' onclick='resetForm();' /></div>";
		
		return false;
	}
	else{	
		var upWin = window.open('','pop_win_'+session_id,'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=420,height=300,left=360,top=250');
		
		if(window.focus){ upWin.focus(); }
		
		blocking("browse");
		return true;
	}
}

// Hide/show any div using block
function blocking(nr){
	if (document.layers){
		document.layers[nr].display 				= (document.layers[nr].display == 'none') ? 'block' : 'none';
	}
	else if (document.all){
		document.all[nr].style.display 				= (document.all[nr].style.display == 'none') ? 'block' : 'none';
	}
	else if (document.getElementById){
		document.getElementById(nr).style.display 	= (document.getElementById(nr).style.display == 'none') ? 'block' : 'none';
	}
}

////////////////////////////
// Add another upload slot
////////////////////////////
var check_file_extensions = true;
var check_duplicate_file_count = true;
var check_file_name_format = false;
var imbedded_progress_bar = true;
var max_upload_slots = 5;
var upload_range = 1;
function addUploadSlot(){
	var num = upload_range;
	
	if(upload_range < max_upload_slots){
		if(num == upload_range){
			var up = document.getElementById('upload_slots');
			var dv = document.createElement("div");
			dv.innerHTML = '<div class="fileholder"><input class="file" type="file" name="upfile_' + upload_range + '" size="39" /></div>';
			up.appendChild(dv);
			upload_range++;
			document.form_upload.upload_range.value = upload_range;
		}
	}
	else { alert('You can only upload ' + max_upload_slots + ' files simultaneously'); }
}

function checkpost()
	{
	tinhthanhid = document.form_upload.tinhthanhid.value;
	quanid = document.form_upload.quanid.value;
	dnumber = document.form_upload.dnumber.value;
	dientich = document.form_upload.dientich.value;
	sotang = document.form_upload.sotang.value;
	price = document.form_upload.price.value;
	phone = document.form_upload.phone.value;
	if (document.form_upload.title.value=='')
		{
		alert('Bạn chưa nhập tiêu đề !');
		document.form_upload.title.focus();
		return false;
		}
	if (document.form_upload.category.value=='')
		{
		alert('Ban chưa chọn mục đăng tin !');
		document.form_upload.category.focus();
		return false;
		}
	if (tinhthanhid=='')
		{
		alert('Bạn chưa chọn thành phố !');
		document.form_upload.tinhthanhid.focus();
		return false;
		}
	if (quanid=='')
		{
		alert('Bạn chưa chọn quận !');
		document.form_upload.quanid.focus();
		return false;
		}
	if (dnumber=='')
		{
		alert('Bạn chưa chọn số ngày đăng tin !');
		document.form_upload.dnumber.focus();
		return false;
		}
	if (document.form_upload.address.value=='')
		{
		alert('Bạn chưa nhập địa chỉ !');
		document.form_upload.address.focus();
		return false;
		}
	if (dientich=='')
		{
		alert('Bạn chưa điền diện tích bất động sản !');
		document.form_upload.dientich.focus();
		return false;
		}
	if (isNaN(dientich))
	 	{
 	   	alert("Diện tích mà bạn nhập phải là số !");
		document.form_upload.dientich.focus();
		return false;
 	 	}
	if (sotang=='')
		{
		alert('Bạn chưa điền số tầng bất động sản !');
		document.form_upload.sotang.focus();
		return false;
		}
	if (isNaN(sotang))
	 	{
 	   	alert("Số tầng mà bạn nhập phải là số !");
		document.form_upload.sotang.focus();
		return false;
 	 	}
	if (price=='')
		{
		alert('Bạn chưa điền giá bất động sản !');
		document.form_upload.price.focus();
		return false;
		}
	if (isNaN(price))
	 	{
 	   	alert("Giá mà bạn nhập phải là số !");
		document.form_upload.price.focus();
		return false;
 	 	}
 	 	
	if (document.form_upload.postby.value=='')
		{
		alert('Bạn chưa nhập họ tên người đăng !');
		document.form_upload.postby.focus();
		return false;
		}
	if (phone=='')
		{
		alert('Bạn chưa nhập số điện thoại !');
		document.form_upload.phone.focus();
		return false;
		}
	if (isNaN(phone))
	 	{
 	   	alert("Số điện thoại phải là số !");
		document.form_upload.phone.focus();
		return false;
 	 	}
 	 	
	if (document.form_upload.email.value=='')
		{
		alert('Bạn chưa nhập địa chỉ Email !');
		document.form_upload.email.focus();
		return false;
		}
		mail = /^[a-z][a-z0-9_\.]*\@[a-z]*\.[a-z]*[a-z0-9_\.]*/g;
	if(mail.test(document.form_upload.email.value)==false)
		{
		alert("Email không hợp lệ !");
		document.form_upload.email.focus();
		flag=false;
		return false;
		}
	return true;
	}

