/////////////////////////////////////////////////////////
//
// program:             WEB based picture order site
// module:              JS functions to manage inputs
// coder:               Koszorus
// date:                2005.01.16 
// modify:              2008.08.11
// system:              Web (HTML/PHP)
// interpreter:         JavaScript
// version:             2.00
// copyrights:          (c) REIDEA Informatic - 2005
//
/////////////////////////////////////////////////////////
var uploadNum = 5;
var minUploadNum = 5;
var maxUploadNum = 50;

function getPos(theObj) {
  x = y = 0;
  h = theObj.offsetHeight;
  w = theObj.offsetWidth;
  while(theObj){
    x += theObj.offsetLeft;
    y += theObj.offsetTop;
    theObj = theObj.offsetParent;
  }
  return {height:h,width:w,x:x,y:y}
}//getPos


function confirmLoginAlert( withLoginAlert ) {
	if ( withLoginAlert )
		if ( !confirm("Jelenleg nincs bejelentkezve!\nAz így leadott rendelésre a rendszer nem tudja érvényesíteni a\nkedvezményeket, előnyöket, képPONTokat.\nBiztosan bejelentkezés nélkül folytatja?") )
			return false;

	return true;
}//confirmLoginAlert


function SubmitOrder( formName, withCheck, withLoginAlert ) {
	var formObject = document.getElementById( formName );
	if ( formObject == null ) {
		window.alert( 'Form: ' + formName + ' nem található!' );
		return false;
	}//if

	if ( withLoginAlert )
		if ( !confirm("Jelenleg nincs bejelentkezve!\nAz így leadott rendelésre a rendszer nem tudja érvényesíteni a\nkedvezményeket, előnyöket, képPONTokat.\nBiztosan bejelentkezés nélkül folytatja?") )
			return false;

	if ( withCheck ) {
		var orderDiv = document.getElementById( 'orderdiv' );
		var divAry = orderDiv.getElementsByTagName('div');
		var msg = '';
		for ( i = 0; i < divAry.length; i++ ) {
			var tableOK = false;

			if ( divAry[i].className == 'picture-cell' ) {
				var inputAry = divAry[i].getElementsByTagName('input');
				for ( j = 0; j < inputAry.length; j++ )
					if ( inputAry[j].className == 'orderinput' )
						if ( inputAry[j].value > 0 ) {
							tableOK = true;
							break;
						}//if

				if ( !tableOK ) {
					window.alert('Nem adott meg darabszámot/méretet!' );
				 	window.scrollTo(0, getPos(inputAry[1]).y);
					inputAry[1].focus(); //0th input is the button not real input
					return false;
				}//if

			}//if

		}//for

	}//if

	document.getElementById("status-win").className="progress_on";
	formObject.submit();
	return true;

}//SubmitOrder



function createUploadInputs( withLoginAlert ) {
	var uploadDiv = document.getElementById("upload-win");
	var strongTag;
	var newTag;
	var newTxt;

	//decrease if needed clear from tail to keep the values
	while( uploadDiv.childNodes.length > uploadNum ) {
		uploadDiv.removeChild(uploadDiv.childNodes[uploadDiv.childNodes.length - 1]);
	}//while

	//increase if needed 
	while( uploadDiv.childNodes.length < uploadNum ) {
		var i = uploadDiv.childNodes.length;
		var iStr = new String(i+1);
		while ( iStr.length < 3 )
			iStr = '0' + iStr;
			
		strongTag = document.createElement( 'div' );
		strongTag.setAttribute('class','input');
		newTxt = document.createTextNode( 'Kép ' + iStr + ':  ');
		strongTag.appendChild( newTxt );

		newTag = document.createElement( 'input' );
		newTag.setAttribute('type','file');
		newTag.setAttribute('id','userfile' + (i+1));
		newTag.setAttribute('name','userfile' + (i+1));
		newTag.setAttribute('size','25');
		if ( uploadDiv.childNodes.length < 1 && withLoginAlert ) // only at first element if needed
			newTag.setAttribute('onChange','return confirmLoginAlert(true)');

		strongTag.appendChild( newTag );

		uploadDiv.appendChild( strongTag );
	}//while

}//createUploadInputs



