// Common JavaScript functions

var dofilter = false; // Enable file filter
var re = /^(\.php)|(\.sh)|(\.inc)|(\.htm)|(\.html)|(\.xml)|(\.xhtml)|(\.xslt)|(\.cgi)|(\.js)|(\.jsp)/; // Disallow these file types
var myButtonEnabled = 1;


function getObj(arg) {
	var obj = (document.getElementById) ? document.getElementById(arg) : eval("document.all['" + arg + "']");
	return obj;
}

function showHideBlock(objName, show) {
	var object = getObj(objName);
	var object_link = getObj(objName+'_link');
	
	if (show == "hide") {
		object.style.display = "none";
	} else if (show == "show") {
		object.style.display = "";
	} else {
		if (object.style.display == "none") {
	    	if (objName == 'more_files') object_link.innerHTML = '<< További fájlok bezárása';
			if (objName == 'more_recipient') object_link.innerHTML = '<< További címzettek bezárása';
			object.style.display = "";
		} else {
	    	if (objName == 'more_files') object_link.innerHTML = 'További fájlok >>';
			if (objName == 'more_recipient') object_link.innerHTML = 'További címzettek >>';
			object.style.display = "none";
		}
	}
}

function checkForm() {

    var inputs = document.getElementsByTagName('input');
    var file_values = "";
    var files_num = 0;
    var recipient_values = "";
    var recipient_num = 0;

    for(i=0 ; i < inputs.length; i++) {

    	if (inputs[i].getAttribute('name')) {
	
	    	if(inputs[i].getAttribute('name').match(/^recip_email_/)) {
				recipient_num++;
				//var PosA = inputs[i].value.indexOf("@");
				//var PosP = inputs[i].value.lastIndexOf(".");
				
				//if(inputs[i].value != "" && (PosA == -1 || PosP == -1 || PosA > PosP) ) {
				if ((inputs[i].value != "") && (!/^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,}$/i.test(inputs[i].value))) {
					alert("Érvénytelen a címzett e-mail címe!");
					
	    	    	// If error is not in the 1st recipient's field, show DIV of all recipients
		    		if (recipient_num > 2) {
						showHideBlock("more_recipient", "show");
		    		}
		    		inputs[i].focus();
		    		
		    		return false;
				}
				recipient_values += inputs[i].value;
	    	}

	    	/*if(inputs[i].getAttribute('type') == "file") {
	    		files_num++;
	    		file_values += inputs[i].value;
	    	}*/
		}
    }

    if (document.mammutmail.sender_email.value != "") {
    	
    	//var PosA = document.mammutmail.sender_email.value.indexOf("@");
  		//var PosP = document.mammutmail.sender_email.value.lastIndexOf(".");
  		
  		//if(PosA == -1 || PosP == -1 || PosA > PosP) {
  		if (!/^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,}$/i.test(document.mammutmail.sender_email.value)) {
  			alert("Érvénytelen a feladó e-mail címe!");
 		
    		document.mammutmail.sender_email.focus();
    		return false;
    	}
    	
    } else {
    	alert("Kérjük add meg a feladó e-mail címét!");
    	document.mammutmail.sender_email.focus();
    	return false;
    }

    if (recipient_values == "") {
    	alert("Kérjük add meg a címzett e-mail címét!");
		document.mammutmail.recip_email_1.focus();
        return false;
    }
    
    // Remove duplicated recipients
    for(var j = 1; j <= recipient_num; j++) {
    	var recip1 = getObj('recip_email_' + j);
		
		for(var k = j+1; k <= recipient_num; k++) {
			var recip2 = getObj('recip_email_' + k);
	    	if (recip1.value == recip2.value && recip1.value != "") {
				recip2.value = "";
	    	}
		}
    }

    if (document.mammutmail.file_uploaded.value != 1) {
    	alert("Túl gyors voltál! Vagy nem választottad még ki a küldendő fájlt, vagy nem vártad meg, amíg az teljesen feltöltődik! A feltöltés állapotát a sárga csík és a százalék érték jelzi.");
        return false;
    }

    /*if (file_values == "") {
		alert("Kérjük add meg a küldendő fájlt az útvonalával együtt!\n(vagy kattints a tallózás gombra és válassz)");
		document.mammutmail.upload_file_1.focus();
        return false;
    }

    // Warning for duplicated file selection
    for(j = 1; j <= files_num; j++) {
    	var file1 = getObj('upload_file_' + j);
		
		for(k = j+1; k <= files_num; k++) {
			var file2 = getObj('upload_file_' + k);
	    	if (file1.value == file2.value && file1.value != "") {
				alert("A megadott fájl már ki lett választva!");
				file2.focus();
				return false;
	    	}
		}
    }*/

    return true;
}

