// The function proccess advertisement item
window.addEvent("domready", function(){
	initFrmMail();
});
function initFrmMail() {
	var frmId=$('frmMail');
	if(!frmId) return;
	var formValidate = [{
		field: "name",
		valid: "required",
		alert: "Please enter field name"
	//},{
		//field: "unit",
		//valid: "required",
		//alert: "Please enter field unit"
	},{
		field: "add",
		valid: "required",
		alert: "Please enter address field"	
	},{
		field: "phone",
		valid: "required",
		restrict: "[0-9]",
		alert: "Please enter phone field"	
	},{
		field: "messages",
		valid: "required",
		alert: "Please enter messages field"			
	}];
	var formObj = new htmlform(frmId, formValidate, {
		alertType: "layer",
		layerWidth: 170,
		hideInterval: 2000,
		resetForm: function(frmId){},
		onSubmit: function(){
			var request = new Request({
				url:'index.php?option=contact&action=sendmail',
				method:'post',
				headers: {'X-Request': 'JSON'},	
				onRequest:function(){
					// Loading....
					$('div_form').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
				},				
				onSuccess: function(responseText, responseXML){	
					opaceIt($('confirmEmail'),1);
					$('div_form').innerHTML = 'Successful. <a href="index.php">Click here to come back</a>';
				},
				onFailure:function(instance){
					//load return fasle
				}				
			});
			request.send(frmId);	
		}
	});
	//$('submit').addEvent('click',function(evt){
		//evt.stop();
	//});
	$('reset').addEvent('click',function(evt){		evt.stop();		$('name').value='';		$('unit').value='';		$('add').value='';		$('phone').value='';		$('messages').value='';
		//frmId.reset();
	});	
}
function ajax_sendmail(){
	alert($('frmMail'));
	var formVal=$('frmMail');
	if(!formVal) return;	
	var request = new Request({
		url: "index.php?option=contact&action=sendmail",
		method: 'post',
		headers: {'X-Request': 'JSON'},
		onRequest:function(){
			// loading.
		},
		onSuccess:function(responseText, responseXML){
			// loaded.
			alert(0);
		},
		onFailure:function(){
		}
	});
	request.send($('frmMail'));
	return false;	
}
function ajax_LoadAdv(){
	var request = new Request({
		url: "index.php?option=adv&onload=ajax",
		method: 'get',
		headers: {'X-Request': 'JSON'},
		onRequest:function(){
			// loading.
		},
		onSuccess:function(responseText, responseXML){
			// loaded.
			$('contRight').innerHTML = responseText;
		},
		onFailure:function(){
		}
	});
	request.send();
	return false;
}
// The function proccess news item
function ajax_load_news_detail(id){
		var request = new Request(
		{	
			url:"index.php?option=news&action=detail&id="+id+"&onload=ajax",
			method: 'get',
			headers: {'X-Request': 'JSON'},
			onRequest:function(){
				// Loading....
				$('content_ajax').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
			},
			onSuccess: function(responseText, responseXML) {
				// Loaded.
				//alert(responseText);
				$('content_ajax').innerHTML = responseText ;
			},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function() {
			}
		});
		request.send();
		return false;
}
function ajax_load_product_item_detail(id, category){
		var request = new Request(
		{	
			url:"index.php?option=product_items&action=ajax_item_detail&category="+category+"&id="+id+"&onload=ajax",
			method: 'get',
			headers: {'X-Request': 'JSON'},
			onRequest:function(){
				// Loading....
				$('main_content_webpart').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
			},
			onSuccess: function(responseText, responseXML) {
				// Loaded.
				//alert(responseText);
				$('main_content_webpart').innerHTML = responseText ;
			},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function() {
			}
		});
		request.send();
		return false;
}
// The function proccess product item
// paging ajax
function ajax_page_news(nav1,nav2,nav3){
	var request = new Request({
		url: "index.php?option=news&action=paging&"+nav3,
		method: 'get',
		headers: {'X-Request': 'JSON'},
		onRequest:function(){
			// Loading....
			$('loading').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
		},
		onSuccess:function(responseText, responseXML){
			// loaded.
			$('content_ajax').innerHTML = responseText;
			$('loading').innerHTML = '';
		},
		onFailure:function(){
		}
	});
	request.send();
	return false;
}
function ajax_page_product_items(nav1,nav2,nav3){
	var request = new Request({
		url: "index.php?option=product_items&action=paging&"+nav3,
		method: 'get',
		headers: {'X-Request': 'JSON'},
		onRequest:function(){
			// Loading....
			$('loading').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
		},
		onSuccess:function(responseText, responseXML){
			// loaded.
			
			$('main_content_webpart').innerHTML = responseText;
			$('loading').innerHTML = '';
		},
		onFailure:function(){
		}
	});
	request.send();
	return false;
}

