function initFormProduct(){
	var frmProduct = $('#frmProduct');
	if(!frmProduct) return;
	
	frmProduct.find('#btnSend').click(function(){
		var errors = frmProduct.find('.error');
		var errorChk = frmProduct.find('.errorChk');
		var required = frmProduct.find('.required');
		var email = required.filter("input[name='email']");
		var address = required.filter("input[name='address']");
		var zip = required.filter("input[name='post']");
		var city = required.filter("input[name='city']");
		var chkBox = frmProduct.find('input[type=checkbox]');
				
		var hasError = false;
		var chkError = true;
		$.each(errors,function(index, it){			
			it.className = 'error hide red';
		});
		//validate checkbox		
		var products = chkBox.filter(function(){return this.checked==true;});
		if(products.length > 0){
			errorChk.addClass('hide');
		}else{
			errorChk.removeClass('hide');
		}		
		var paProd = '';
		var paProdName = '';
		$.each(products, function(index, prod){
			if(index==0){
				paProd = prod.value;
				paProdName = prod.name;		
			}else{
				paProd += ';' + prod.value;
				paProdName += ';' + prod.name;		
			}
		});
		
		var _html = $('#frmProductFail').html();
		$.ajax({
			type: "POST",
			url: "/services/checkvalidate.aspx",
			data: {
				'address': address[0].value,
				'city': city[0].value,
				'zip': zip[0].value,
				'products': paProd
			},
			success: function(msg){					
				//validate textbox
				var paText = '';
				$.each(required, function(index, item){
					if(index == 0){
						paText += item.name + '=' + item.value;							
					}else{
						paText += '&' + item.name + '=' + item.value;
					}
					if(item.className.indexOf('checkEmail')!=-1){
						if(trim(item.value) == ''){
							hasError = true;
							errors[index].className = 'error red';
						}else if(isEmail(trim(item.value)) == 0){
							hasError = true;
							errors[index].className = 'error red';
						}
					}else if(trim(item.value) == ''){				
						if((item.name == 'address' || item.name == 'post' || item.name == 'city') && msg==1){
							// hasError = true;
							// errors[index].className = 'error red';
							$('#frmProduct').addClass('hide');						
							$('#frmProductFail').removeClass('hide');
							$('#frmProductFail').html(_html + paProdName);
						}else if(msg==0){
							hasError = true;
							errors[index].className = 'error red';
						}
					}else{
						if((item.name == 'address' || item.name == 'post' || item.name == 'city') && msg==1){
							// hasError = true;
							// errors[index].className = 'error red';
							$('#frmProduct').addClass('hide');						
							$('#frmProductFail').removeClass('hide');
							$('#frmProductFail').html(_html + paProdName);
						}
					}
				});					
				if(!hasError && products.length > 0){						
					$.ajax({
						type: "POST",
						url: "/services/insertordersample.aspx",
						data: paText + '&products=' + paProd + '&areaid=' + iLangNum + '&section=' +  $('#HiddenSectionName')[0].value,
						success: function(msg){
							if(msg == 1){
								$('#frmProduct').addClass('hide');						
								$('#frmProductFail').removeClass('hide');
								$('#frmProductSuccess').addClass('hide');
								$('#frmProductFail').html(_html + paProdName);
							}else if(msg == 0){
								$('#frmProduct').addClass('hide');
								$('#frmProductFail').addClass('hide');
								$('#frmProductSuccess').removeClass('hide');
							}
							$('#formIntro').addClass('hide');
						}
					});
				}
			}
		});			
		
	});
}
function trim(value){
	return value.replace(/^\s+|\s+$/g,"");
}
function isEmail(value){
	var re = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$");
	return (value.search(re) != -1);
}

$(document).ready(function(){
	initFormProduct();
});