function checkFileType() {
	
	if (dofilter == false) {
		return true;
	}
	
	with (document.mammutmail) {
		for(i = 0; i < elements.length; i++) {
			if (elements[i].value.match(re)) {
				alert('Sajnos ennek a fájlnak a feltöltése nem engedélyezett: ' + elements[i].value);
				return false;
			}
		}
	}
	return true;
}


function submitForm() {
	
	if(checkForm() == false) {
		return false;
	}

	if(checkFileType() == false) {
		return false;
	}
	
    /*var Controls = getObj('upload_form');
    var Progress = getObj('upload-progress');
    
    Controls.style.display = "none";
    Progress.style.display = "block";

   	d = new Date();
	t = Math.random() + d.getTime();
	sid = hex_md5(t + " ");
	document.mammutmail.usercode.value = sid;*/
	
 	if (myButtonEnabled==1) { 
		myButtonEnabled=0;
		document.mammutmail.submit();
		return true;
		document.mammutmail.reset();
	}
}


function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
    var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "send_text";
    var countBody = opt_countBody ? opt_countBody : "countBody";
    var maxSize = opt_maxSize ? opt_maxSize : 1024;

    var field = getObj(countedTextBox);

    if (field && field.value.length >= maxSize) {
    	field.value = field.value.substring(0, maxSize);
    }
    var txtField = getObj(countBody);
    if (txtField && field.value.length > 0) { 
        //txtField.innerHTML = field.value.length;
        txtField.innerHTML = maxSize-field.value.length;
    } else {
    	txtField.innerHTML = maxSize;
    }
}

/* Flash Uploader */

function loadUploader(div_id, sid) {
	uploader_flash_file = 'uploader.swf';
	
	var UploadFlashDiv = getObj('upload-flash-'+div_id);
	
	var div_content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="330" height="50" id="uploader_'+div_id+'" align="middle">';
	div_content = div_content+'<param name="allowScriptAccess" value="always" />';
	div_content = div_content+'<param name="allowFullScreen" value="false" />';
	div_content = div_content+'<param name="wmode" value="opaque" />';
	div_content = div_content+'<param name="movie" value="'+uploader_flash_file+'?div_id='+div_id+'&sid='+sid+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="'+uploader_flash_file+'?div_id='+div_id+'&sid='+sid+'" quality="high" bgcolor="#ffffff" width="330" height="50" name="uploader_'+div_id+'" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" wmode="opaque" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />';
	div_content = div_content+'</object>';
	//div_content = div_content+'</div>';
	UploadFlashDiv.innerHTML = div_content;
}

function upload(data, name, div_id, sid) {

	//alert(data+"-"+name+"-"+div_id);
	
	if (data == 1) {
		var UploadFlash = getObj('upload-flash-'+div_id);
		var UploadEnd = getObj('upload-end-'+div_id);
		
		UploadEnd.innerHTML = name+" feltöltve. &nbsp;<a href=\"javascript:void(0);\" onclick=\"return removeFile("+div_id+");\" title=\"Fájl törlése\">Töröl</a>";
		
		UploadFlash.style.display = "none";
		UploadEnd.style.display = "block";
				
		document.mammutmail.file_uploaded.value = 1;
	}
}

function removeFile(div_id) {
	var sid = document.mammutmail.usercode.value;
	
	//alert(div_id+'-'+sid);
	
	var ajax_url = 'remove_file.php';
	var pars  = 'div_id='+div_id+'&sid='+sid;
	
	var myAjax = new Ajax.Request(ajax_url, {method: 'get',parameters: pars,onComplete: showUploader(div_id), onFailure: function() {alert("Ajax error!");} });
	
	loadUploader(div_id, sid);
		
	return true;
}

function showUploader(div_id) {
	var UploadFlash = getObj('upload-flash-'+div_id);
	var UploadEnd = getObj('upload-end-'+div_id);
	
	UploadEnd.style.display = "none";
	UploadFlash.style.display = "block";
}