function changeUploadNum( direction, withLoginAlert ) {
	uploadNum += direction;

	if ( uploadNum < minUploadNum )
		uploadNum = minUploadNum;

	if ( uploadNum > maxUploadNum )
		uploadNum = maxUploadNum;

	createUploadInputs(withLoginAlert);
}//changeUploadNum


// **** Functions for apply order info on each picture ****



/////////////////////////////////////////////////////////
// propContainer: 
// Custom Object to store IDs and its values
function propContainer( parId, parValue ) {
	this.id = parId;
	this.value = parValue;
}//propContainer



/////////////////////////////////////////////////////////
// getInputBaseId:
// gets returns the basic part of given input ID
// pattern: <timestamp>-<count>_<filetype>_<inputBaseName>
function getInputBaseId( inputId ) {
	var result= '';

	var i = inputId.indexOf('_');
	if ( i  > -1 ) {
		i = inputId.indexOf('_', i+1);

		if ( i  > -1 )
			result = inputId.slice(i+1);
	}//if

	return result;
}//getInputBaseId



/////////////////////////////////////////////////////////
// applyOrderOnEachPicture:
// Applies ordered counts and details of given Picture
// on each other Picture
function applyOrderOnEachPicture( pictureCellId ) {
	var orderDiv = document.getElementById( 'orderdiv' );
	var divAry = orderDiv.getElementsByTagName('div');
	var sourcePictureDiv = document.getElementById( pictureCellId );
	var sourceInputs = new Array();
	var sourceSelects = new Array();
	var strBuff = '';

	if ( sourcePictureDiv == null )
		return -1;

	if ( !confirm("Az itt megadott méret és darabszámadatok másolása az összes többi képhez?") )
		return -1;

	// collect input basic IDs and values from source picture
	var sourceInputAry = sourcePictureDiv.getElementsByTagName('input');
	for ( i=0, j=0; i < sourceInputAry.length; i++ ) {
		if ( sourceInputAry[i].className == 'orderinput' ) {
			strBuff = getInputBaseId(sourceInputAry[i].id);
			if ( strBuff != '' ) {
				sourceInputs[j++] = new propContainer(strBuff, sourceInputAry[i].value);
			}//if
		}//if
	}//for

	// collect select basic IDs and values from source picture
	var sourceSelectAry = sourcePictureDiv.getElementsByTagName('select');
	for ( i=0, j=0; i < sourceSelectAry.length; i++ ) {
		strBuff = getInputBaseId(sourceSelectAry[i].id);
		if ( strBuff != '' ) {
			sourceSelects[j++] = new propContainer(strBuff, sourceSelectAry[i].selectedIndex);
		}//if
	}//for

	// set inputs and selects of all other pictures
	for ( i = 0; i < divAry.length; i++ ) {
		if ( divAry[i].className == 'picture-cell' && divAry[i].id != pictureCellId ) {
			window.scrollTo(0, getPos(divAry[i]).y);
			//inputs of picture
			var inputAry = divAry[i].getElementsByTagName('input');
			for ( j = 0; j < inputAry.length; j++ )
				for ( k = 0; k < sourceInputs.length; k++ )
					if ( 
						//	(sourceInputs[k].value != '') &&	//source not empty
						//	(inputAry[j].value == '') &&			//target empty	
							(sourceInputs[k].id == getInputBaseId(inputAry[j].id)) ) { //baseID does match
							inputAry[j].value = sourceInputs[k].value;
							break; //next picture
					}//if

			//selects of picture
			var selectAry = divAry[i].getElementsByTagName('select');
			for ( j = 0; j < selectAry.length; j++ )
				for ( k = 0; k < sourceSelects.length; k++ )
					if ( sourceSelects[k].id == getInputBaseId(selectAry[j].id) ) { //baseID does match
							selectAry[j].selectedIndex = sourceSelects[k].value;
							break; //next picture
					}//if
		}//if
	}//for

	window.alert("A méretek és darabszámok az összes képre beállítva.");
}//applyOrderOnEachPicture

