header = 0;

function which(click_id,id,str){
	switch(click_id){
		case "del":
			if (confirm("Ali ste prepricani, da zelite iz seznama odstraniti '" + str + "'?"))
				{document.getElementById(id).click();}
		break;
		case "upload":
			if (preveri()){
				document.getElementById('obvestilo').innerHTML = "Poteka posiljanje slik, prosimo da tega okna ne zaprete!<br />Postopek lahko traja vec minut, odvisno od tega koliko slik ste izbrali in kaksno imate internetno povezavo!";
				document.form1.action="poslano.php";
				document.form1.submit();
			}
		break;
	}
}

function preveri(){
	if (multi_selector.count <= 1){
		alert("NAPAKA! Izbrana ni nobena datoteka...");
		return 0;
	}else if(document.getElementById('ime').value == ""){
		alert("NAPAKA! Polje ime ne sme ostati prazno!");
		document.getElementById('ime').focus();
		return 0;
	}else if(document.getElementById('priimek').value == ""){
		alert("NAPAKA! Polje priimek ne sme ostati prazno!");
		document.getElementById('priimek').focus();
		return 0;
	}else if(!preveri_email(document.getElementById('email').value)){
		alert("NAPAKA! E-Mail naslov ni veljaven!");
		document.getElementById('email').focus();
		return 0;
	}
	return 1;
}

function preveri_email(input_value){
	//podvojene @
	zblj_count = 0;
	zblj_pos = input_value.indexOf("@");
	while(zblj_pos >= 0){
		zblj_count++;
		zblj_pos = input_value.indexOf("@",zblj_pos + 1);
	}
	if (!zblj_count || zblj_count >=2){return 0;}
	//cekiranje ce so presledki
	zblj_count = 0;
	zblj_pos = input_value.indexOf(" ");
	while(zblj_pos >= 0){
		zblj_count++;
		zblj_pos = input_value.indexOf(" ",zblj_pos + 1);
	}
	if (zblj_count){return 0;}	
	//cekiranje vsebine pred in za @
	zblj_pos = input_value.indexOf("@");
	zblj_po = input_value.substr(zblj_pos + 1,1);
	
	if (!zblj_pos || !zblj_po){return 0;}
	return 1;
}

function isInteger(tmp) {
	for (i = 0; i < tmp.length; i++) {
		var c = tmp.charAt(i);
		if(i == 0){
			from = "1";
		}else{
			from = "0";
		}
		if (!((c >= from) && (c <= "9"))) {
			return false;
		}
	}
	return true;
}

function if_int(input){
	if(!isInteger(input.value)){
		input.value = "1";
	}
}

function check_field(input){
	if(input.value == ""){
		input.value = "1";
	}
}

function picture_size_dropdown(id){
	picture_size = '<select name="size' + id + '" id="size' + id + '"><option value="10x15" style="width:35px;" >10x15</option><option value="9x13">9x13</option><option value="13x18">13x18</option><option value="15x21">15x21</option><option value="20x25">20x25</option><option value="20x30">20x30</option><option value="30x40">30x40</option><option value="50x60">50x60</option><option value="60x90">60x90</option></select>';
	return picture_size;
}

function quantit_of_pictures(id){
	quantity = '<input type="text" name="quantity' + id + '" id="quantity' + id + '" value="1" style="width:35px;text-align:center;" onKeyUp="javascript: if_int(this);" onBlur="javascript: check_field(this);" />';
	return quantity;
}

function MultiSelector( list_target, max ){

	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};

	this.addElement = function( element ){

		if( element.tagName == 'INPUT' && element.type == 'file' ){
			element.name = 'file_' + this.id++;
			element.multi_selector = this;
			
			element.onchange = function(){
				
				tmp = element.value;
				tmp = tmp.substr(tmp.length - 4);
				tmp = tmp.toLowerCase();
				
				if (tmp == ".jpg" || tmp == "jpeg" || tmp == ".bmp" || tmp == ".gif" || tmp == ".pcx" || tmp == ".psd" || tmp == ".tif" || tmp == "tiff" || tmp == ".pdf"){
					var new_element = document.createElement( 'input' );
					new_element.type = 'file';
					new_element.className = "text_field";
					new_element.size = "50";
					
					// Add new element
					this.parentNode.insertBefore( new_element, this );
	
					// Apply 'update' to element
					this.multi_selector.addElement( new_element );
	
					// Hide this
					this.style.display = 'none';
					
					// Update list
					this.multi_selector.addListRow( this );
				}else{
					alert("NAPAKA! Slika mora biti formata 'jpg'...");
					element.value = "";
				}
			};
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			if (this.count == 1) {
				this.list_target.innerHTML = "";
			}
			
			this.count++;
			
			this.current_element = element;
			
			header = 1;
		} else {
			alert( 'NAPAKA' );
		};
	};
	this.addListRow = function( element ){
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';
		new_row_button.style.display = "none";
		
		new_row.element = element;

		new_row_button.id = "button_" + element.name;
		
		new_row_button.onclick= function(){
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			this.parentNode.element.multi_selector.count--;
			this.parentNode.element.multi_selector.current_element.disabled = false;
			if (this.parentNode.element.multi_selector.count == 1){
				this.parentNode.element.multi_selector.list_target.innerHTML = "..:: ni izbranih slik ::..";
				document.getElementById('header').innerHTML = "";
			}
		};
		tmp = element.value;
		pos = tmp.indexOf("\\");
		while (pos != -1){
			tmp = tmp.substr(pos + 1);
			pos = tmp.indexOf("\\");
		}
		
		size_dropdown = picture_size_dropdown(this.id-2);
		quantity_dropdown = quantit_of_pictures(this.id-2);
		
		document.getElementById('header').innerHTML = '<table width="470" border="0" cellspacing="0" cellpadding="0"><tr><td align="left">Ime datoteke:</td><td align="center" width="90">Velikost:</td><td align="center" width="70">St. slik:</td><td width="100" align="center">&nbsp;</td></tr></table>';
		
		new_row.innerHTML = '<table width="470" border="0" cellspacing="0" cellpadding="0"><tr><td align="left">' + tmp + '</td><td align="center" width="90">' + size_dropdown + '</td><td align="center" width="70">' + quantity_dropdown + '</td><td width="100" align="center"><input type="button" value="Odstrani" onClick="which(\'del\',\'' + new_row_button.id + '\',\'' + tmp + '\');"></td></tr></table>';
		new_row.appendChild( new_row_button );
		this.list_target.appendChild( new_row );
	};
};
