// JavaScript Document

/*alert('hehe');
var _width = $(document).height();
var _height = $(document).width();

alert(_width);
alert(_height);*/
var _width;
var _height;
var _cardWidth;

//var dim; 

/*window.onload = function()
{
	//dim = $(document.body).getDimensions();
}*/

function showHideSelectTag(status)
{
	if(status != 2)
		status = 1;
	
	$('select').each(function(i){
		if(status == 1)
			$(this).show();
		else
			$(this).hide();
	});
}

function checkSendCard()
{
	card_id = $('#card_id').val();
	word_id = $('#word_id').val();
	word = $('#word').val();
	name = $('#name').val();
	email = $('#email').val();
	toEmail = $('#toEmail').val();
	key = $('#key').val();
	agree = document.getElementById('agree').checked;
	//alert(agree);
	
	if(card_id == '' ||
	   //word_id == '' ||
	   word == '' ||
	   name == '' ||
	   email == '' ||
	   toEmail == '' ||
	   key == '')
	{
		alert('Mohon semua kolom diisi !');
		return false;
	}
	else if(!agree)
	{
		alert('Centang syarat dan ketentuan');
		return false;
	}
	else
	{
		return true;
	}
	
}

function previewCard(url)
{
	//alert($('#card_id').val());
	card_id = $('#card_id').val();
	word_id = $('#word_id').val();
	word = $('#word').val();
	name = $('#name').val();
	email = $('#email').val();
	
	if(card_id == '' ||
	   //word_id == '' ||
	   word == '' ||
	   name == '' ||
	   email == '')
	{
		alert('Pilih Kartu Ucapan\nIsi Pesan di dalam kartu\nIsi nama & email !');
		return;
	}
	lockUnlockWind();
	//alert(_width);
	//var width = $(document.body).getWidth();
	var left = (_width-_cardWidth)/2;
	var pCard = $('#previewCard');
	pCard.css({width:_cardWidth+'px',left:left+'px',top:'40px',zIndex:10000});
	pCard.show();
	
	/*if(!document.getElementById('mib-cardCss'))
	{
		_link = document.createElement('link');
		_link.setAttribute('id','mib-cardCss');
		_link.rel = 'stylesheet';
		_link.href = cards[$('#card_id').val()];
		_link.media = 'screen, print';
		_link.type = 'text/css';
		
		head = document.getElementsByTagName("head");
		//alert(head);
		head[0].appendChild(_link);
	}*/
	
	showHideSelectTag(2);
	showHideLoading();
	$.post(url, { card_id:$('#card_id').val(),word_id:$('#word_id').val(),tWord:$('#tWord').val(),word:$('#word').val(),name:$('#name').val(),email:$('#email').val() }, 
		function(data){ 
			//alert(data);
			showHideLoading(2);
			$('#cardContent').html(data); 
	});

	/*new Ajax.Request(url, {
		method: 'post',
		postBody: $('frm_input_data').serialize(),
		onComplete: function(transport) {
			$('cardContent').update(transport.responseText);
		}
	});*/
}

function closeWindow()
{
	var pCard = $('#previewCard');
	showHideSelectTag();
	pCard.hide();
	lockUnlockWind(2);
}


function lockUnlockWind(status)
{
	//alert(document.getElementById('slider').style.zIndex);
	
		_width = $(document.body).width()-7;
		_height = $(document.body).height();
		
		//alert(_width);
		//alert(_height);
		
	if(status != 2)
		status = 1;
	
	$('#divLock').css({width: _width+'px',height:_height+'px',zIndex:100,opacity: 0.5});
	if(status == 1)
	{
		showHideSelectTag(2);
		$('#divLock').show();
	}
	else
	{
		showHideSelectTag(1);
		$('#divLock').hide();
	}
}

function showListCards(url)
{
	//alert(url);
	lockUnlockWind();
	
	var left = (_width-535)/2;
	$('#listCard').css({left:left+'px',top:'40px',zIndex:101});
	//alert($('#listCard').style.left)
	$('#listCard').show();

	showHideLoading();
	
	$.get(url,function(data){
		showHideLoading(2);
		$('#listCardContent').html(data);
	});
}

function closeListCards()
{
	lockUnlockWind(2);
	$('#listCard').hide();
}

function chooseCard(id,imagePath,w)
{
	//alert(id);
	$('#card_id').val(id);
	$('#choosedCard').html('<img src="'+imagePath+'" border="0" width="89" height="89" />');
	closeListCards();
	closeEnlargeImage();
	_cardWidth = w;
	//alert(_cardWidth);
}

function getEmail(url)
{
	lockUnlockWind();
	
	var left = (_width-500)/2;
	$('#divMainGetEmail').css({left:left+'px',top:'40px',zIndex:101});
	//alert($('#listCard').style.left)
	$('#divMainGetEmail').show();

	showHideLoading();
	$.get(url,function(data){
		showHideLoading(2);
		$('#divContentGetEmail').html(data);
	});
	
}

function sendGetEmail(url)
{
	showHideLoading();
	$.post(url, 
		{ 
			step:$('#step').val(),
			email_box:$('#email_box').val(),
			password_box:$('#password_box').val(),
			provider_box:$('#provider_box').val(),
			'import':$('#import').val()
		}, 
		function(data){ 
			showHideLoading(2);
			//alert(data);
			$('#divContentGetEmail').html(data); 
	});
	
}

function closeGetEmail()
{
	var pCard = $('#divMainGetEmail');
	showHideSelectTag();
	pCard.hide();
	lockUnlockWind(2);
}

function enlargeImage(id,imagePath,w)
{
	var left = (_width-w)/2;
	$('#divLock').css({zIndex:99998});
	$('#divEnlarge').css({width:w+'px',left:left+'px',zIndex:99999});
	str = '<div class="divChooseClose"><a href="javascript:chooseCard(\''+id+'\',\''+imagePath+'\',\''+w+'\');">Pilih Desain Kartu Ini</a> | <a href="javascript:closeEnlargeImage();">Kembali Ke Halaman Sebelumnya</a></div><img src="'+imagePath+'" />';
	$('#divEnlarge').html(str);
	$('#divEnlarge').show();
}
function closeEnlargeImage()
{
	$('#divEnlarge').hide();
	$('#divLock').css({zIndex:100});
}

function showHideLoading(status)
{
		
	if(status != 2)
		status = 1;
	if(status == 1)
	{
		var left = (_width-32)/2;
		var top = (_height-32)/2;
		$('#divLoading').css({left:left+'px',top:top+'px'});
		$('#divLoading').show();
	}
	else
	{
		$('#divLoading').hide();
	}
}