﻿// JScript File

function ProductsListingFithHeight(){
    
    var maxHeight=0;
    
    var oThumbCells = $('.ProductsListingThumbnailCell')
    oThumbCells.each(function(idx,oItem){
        if(maxHeight<$(oItem).height()) maxHeight=$(oItem).height()
    })    
    oThumbCells.each(function(idx,oItem){
        $(oItem).height(maxHeight)
    })
    
    maxHeight=0;
    var oInfoCells = $('.ProductsListingProductInfo')
    oInfoCells.each(function(idx,oItem){
        if(maxHeight<$(oItem).height()) maxHeight=$(oItem).height()
    })
    oInfoCells.each(function(idx,oItem){
        $(oItem).height(maxHeight)
    })    
}

function ShowAlbum(sAlbumId){
    //Tracking
    var sAction = "Albums.aspx"
    var sData = 'trackalbumid=' + sAlbumId
        
        $.ajax({
				method:'get', 
				url:sAction, 
				data:sData,
				cache: false,
				beforeSend:function(){				    
				},
				complete:function(){					
				},
				success:function(sHTML){                    
				}
        })
    //Show album
    $('#bmvAlbum' + sAlbumId + '').mbGallery({maskBgnd:'#666', maskOpacity:.8});
}

////////////////////////////////////////////////////////
//Survey
////////////////////////////////////////////////////////
function SubmitSurvey(ModuleSurveyID, sAction, NoOptionMessage, ThankMessage){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var oCheckedOptions=$(sSurveyId + ' input:checked')
	
	if(! oCheckedOptions.length){
	    if(jQuery.trim(NoOptionMessage)!='') alert(NoOptionMessage)
		return false
	}
	
	var sIds = oCheckedOptions.serialize()
	while(sIds.indexOf(oCheckedOptions[0].name + '=')>=0){
		sIds=sIds.replace(oCheckedOptions[0].name + '=','')
	}
	while(sIds.indexOf('&')>=0){
		sIds=sIds.replace('&',',')
	}
	
	$.ajax({
		method		:'get',
		url			:sAction,
		data		:'ModuleSurveyID=' + ModuleSurveyID + '&option=' + sIds,
		beforeSend	:function(){			
		},
		complete	:function(){
		},
		success		:function(sResult){
			var oResultContainer = $(sSurveyId + ' .Result-Container')			
			oResultContainer.html(sResult)
			ShowResult(ModuleSurveyID)
			if(jQuery.trim(ThankMessage)!='') alert(ThankMessage)
		}
	})
}

function ShowSurvey(ModuleSurveyID){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
	var oOptionContainer = $(sSurveyId + ' .Option-Container')
	var oResultContainer = $(sSurveyId + ' .Result-Container')
	oResultContainer.css('display','none')
	oOptionContainer.css('display','block')
	btnResult.show()
}

function ShowResult(ModuleSurveyID){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
	var oOptionContainer = $('#BmvSurvey' + ModuleSurveyID + ' .Option-Container')
	var oResultContainer = $('#BmvSurvey' + ModuleSurveyID + ' .Result-Container')
	oResultContainer.css('display','block')
	oOptionContainer.css('display','none')
	btnResult.hide()
}
	
function CreateSurvey(ModuleSurveyID,sAction,NoOptionMessage,ThankMessage){    
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var oSurvey = $(sSurveyId)
	if(oSurvey){
		var oOptions = oSurvey.find(':input')
		oOptions.attr('name','BmvSurveyOption' + ModuleSurveyID)
		
		var btnSubmit = $(sSurveyId + ' .bmv-Suervey-submit-button')
		var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
		
		var oOptionContainer = $(sSurveyId + ' .Option-Container')
		var oResultContainer = $(sSurveyId + ' .Result-Container')
	
		//Setup submit button		
		if(btnSubmit){
			btnSubmit.click(function(){
				var bShowingResult = (oResultContainer.css("display")=='block')
				if(bShowingResult){
					ShowSurvey(ModuleSurveyID)
				}
				else{
					SubmitSurvey(ModuleSurveyID,sAction,NoOptionMessage,ThankMessage)
				}
				
				return false
			})
		}
		
		//Setup result button
		if(btnResult){
			btnResult.click(function(){
				ShowResult(ModuleSurveyID)				
				return false
				
			})
		}
	
	}	
}
////////////////////////////////////////////////////////
//End of Survey
////////////////////////////////////////////////////////
function HightlightGridRow(){
	var oSimRows = $('.SimGrid tr');
	oSimRows.each(function(){
		var oRow = $(this);
		oRow.hover(function(){oRow.css('background-color','#F6F6F6')},function(){oRow.css('background-color','')})				   
	})
}

//Init custom search form
function InitSearchForm(){
    
    $('.Search-form-cell input[name=Keyword]').focus(function(){
        if($(this).val()=='Dãy số liên quan') $(this).val('')
    }).blur(function(){
        if($.trim($(this).val())=='') $(this).css('color','#333333').val('Dãy số liên quan')
    })
    
    $('#tbSearch .Search-form-cell').keypress(function(e){
	    if(e.which==13){
	       $(document).get(0).location='default.aspx?act=search&' + $('.Search-form-cell input,.Search-form-cell .Input-item').serialize();
	    };
	})
	$('#btnSearch').click(function(){$(document).get(0).location='default.aspx?act=search&' + $('.Search-form-cell input,.Search-form-cell .Input-item').serialize();})
}


function PostOrder(){
    var divContainer = $('#divRequestForm');
    var divOverlay = $('<div id="divContactOverlay" class="Loading-overlay" style="width:' + divContainer.width() + 'px; height:' + divContainer.height() + 'px; background-color:#f1f2f2"></div>').css('visibility','visible');
    divContainer.prepend(divOverlay);    
    $.ajax({
        type:"POST",
        url :'GetContent.aspx?act=postorder&SimNumber=' + $('#tdSimNumber').text() + '&' + $('#divRequestForm input,#divRequestForm textarea, #divRequestForm select').serialize(),
        success: function(sRtn){            
            if($.trim(sRtn)!=''){
                alert(sRtn);
            }
            else{                
                divContainer.empty().append('<div style="padding-top:20px; padding-bottom:40px"><div style="font-size:13px; font-weight:bold; color:Red">Đơn đặt hàng của bạn đã được gửi vào hệ thống. Chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất, bấm <a href="Default.aspx">vào đây</a> để quay lại trang chủ.</div></div>');    
            }
            divOverlay.remove();
        }
    });   
}

function PostFengShuiForm(){
    var divContainer = $('#divFengShuiForm');
    var divOverlay = $('<div id="divContactOverlay" class="Loading-overlay" style="width:' + (divContainer.width()+30) + 'px; height:' + divContainer.height() + 'px; background-color:#f1f2f2"></div>').css('visibility','visible');
    divContainer.prepend(divOverlay);    
    $.ajax({
        type:"POST",
        url :'GetContent.aspx?act=fengshui&' + $('#divFengShuiForm .Input-item, #divFengShuiForm select').serialize(),
        success: function(sRtn){            
            if($.trim(sRtn)!=''){
                $('#divFengShuiFormResult').html(sRtn);
            }            
            divOverlay.remove();
        }
    });
}

$(document).ready(function(){
	HightlightGridRow();
	InitSearchForm();
	
	$('#divFengShuiForm').keypress(function(e){if(e.which==13) PostFengShuiForm()})
		
})