window.onload = function(){	
	var arrAlink = $('div.mainanswer > p.btnsgroup > a');
	var arrDiv = $('div.answersec');
	arrAlink.each(function(el) {
		$(arrAlink[el]).click(function() {
			$(arrDiv).css({display:'none'});
			$(arrDiv[el]).css({display:'block'});
			$('div.mainanswer').css({display:'none'});
			$(arrDiv[el]).find('li.question').css({display: 'none'});
			$(arrDiv[el]).find('li.firstLi').css({display: 'block'});
		});		
	});
	
	var arrDivFinal = $('div.final');
	arrDivFinal.each(function(el) {
		$(arrDivFinal[el]).css({display:'none'});		
	});
	
	var arrLis = $('ul.questionaire  > li.question');
	var thePoint = 0;
	arrLis.each(function(el){
		$(arrLis[el]).css({display:"none"});
		if($(arrLis[el]).find('a')) {
			$(arrLis[el]).find('a').click(function() {
				
				var arrRadio = $(arrLis[el]).find('input[@type=radio]');
				var flag = false;
				arrRadio.each(function(elRadio) {
					if(arrRadio[elRadio].checked) {
						flag = true;
						thePoint = parseInt(thePoint, 10) + parseInt(arrRadio[elRadio].value, 10);
					}
				});
				if(flag) {
					if($(arrLis[el + 1])) {
						if($(arrLis[el]).attr('class') == "theChildren question") {
							if(thePoint >= 0 &&  thePoint < 10) {
								$(arrDivFinal[0]).css({display:'block'});	
							}
							if(thePoint > 9) {
								$(arrDivFinal[1]).css({display:'block'});	
							}
						}
						if($(arrLis[el]).attr('class') == "theAdult question") {
							if(thePoint >= 0 &&  thePoint < 7) {
								$(arrDivFinal[2]).css({display:'block'});	
							}
							if(thePoint > 6 &&  thePoint < 15) {
								$(arrDivFinal[3]).css({display:'block'});	
							}
							if(thePoint > 14) {
								$(arrDivFinal[4]).css({display:'block'});	
							}
						}
						$(arrLis[el]).css({display:"none"});
						$(arrLis[el + 1]).css({display:"block"});
						
					}
				}	
			});
		}		
	});
	
	//custom selectBox
	customSelectboxScroller();	
}
  
var oldTable = 0;
function changeLocation(selectBox){
	var tables = $('#form1').find('table');	
	$(tables[oldTable]).css({
		display:"none"
	});
	oldTable = $(selectBox).get(0).value;
	$(tables[oldTable]).css({
		display:""
	});
	var divPopup = $('div.popup');
	divPopup.each(function(el, item) {
		$(item).css({
			display:"none"
		});
	});
}

function openPopup(popupID, aTag){
	var divPopup = $('div.popup');
	divPopup.each(function(el, item) {
		$(item).css({
			display:"none"
		});
	});	
	
	$("#"+popupID).css({
		display:"block",
		top: $(aTag).position().top - 20,
		left: $(aTag).position().left + 203
	}).find('a.close').click(function(e) {
		$(divPopup).css({
			display:"none"
		});
	});
	IEPNGFix.update();
	return false;
}

