var numOfViewing=0;

function addNewMenu(){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
			}else{
				requestNotReady()
			}
		}
				
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=addNewMenu', true)
		xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
		xmlhttp.setRequestHeader('Content-length', parameters.length)
		xmlhttp.setRequestHeader('Connection', 'close')
		xmlhttp.send(parameters)
	}
}

function addNewMenuData(){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
			}else{
				requestNotReady()
			}
		}
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=addNewMenuData&name='+document.getElementById('name').value+'&place='+document.getElementById('place').value+'&data_order='+document.getElementById('data_order').value+'&menu_view_in_box='+document.getElementById('menu_view_in_box').value+'&data_in_box='+document.getElementById('data_in_box').value+'&box_order='+document.getElementById('box_order').value+'&content_foreach_page='+document.getElementById('content_foreach_page').value+'&view_content_format='+document.getElementById('view_content_format').value+'&parent_id='+document.getElementById('parent_id').value, true)
		xmlhttp.send(null)
	}
}

function editMenu(id){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
			}else{
				requestNotReady()
			}
		}
		
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=editMenu&id='+id, true)
		xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
		xmlhttp.setRequestHeader('Content-length', parameters.length)
		xmlhttp.setRequestHeader('Connection', 'close')
		xmlhttp.send(parameters)
	}
}

function editMenuData(){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
			}else{
				requestNotReady()
			}
		}
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=editMenuData&name='+document.getElementById('name').value+'&place='+document.getElementById('place').value+'&data_order='+document.getElementById('data_order').value+'&id='+document.getElementById('id').value+'&menu_view_in_box='+document.getElementById('menu_view_in_box').value+'&data_in_box='+document.getElementById('data_in_box').value+'&box_order='+document.getElementById('box_order').value+'&content_foreach_page='+document.getElementById('content_foreach_page').value+'&view_content_format='+document.getElementById('view_content_format').value+'&parent_id='+document.getElementById('parent_id').value, true)
		xmlhttp.send(null)
	}
}

function deleteMenuData(id){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
			}else{
				requestNotReady()
			}
		}
				
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=deleteMenuData&id='+id, true)
		xmlhttp.send(null)
	}
}

var offset=0;

function viewContent(id){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("mainData").innerHTML=xmlhttp.responseText
				document.getElementById("loadDiv").style.visibility='hidden'
				viewWhereUser();
			}else{
				requestNotReady()
			}
		}
				
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=viewContent&menu_id='+id+'&offset='+offset, true)
		xmlhttp.send(null)
	}
}

function chnagePage(id, pageOffset){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("mainData").innerHTML=xmlhttp.responseText
				document.getElementById("loadDiv").style.visibility='hidden'
			}else{
				requestNotReady()
			}
		}
				
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=viewContent&menu_id='+id+'&offset='+pageOffset, true)
		xmlhttp.send(null)
	}
}

function addNewContent(menuId){
	xmlhttp = checkBrwser()
	
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
				document.getElementById("loadDiv").innerHTML=''
					
			}else{
				requestNotReady()
			}
		}
				
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=addNewContent&menuId='+menuId, true)
		xmlhttp.send(null)
	}
	//document.getElementById("loadDiv")style.visibility='hidden'
	//document.getElementById("viewContentDiv").style.visibility='visible'
	
}

function AddContentNew(menuId){
	var description='';
	if(editor){
		var description =  tinyMCE.get('description').getContent()
	}else{
		description = document.getElementById("description").value
	}
	
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if(xmlhttp.responseText==1){
					document.getElementById("editDataDiv").style.visibility='hidden'
					createDescriptionForm(menuId, description, wwwroot+'ajax/?module=Viewing&action=createDescriptionForm')
					//viewContent(menuId)
				}
			}else{
				requestNotReady()
			}
		}
		
		var url="&menuId="+menuId
		url=url+"&title="+document.getElementById("title").value
		url=url+"&intro_description="+document.getElementById("intro_description").value
		url=url+"&data_order="+document.getElementById("data_order").value
		url=url+"&contentId="+document.getElementById("contentId").value
								
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=AddContent'+url, true)
		xmlhttp.send(null)
	}
}

