function openWebPage(webUrl){
	height	=	500;
	width	=	500;
	myWindow = window.open(webUrl, "mywindow", "location=1, status=1, scrollbars=1, width=500, height=500");
	topX = (screen.height - height)/2;
	leftY = (screen.width - width)/2;
	myWindow.moveTo(leftY, topX);
	window.print();
}
function isNotBlank(str) {
	nochar="\n\r\t ";
	for(i=0;i<str.length;i++)
		if(nochar.indexOf(str.charAt(i))<0)
			return true;
	return false;
}
function isStrLenMin(str, lmin) {
	if(str.length >= lmin) return true;
	return false;
}
function isStrLenMax(str, lmax) {
	if(str.length <= lmax) return true
	return false;
}
function isEmail(str) {
	if(isNotBlank(str)) {
		re=/^\w.+@\w+[.]\w+[\.]?[a-z,A-Z,0-9]+$/
		if(re.test(str)) return true;
		else {
			return false;
		}
	} else return false;
}

$(document).ready(function() {
	$('a.poplight[href^=#]').click(function() {
		
		var s_content =  $('#cscx_content').val();
		
		if (s_content == '' || s_content == 'Nội dung chia sẻ') {
			alert('Vui lòng nhập nội dung.');
			return false;
		}
		
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
	});
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	    $('#fade , .popup_block').fadeOut(function() {
	        $('#fade, a.close').remove();  //fade them both out
	    });
	    return false;
	});
	
	$('#btnSendCSCX').click(function() {
		var s_name = $('#cscx_name').val();
		var s_mail = $('#cscx_email').val();
		var s_content =  $('#cscx_content').val();
		
		if (s_name == '' || s_name == 'Họ & Tên') {
			alert('Vui lòng nhập Họ tên của bạn.');
			return false;
		}
		
		if (!isEmail(s_mail)) {
			alert('Địa chỉ email không chính xác.');
			return false;
		}
		
		$.post('includes/process_chiasecx.php',{'name':s_name,'email':s_mail,'content':s_content},function(data) {
			alert(data);
			$('#fade , .popup_block').fadeOut(function() {
	        $('#fade, a.close').remove();
	    });
		});
		
	});
	
});