function customSelectboxScroller(){
	var selectBox = $('div.selectListT');
	var selectBoxUi = $('div.customSelect');
	var selTarget = $('div.targetSelect');
	var selNeed = $('div.needSelect');
	
	if(typeof(selectBox) == 'undefined') return;
	var arrSel = new Array($('div.customSelect').length);	
	$.each(selectBox, function(idx, sel){		
		sel = $(sel, idx);
		sel.timerScroll = null;
		sel.selectBoxIco = $(selectBoxUi[idx]).find('a.icon');
		sel.timeDuration = 5000;
		sel.selectBoxText = $(selectBoxUi[idx]).find('a.text');
		
		if(sel.hasClass('targetSelect')){
			selTarget.dfltText = $(sel.selectBoxText).find('span').text();
			selTarget.selectBoxText = sel.selectBoxText;
		}
		
		if(sel.hasClass('needSelect')){
			selNeed.dfltText = $(sel.selectBoxText).find('span').text();
			selNeed.selectBoxText = sel.selectBoxText;
		}
		
		arrSel[idx] = 0;
		
		sel.selectBoxIco.click(function() {
			if(arrSel[idx] == 0) {
				openSelect(sel, idx);
			} else {
				closeSelectAll(sel, idx);
			}			
		}).mouseout(function(){
			sel.timerScroll = setTimeout(function(){
				closeSelect(sel, idx);
			}, sel.timeDuration);
		}).mouseover(function(){
			clearInterval(sel.timerScroll);			
		});	
		
		sel.selectBoxText.click(function() {
			if(arrSel[idx] == 0) {
				openSelect(sel, idx);
			} else {
				closeSelectAll(sel, idx);
			}
		}).mouseout(function(){
			sel.timerScroll = setTimeout(function(){
				closeSelect(sel, idx);
			}, sel.timeDuration);
		}).mouseover(function(){
			clearInterval(sel.timerScroll);			
		});	
		
		sel.mouseout(function(){
			sel.timerScroll = setTimeout(function(){
				closeSelect(sel, idx);
			}, sel.timeDuration);
		}).mouseover(function(){
			clearInterval(sel.timerScroll);			
		});
		
		
		var arrLinks = sel.find('a');		
		arrLinks.each(function(index){
			$(arrLinks[index]).click(function() {
				if(!sel){
					openSelect(sel, idx);
				}else{
					closeSelect(sel, idx);
				}
				//get value
				sel.selectBoxText.find(':first').text($(this).text());
				
				if(sel.hasClass('specialSelect'))
					window.location.href = $(arrLinks[index]).attr('href');
				
				if(sel.hasClass('targetSelect')){					
					window.paTarget = $(this).parent().attr('value');
				}
				if(sel.hasClass('needSelect')){
					window.paNeed = $(this).parent().attr('value');
				}
				
				var that = selTarget;
				if(sel.hasClass('targetSelect'))
					that = selNeed;
				
				$.ajax({
					type: 'POST',
					url: '/services/getitemavailable.aspx',
					data: 'type=' + (sel.hasClass('needSelect') ? 'need' : 'target') + '&areaid=' + iLangNum + '&id=' + $(this).parent().attr('value'),
					success: function(re){
						if(re){
							var values = $.trim(re);
							values = values.split(',');
							
							var showitems = $(that).find('ul li');
							for(var j = 0, slen = $(showitems).length; j < slen; j++){
								$(showitems[j]).css('display', 'none');
							}
							
							//var isReset = 1;
							for(var i = 0, len = values.length; i < len; i++){
								for(var j = 0, slen = $(showitems).length; j < slen; j++){
									if($(showitems[j]).attr('value') == values[i]){
										$(showitems[j]).css('display', 'block');
										
										/*
										if(sel.hasClass('needSelect') && $(values[i] == window.paTarget))
											isReset = 0;
											
										if(sel.hasClass('targetSelect') && $(values[i] == window.paNeed))
											isReset = 0;
										*/	
										break;
									}
								}
							}
							
							/*
							if(isReset){
								if(sel.hasClass('needSelect')){
									window.paTarget = '';
									that.selectBoxText.find('span').text(that.dfltText);
								}
								
								if(sel.hasClass('targetSelect')){
									window.paNeed = '';
									that.selectBoxText.find('span').text(that.dfltText);
								}
							}	
							*/
						}
					}
				});

				
			});	
		});
		var ocontainer;
		var ocontent;
		var spaceToScroll;
		customScroller(sel.find('.smScrollContent'));		
		function customScroller(el) 
		{
			// this initialises the demo scollpanes on the page.
			el.jScrollPane({
				showArrows:true, 
				scrollbarWidth:20,
				arrowSize:18
			});
			// this initialises the scrollBy and scrollTo links.
			$('.scroll-by-demo').bind('click',function(){
				el.scrollBy(parseInt($(this).attr('rel')));
				return false;
			});				
		}
		function closeSelectAll(sel, idx){
			$.each(selectBox, function(j, seleteItem){				
				seleteItem = $(seleteItem);
				seleteItem.css('top', '-15000px');
				arrSel[j] = 0;
			});
		}
		
		function closeSelect(sel, idx){
			sel.css('top', '-15000px');
			arrSel[idx] = 0;
		}
		
		function openSelect(sel, idx){
			$.each(selectBox, function(j, seleteItem){				
					seleteItem = $(seleteItem);
					seleteItem.css('top', '-15000px');
					arrSel[j] = 0;
			});
			
			if(sel.hasClass('specialSelect')){
				var coords = sel.parent().position();
				sel.css('top', sel.parent().height());				
				sel.css('left', 0);
			}else{
				var coords = sel.parent().position();
				sel.css('top', coords.top + sel.parent().height());
				sel.css('left', coords.left);
				sel.css('width', 224);
			}
			arrSel[idx] = 1;
		}
	});
}
window.paTarget = '';
window.paNeed = '';

function checkSearch(link){
	if(window.paTarget==''){
		$("#requiretarget").removeClass('hide2');
		$("#requireText").removeClass('hide2');		
		return false;
	} else if(window.paNeed==''){
		$("#requireNeed").removeClass('hide2');
		$("#requireText").removeClass('hide2');
		$("#requiretarget").addClass('hide2');
		return false;
	} else {
		location.href = ''+ link + '&target=' + window.paTarget + '&need=' + window.paNeed;		
		return false;		
	}
}

