/////////////////////////////////////////////////////////
//
// 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;
var elment;
	  function DropList(selectid) {
	//	alert(selectid);
	//echo (document.getElementById(selectid).opened );
	
            var n = document.getElementById(selectid).options.length;
            document.getElementById(selectid).size = n-n+10;
        }


function ManageTabPanelDisplay() {

 


//
// Between the parenthesis, list the id's of the div's that 
//     will be affected when tabs are clicked. List in any 
//     order. Put the id's in single quotes (apostrophes) 
//     and separate them with a comma -- all one line.
//
var idlist = new Array('tab1focus','tab2focus','tab3focus','tab4focus','tab5focus','tab1ready','tab2ready','tab3ready','tab4ready','tab5ready','content1','content2','content3','content4','content5');
//alert(i);
//window.location="index.php";
//alert("tovabbi");
//alert(arguments[3]);
document.forms['formom'].aktivful.value=arguments[arguments.length-1];
//document.forms['formom'].submit();
// No other customizations are necessary.


str=(window.location.href);
if(str.indexOf('=')>0){
	//window.location="index.php";
//alert(document.forms['formom'].aktivful.value)	;
	document.forms['formom'].submit();
	};
	
//if(str.indexOf('')>0){
//	window.location="index.php"
	
//	};
if(arguments.length < 1) { return; }
for(var i = 0; i < idlist.length; i++) {
   var block = false;
   for(var ii = 0; ii < arguments.length; ii++) {
      if(idlist[i] == arguments[ii]) {
         block = true;
         break;
         }
      }
	//  alert(i);


//	alert (document.getElementById(idlist[i]));
   if(block) { document.getElementById(idlist[i]).style.display = "block"; }
   else { document.getElementById(idlist[i]).style.display = "none"; }
   }
}
		
function elrejt(selectid)
{	



         
		   document.getElementById(selectid).size = 1;


indexe=document.getElementById(selectid).selectedIndex;
	document.images["kep"].style.display="none";
	//document.getElementById('kep').style.display="none";
	document.getElementById('felbukkano_kep').style.display = 'none';
}

	
		function csere(selectid)
{
	//alert(selectid);

    //alert(document.getElementById(selectid).selectedIndex);
       
//tomb=new Array("0.gif","1.gif","2.gif","3.gif")

indexe=document.getElementById(selectid).selectedIndex;

//if (elment==(document.getElementById(selectid).options[indexe].value+".jpg")){exit;};


document.getElementById('kep').src="kepek/"+document.getElementById(selectid).options[indexe].value+".jpg";
document.images["kep"].src = "kepek/"+document.getElementById(selectid).options[indexe].value+".jpg";

elment=document.getElementById(selectid).options[indexe].value+".jpg";

document.getElementById('felbukkano_kep').style.display = '';
document.images["kep"].style.display="block";
document.getElementById('kep').style.display="block";
   }

		
		

function height(){
   
    myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && (document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && (document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
 return myHeight;
}
   

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

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("Rendelését leadhatja regisztráció és bejelentkezés nélkül is!\nHa bejelentkezik rendszerünkbe kedvezményekben részesítjük és képPONT-okkal jutalmazzuk,\nmelyeket rendeléseinél további kedvezmények elérésére használhat fel!") )
			return false;

	return true;
}//confirmLoginAlert

function SubmitOrder2( 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("Rendelését leadhatja regisztráció és bejelentkezés nélkül is!\nHa bejelentkezik rendszerünkbe kedvezményekben részesítjük és képPONT-okkal jutalmazzuk,\nmelyeket rendeléseinél további kedvezmények elérésére használhat fel!\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! Ellenőrizze, hogy minden képhez adott-e meg darabszámot a "Képek feltöltése" menüpont alatt!' );
				 	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 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("Rendelését leadhatja regisztráció és bejelentkezés nélkül is!\nHa bejelentkezik rendszerünkbe kedvezményekben részesítjük és képPONT-okkal jutalmazzuk,\nmelyeket rendeléseinél további kedvezmények elérésére használhat fel!\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! Ellenőrizze, hogy minden képhez adott-e meg darabszámot a "Képek feltöltése" menüpont alatt!' );
				 	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>

//getInputselectsBaseId

function getInputselectsBaseId( inputId ) {
	var result= '';
	var ideigl='';
	
	var i = inputId.lastIndexOf('GADGETS_selected');
	
	if ( i  > -1 ) {

	ideigl=inputId.substr(i,inputId.length);
	
	

	}//if
     result=ideigl;
	return result;
}//getInputselectsBaseId



function getInputBaseId( inputId ) {
	var result= '';
	var ideigl='';
	
	var i = inputId.lastIndexOf('_');

	if ( i  > -1 ) {
	//	i = inputId.indexOf('_', i+1);
	
	ideigl=inputId.substr(0,i);
	//window.alert(ideigl);
	
	i = ideigl.lastIndexOf('_');
	ideigl=inputId.substr(0,i);

	i = ideigl.lastIndexOf('_');
	ideigl=inputId.substr(0,i);
	
	
	ideigl=inputId.substr(i,inputId.length-1);

		
		if (inputId.indexOf('GADGETS_darab')>-0){ideigl=inputId.substr(inputId.indexOf('GADGETS_darab'),inputId.length-1)}

		
		result = ideigl;
	}//if

	return result;
}//getInputBaseId




//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 = getInputselectsBaseId(sourceSelectAry[i].id);
		strBuff = 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 ( getInputselectsBaseId(sourceSelects[k].id) == getInputselectsBaseId(selectAry[j].id) ) { //baseID does match
						//	window.alert('egyezik');
							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

