// JavaScript Document
function showFrame(id){
	
	loaded = document.getElementById('drop_cat_'+id);
	
	if(loaded == null){
		document.getElementById('loadingmsg').style.display="block";	
		fetchData('xwrapper',id);
	}
	else{
		
		sts  = getElementsByClassName('frameview');
		
		for(i= 0; i < sts.length; i++){
			if(sts[i].style.display=='block') sts[i].style.display='none';
		}
		
		if(id!=8 && id!=9){
			document.getElementById('mat_form').style.display='none';		
			document.getElementById('gallery_form').style.display='none';		
			
		}else if(id==8){
			document.getElementById('mat_form').style.display='block';		
			document.getElementById('sub_mat').value="Save";
		}
		else if(id==9){
			document.getElementById('gallery_form').style.display='block';			
			document.getElementById('sub_gallery').value="Save";
		}
		
		document.getElementById('drop_cat_'+id).style.display='block';
		document.getElementById('poster_form').style.display='none';		
	}
	
}

function closeThis(id){
	document.getElementById('drop_cat_'+id).style.display='none';
	if(id==8) document.getElementById('mat_form').style.display='none';		
	if(id==9) document.getElementById('gallery_form').style.display='none';		
	
}

function getElementsByClassName(needle) {
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i;
	var j;
	for (i=0,j=0;i<my_array.length;i++) {
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i];
	}
	return retvalue;
}
function choseMat(id, src){
	
	if(id==-1){
		
		document.getElementById('imgsrc').value='notselected.jpg';
		document.getElementById('matimg').src='notselected.jpg';
		document.getElementById('sub_mat').value = 'Remove';
	}
	document.getElementById('imgid').value=id;
	document.getElementById('matimg').src=src;
	
	//document.getElementById('mat_form').style.display='block';
	
	
}

function choseSupport(id, src){
	
	if(id==-1){
		document.getElementById('supimgsrc').value='notselected.jpg';
		document.getElementById('supportimg').src='notselected.jpg';	
		document.getElementById('sub_support').value = 'Remove';
	}
	document.getElementById('supimgid').value=id;
	document.getElementById('supportimg').src=src;
	
	//document.getElementById('mat_form').style.display='block';
}

function choseGallery(id, src){
	
	if(id==-1){
		document.getElementById('galimgsrc').value='notselected.jpg';
		document.getElementById('galleryimg').src='notselected.jpg';	
		document.getElementById('sub_gallery').value = 'Remove';
	}
	document.getElementById('galimgid').value=id;
	document.getElementById('galleryimg').src=src;
	
	//document.getElementById('mat_form').style.display='block';
}

function colorWall(){
	val = document.getElementById('rgb').value;
	//alert(val);
	
	if(val != '' ){
		document.getElementById('xwrapper').style.backgroundColor=val;
	}
	
	
	
	postdata = 'wallbg='+val;//'foodid='+foodid+'&unit=1&restid='+restid;	
	
	$.ajax({
		url: "/modules/mod_xmodeleur/setcolor.php",
		type: "POST",
		async: false,
		processData: false,
		data: postdata,
		error: function(){alert('hello, I am error.');},
		success: function(response){
			//alert(response);
			//$('#usernamecheck').html(response);
		}
	});
		
}

function fetchData(divid,catid){
	//val = document.getElementById('rgb').value;
	//alert(val);
	
	/*if(val != '' ){
		document.getElementById('xwrapper').style.backgroundColor=val;
	}
	*/
	
	
	postdata = 'catid='+catid;//'foodid='+foodid+'&unit=1&restid='+restid;	
	
	$.ajax({
		url: "/modules/mod_xmodeleur/fetchdata.php",
		type: "POST",
		processData: false,
		data: postdata,
		error: function(){alert('hello, I am error.');},
		success: function(response){
			//alert(response);
			$('#'+divid).append(response);
			document.getElementById('loadingmsg').style.display="none";	
			showFrame(catid);
		}
	});
		
}

function showScroll(){
		document.getElementById('fileupscroll').style.display='block';
}
function checkMaPhoto(){
	if(document.getElementById('poster_form').style.display=='none')
		document.getElementById('poster_form').style.display = 'block';
	else 
		document.getElementById('poster_form').style.display = 'none';
}
function showMsg(){
		document.getElementById('uparea').style.display='none';
		document.getElementById('msgarea').style.display='block';
}
function checkSupport(){
	//alert('Hello');
	//return false;
	
	supid = document.getElementById('supimgid').value;

	if(supid=='' || supid==0){
		alert("Please Select Support Item"+supid);
		return false;
	}
	return true;
	
	
	/*h = document.getElementById('supportheight').value;
	w = document.getElementById('supportwidth').value;
	//alert('Dimension:'+h+' '+w);
	
	if((h<=0 || h=='' || w<=0 || w=='' ) && supid!=-1){
		alert("Please provide dimensions properly");
		return false;
	}
	
	
	mx = h > w ? h : w;
	mn = h < w ? h : w;
	
	//alert('Maxmin: '+mx+' '+mn);

	
	rat = Math.round((mx / mn)*100);
	rat = rat / 100;
	//alert('Ratio: '+rat);
		
	
	if((rat==1.00 || rat==1.33 || rat==1.50 || rat==2.00) || supid==-1){
		return true;
		
	}
	else{
			alert("Given dimenssion is not printable");
			return false;
	}
	*/

}
function checkGallery(){
	//alert('Hello');
	//return false;
	
	galid = document.getElementById('galimgid').value;
	galheight = document.getElementById('galleyheight').value;
	//ht = parseInt(galheight);
	
	//alert(galheight+' ');
	
	if(galid=='' || galid==0){
		alert("Please Select Image from gallery"+galid);
		return false;
	}
	
	if(galheight == '' || galheight == 0 ){
		alert("You must provide proper height information.");
		return false;
		
	}
	return true;

}

function allowUserPhotoSize(event, qid, fileobj,response, data){
	alert(fileobj);	
	document.getElementById('fileuploadarea').style.display='none';
	document.getElementById('imgInfo').style.display = 'block';
	return true;
}

function doFileUp(){
	/*if(document.getElementById("ov_h").value=='' || document.getElementById("ov_w").value==''){
			alert("S'il vous plaît fournir l'information Largeur et Hauteur");
			return false;
	}*/
	if(isFileSelected == 0){
		alert("image s'il vous plaît sélectionner le fichier à télécharger");
		return false;
	}
	else{
		//h = document.getElementById("ov_h").value;
		//w = document.getElementById("ov_w").value;
		user = document.getElementById("user_id").value;
		//alert(user);
		//$('#uploadify').uploadifySettings('scriptData', {'height' : h, 'width':w, 'userid':user});
		$('#uploadify').uploadifySettings('scriptData', {'userid':user});
		//alert($('#uploadify').uploadifySettings('userid'));
		//$('#id_maphoto').attr('disabled', 'disabled');
		t = $('#uploadify').uploadifyUpload();
		//alert(t);
		return false;
	}
}
function heightCheck(){
	ih = document.getElementById('imageheight').value;
	iw = document.getElementById('imagewidth').value;	
	
	height = document.getElementById('ov_h').value;	
	
	
	//alert(height);
	
	width = Math.ceil((iw/ih)*height);
	document.getElementById('img_width').innerHTML = width;
}