function ajax_page_product_categories(nav1,nav2,nav3){
	var request = new Request({
		url: "index.php?option=product_categories&action=paging&"+nav3,
		method: 'get',
		headers: {'X-Request': 'JSON'},
		onRequest:function(){
			// Loading....
			$('loading').innerHTML = '<img src="templates/default/images/loading.gif" border="0">' ;
		},
		onSuccess:function(responseText, responseXML){
			// loaded.
			$('main_content_webpart').innerHTML = responseText;
			$('loading').innerHTML = '';
		},
		onFailure:function(){
		}
	});
	request.send();
	return false;
}
// end paging ajax
//call open popup
function opaceIt(thisObj, opaceTo, idDeleteFile) {
	if (!thisObj) return;
	if (opaceIt.mask == null) {
		opaceIt.mask = new Element("iframe").setProperties({
			frameborder: "no"
		}).setStyles({
			filter:'alpha(opacity=0)',
			overflow: "hidden",
			position: "absolute", 
			top: "0", 
			left: "0",
			"z-index": "998"
		});
		opaceIt.div = new Element("div").setStyles({
			overflow: "hidden",
			position: "absolute", 
			top: "0", 
			left: "0",
			"z-index": "998"
		}).setOpacity(1);
	}
	
	if (opaceTo != 0) {
		opaceIt.mask.injectInside($$('body')[0]);
		opaceIt.div.injectInside($$('body')[0]);		
		thisObj.injectInside($$('body')[0]);
		adjustPosition(thisObj);
	}
	else{
		opaceIt.mask.dispose();
		opaceIt.div.dispose();
		thisObj.setStyle('top', '-1500px');
		if(Browser.Engine.trident4) {
			window.onscroll = function(){
				thisObj.setStyle('top' , '-1500px');
			}
		}
		return ;
	}
	var myEffects = new Fx.Morph(thisObj,{ 'duration': '1000' });
	function adjustPosition(thisObj) {
		thisObj.setStyles({
			display:'block',
			left: window.getWidth()/2-thisObj.getCoordinates().width/2,
			top : screen.height/2-thisObj.getCoordinates().height/3 +10,
			zIndex: 999,
			visibility : 'visible'
		});
		if(Browser.Engine.trident) {
			opaceIt.mask.setStyles({
				width: window.getWidth(), 
				height: window.getScrollHeight()
			});
		}	
		if(Browser.Engine.trident4) {
			var topLayer = window.getScrollTop()+ screen.height/4-thisObj.getCoordinates().height/3 +10;
			thisObj.setStyle('top' , topLayer+'px');
		}
		opaceIt.div.setStyles({
			width: window.getWidth(), 
			height: window.getScrollHeight()
		});
	}
	if(idDeleteFile) {
		var confirmDelete = $('confirmDelete');
		var aLink = confirmDelete.getElements('a')[1];
		var href=aLink.get('href')+'&delete='+idDeleteFile;
		aLink.set('href' , href);
	}
	//set scroll for layer
	
	if(!Browser.Engine.trident4){
		if(opaceTo == 1) {
			var topLayer = screen.height/4-thisObj.getCoordinates().height/3 +10;
			thisObj.setStyles({
				'position' : 'fixed',
				'top' : topLayer+'px'
			});
		}
		else {
			thisObj.setStyles({'position' : 'absolute', 'top': '-5500px'});
		}	
	} else { //fixed bug for ie6 
		 window.onscroll = function(){
				var topLayer = window.getScrollTop() + 70;
				thisObj.setStyle('top' , topLayer+'px');
		}
	}
	//get form in layer
	var form = thisObj.getElements('form')[0];
	if(form) {
		var id = form.get('id');
		var idShowAlert = id+'Layer';
		if(opaceTo = 1) {
			if($(idShowAlert)) $(idShowAlert).setStyle('position' , 'fixed');
		}
	}		
	
}
