﻿var flagProcessPoint;
function confirmAction(url, msg) {
	if (msg != '')	{
		if (confirm(msg)) {		
			document.location.href = url;			
		}
		return;
	}
	location.href = url;
}
function dateDiff(date1,date2,interval) {
    var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
        case "years": return date2.getFullYear() - date1.getFullYear();
        case "months": return (
            ( date2.getFullYear() * 12 + date2.getMonth() )
            -
            ( date1.getFullYear() * 12 + date1.getMonth() )
        );
        case "weeks"  : return Math.floor(timediff / week);
        case "days"   : return Math.floor(timediff / day);
        case "hours"  : return Math.floor(timediff / hour);
        case "minutes": return Math.floor(timediff / minute);
        case "seconds": return Math.floor(timediff / second);
        default: return undefined;
    }
}
function stripTags(str, option) {
    str = str.replace(/&nbsp;/g,'');
	str = jQuery.trim(str);
	if( option == 'comment_vhome' || option == 'blast' )
		allowed_tags = '<a>';
	else if( option == 'comment' )
		allowed_tags = '<a><img>';
	else if( option == 'question' )
		allowed_tags = '<a>';
	else if( option == 'mail' )
		allowed_tags = '<a><img>';
	else if( option == 'blog' ) 
		allowed_tags = '<a><img>';
	else if (option == 'faq') 
		allowed_tags = '<font>';
	else allowed_tags = '';
	var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
    str += '';
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
        // Save HTML tag
        html = matches[key].toString();
        // Is tag not in allowed list? Remove from str!
        allowed = false;
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}
function roundNumber(rnum, rlength) {
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}
function isValidImage(strFilename) {
  var ext = (strFilename.substring(strFilename.length-4, strFilename.length)).toLowerCase();
  ext = ext.replace('.', '');
  if( ext != 'jpg' && ext != 'gif' && ext != 'png' && ext != 'jpeg' ) return false;
  return true;
}
function isValidDate(dateStr, format) {
	if (format == null) { format = "MDY"; }
	format = format.toUpperCase();
	if (format.length != 3) { format = "MDY"; }
	if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
	if (format.substring(0, 1) == "Y") {
	  var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	  var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	} else if (format.substring(1, 2) == "Y") {
	  var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
	  var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
	} else {
	  var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
	  var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	}
	if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
	var parts = dateStr.split(RegExp.$1);
	if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
	if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
	if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
	if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
	if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
	var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
	if (parseFloat(dd) != dt.getDate()) { return false; }
	if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
	return true;
}
function isPhoneNumber(str){
	var alphaExp = /^((\(\+?84\)[\-\.\s]?)|(\+?84[\-\.\s]?)|(0))((\d{3}[\-\.\s]?\d{6})|(\d{2}[\-\.\s]?\d{8}))$/;
	if(str.match(alphaExp)){
		return true;
	}
	return false;
}
function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
function isAlphabet(str){
	var alphaExp = /^[a-zA-Z]+$/;
	if(str.match(alphaExp)){
		return true;
	}
	return false;
}
function isAlphabetAndNumber(str) {
	var alphaExp = /^[a-zA-Z0-9_]+$/;
	if (str.match(alphaExp)){
		return true;
	}
	return false;
}
function isNumber(str) {
	var alphaExp = /^[0-9]+$/;
	if(str.match(alphaExp)){
		return true;
	}
	return false;
}
function checkAll(strItemName, value) {
	var x=document.getElementsByName(strItemName);
	for (var i=0; i<x.length; i++) {
	  	if (value == 1) {
			if( !x[i].checked ) x[i].checked = 'checked';
		} else {
			if(x[i].checked) x[i].checked = '';
		}
	}
}
function getItemsChecked(strItemName, sep) {
	var x=document.getElementsByName(strItemName);
	var p="";
	for(var i=0; i<x.length; i++) {
		if(x[i].checked) {
			p += x[i].value + sep;
		}
	}
	var result = (p != '' ? p.substr(0, p.length - 1) : '');
	return result;
}
function setItemsChecked(strItemName) {
	vnc_setCookie( strItemName, getItemsChecked(strItemName) );
}
function vnc_setCookie( name, value, expires) {
	// set time, it's in milliseconds
	var domain = '.'+DOMAIN;
	var path = '/';
	var secure = '';
	var today = new Date();
	today.setTime( today.getTime() );
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if (expires==undefined) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function vnc_getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for (i = 0; i < a_all_cookies.length; i++) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		// if the extracted name matches passed check_name
		if  (cookie_name == check_name) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found) {
		return null;
	}
}
/*****/
function checkPointKey(e){
	var keyCode //= (window.event) ? event.keyCode : e.which;   
	if (window.event) keyCode = event.keyCode   
	else keyCode = e.which   
	// Was key that was pressed a numeric character (0-9) or , or <- or -> or backspace (8)?   
	if ( keyCode > 47 && keyCode < 58 || keyCode == 46 || keyCode == 37 || keyCode == 39 || keyCode == 8 ) {
		flagProcessPoint = true;
		return; // if so, do nothing   else // otherwise, discard character   
	}
	flagProcessPoint = false;
	if (window.event) //IE       
		window.event.returnValue = null;     
	else //Firefox       
		e.preventDefault(); 
	 
}
function filterStr(inputString, filterString){
	var outString = "";	
	for(var i=0; i<inputString.length; i++){
		if(filterString.indexOf(inputString.substr(i,1) )!=-1)
			outString = outString + inputString.substr(i,1);
	}	
	return outString;
}
function addPointer(intValue){
	var strTemp  = "";
	var isCont 	 = true;
	var strValue = intValue.value;	
	if(!flagProcessPoint) return;	
	strValue = filterStr(strValue, '0123456789');
	while(isCont){
		if(strValue.charAt(0) == '0'){
			strValue = strValue.substr(1,strValue.length);
		}
		else isCont = false;
	}
	for(var i=strValue.length-1; i>=0; i--){
		strTemp = strValue.charAt(i) + strTemp;		
		if ( (((i-strValue.length)%3)==0) && (i!=0)){
			strTemp= '.' + strTemp;
		}
	}	
	intValue.value = strTemp;
}
/*****/
/*****/
function closeUI() {
	$.unblockUI();
}
function vnc_deleteCookie(name) {
	vnc_setCookie(name, '',  315554400);
}
function addError(txt, msg) {
	var span = document.createElement("span");
	span.className = "errorMessage";
	span.innerHTML = msg;
	span.insertAfter($('#' + txt));
}
function onsubmitLogin() {
	var strEmail = $("#txtEmailLogin").val();
	var strPassword = $("#txtPasswordLogin").val();
	if( strEmail == '' || strPassword == '' )
	{
		return false;
	}
}
function showUIMessage(msg) {
	if (msg!='') {
		$.blockUI({message: msg, css: {background:'#fffbba', color:'#a90a0e', border:'1px solid #ffc407', showOverlay: false, width:'300px', top: ($(window).height()-200)/2 + 'px', left:($(window).width()-300)/2 + 'px', padding:'10px'}, overlayCSS: {opacity:'0'}});
		$('.blockOverlay').click($.unblockUI);
		setTimeout($.unblockUI, 2*20*60);
	}
}
function submitAction(formName, actionForm){
	var frm = document.forms[formName] ;
	frm.action	= actionForm;
	frm.submit();
}
function page(offset, url, div) {
	if(offset==undefined) offset=0;
	if(url==undefined) url='';
	if(div==undefined) div='';
	$.get(url+'offset='+offset,{},function(result){if(result){$('#'+div).html(result);}});
}
function paging(url, div) {
	/* if(offset==undefined) offset=0;
	if(url==undefined) url='';
	if(div==undefined) div=''; */
	$.get(url,{},function(result){if(result){$('#'+div).html(result);}});
}
validateCaptcha = function (pinput, pdomain) {
	var result = false;
	if (pinput.length == 5) {
		if (pdomain== undefined || pdomain == 'default') HOST = HOST_HAT;
		else HOST = 'http://' + pdomain + '.' + DOMAIN + '/';
		var strUrl = HOST + 'valid/isvalidcaptcha';
		$.ajax({
			cache	: false,
			async	: false,
			type	: 'POST',
			data	: 'input='+pinput,
			url		: strUrl,
			success	: function(response) {
				result = (response == '1' ? true : false);
			}
		});
	}
	return result;
}
validateComment = function (content, pdomain) {
	var result = false;
	if (pdomain== undefined || pdomain == 'default') HOST = HOST_HAT;
	else HOST = 'http://' + pdomain + '.' + DOMAIN + '/';
	var strUrl = HOST + 'valid/isvalidcomment';
	$.ajax({
		cache	: false,
		async	: false,
		type	: 'POST',
		data	: 'content='+content,
		url		: strUrl,
		success	: function(response) {
			result = (response == '1' ? true : false);
		}
	});	
	return result;
}
refreshCaptcha = function (pcontainer, pdomain, psession) {
	if (psession == undefined) psession = '';
	if (pdomain == undefined || pdomain == 'default') HOST = HOST_HAT;
	else HOST = 'http://' + pdomain + '.' + DOMAIN + '/';	
	var strUrl = HOST + 'ajax/refreshcaptcha';
	$.ajax({
		cache	: false,
		async	: false,
		type	: 'POST',
		data	: 'container='+pcontainer + '&session='+psession + '&domain='+pdomain,
		url		: strUrl,
		success	: function(response) {			
			$('#'+pcontainer).html(response);			
		}
	});
}

function InsertWord(word, idinput) {
	var word = word + ' '; 
	var field = document.getElementById(idinput);
	if (document.selection) {
		field.focus();
		var sel = document.selection.createRange();
		sel.text = word;
		sel.select();
	} else if (field.selectionStart || field.selectionStart == '0') {
		var start = field.selectionStart;
		var end = field.selectionEnd;
		var scroll = field.scrollTop;
		field.value = field.value.substring(0, start) + word + field.value.substring(end, field.value.length);
		field.focus();
		field.selectionStart = start + word.length;
		field.selectionEnd = start + word.length;
		field.scrollTop = scroll;
	} else {
	field.value += word;
	field.focus();
	}
}
showMsg = function(error, msg) {
	if (msg) {
		if (error==0) {		
			showUIMessage(msg);
		} else {
			alert(msg);
		}
	}
}
if (strMsg) showUIMessage(strMsg);
if (strErr) alert(strErr);