function createDescriptionForm(menuId, description, action){
	var data='<form name="postDescription" id="postDescription" method="post" target="postDesciptionIfram" action="'+action+'" >\
		<textarea name="newDescription" id="newDescription">'+description+'</textarea>\
		<input type="text" name="menuId" id="menuId" value="'+menuId+'" />\
		</form><iframe id="postDesciptionIfram" name="postDesciptionIfram" style="width:0px;height:0px;border:0px;"></ifram>';
	document.getElementById("editDataDiv").innerHTML=data
	document.getElementById('postDescription').submit();
}

function stopDescription(menuId){
	document.getElementById("loadDiv").style.visibility='hidden'
	document.getElementById("editDataDiv").style.visibility='hidden'
	viewContent(menuId)
}

function viewContantDetauls(id){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("mainData").innerHTML=xmlhttp.responseText
				document.getElementById("loadDiv").style.visibility='hidden'
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=viewContantDetauls&id='+id, true)
		xmlhttp.send(null)
	}
}

function editContent(menuId, id){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("editDataDiv").style.visibility='visible'
					document.getElementById("loadDiv").innerHTML='';
				document.getElementById("loadDiv").style.visibility='visible'
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=editContent&menuId='+menuId+'&id='+id, true)
		xmlhttp.send(null)
	}
}

function deleteContent(menuId, id, image, video){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("loadDiv").style.visibility='hidden'
				viewContent(menuId)
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=deleteContent&menuId='+menuId+'&id='+id+'&image='+image+'&video='+video, true)
		xmlhttp.send(null)
	}
}

function updateSiteView(){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("msg").innerHTML=xmlhttp.responseText
			}else{
				requestNotReady()
			}
		}
		var url="&view_id="+document.getElementById("view_id").value
		url=url+"&menu_background="+document.getElementById("hidden_menu_background").value
		url=url+"&menu_color="+document.getElementById("hidden_menu_color").value
		url=url+"&big_box_width="+document.getElementById("big_box_width").value
		url=url+"&small_box_width="+document.getElementById("small_box_width").value
						
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=updateSiteView'+url, true)
		xmlhttp.send(null)
	}
}

function frontPageValues(option){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("editDataDiv").innerHTML=xmlhttp.responseText
				document.getElementById("option").value=option;
				if(option=='option1'){
					numOfViewing=1
				}else if(option=='option2a'){
					numOfViewing=2
				}else if(option=='option2b'){
					numOfViewing=2
				}else if(option=='option3a'){
					numOfViewing=3
				}else if(option=='option3b'){
					numOfViewing=3
				}else if(option=='option3c'){
					numOfViewing=3
				}else if(option=='option3d'){
					numOfViewing=3
				}else if(option=='option4'){
					numOfViewing=4
				}else if(option=='option5a'){
					numOfViewing=5
				}else if(option=='option5b'){
					numOfViewing=5
				}else if(option=='option5c'){
					numOfViewing=5
				}else if(option=='option5d'){
					numOfViewing=5
				}else if(option=='option6'){
					numOfViewing=6
				}
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=front_page&type='+option, true)
		xmlhttp.send(null)
	}
}

function editFrontPage(){
	document.getElementById('value').value=''
	for(i=1; i<=numOfViewing; i++){
		document.getElementById('value').value+=document.getElementById('view'+i).value+';'
	}
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				document.getElementById("msg").innerHTML=xmlhttp.responseText
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=editFrontPage&type='+document.getElementById('option').value+'&view='+document.getElementById('value').value, true)
		xmlhttp.send(null)
	}
}

function changeLang(lang){
	xmlhttp = checkBrwser()
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				window.location=wwwroot
			}else{
				requestNotReady()
			}
		}
					
		xmlhttp.open("GET",wwwroot+'ajax/?module=Viewing&action=changeLang&lang='+lang, true)
		xmlhttp.send(null)
	}
}
