/*
	IO Media
*/

function disableHandleForm(frm, flgDisable)
{
	for (var n=0; n < frm.elements.length; n++) 
		frm.elements[n].disabled = flgDisable;
}

function getStringId(frm, checkbox_name)
{
	temp = '';
	var max = frm.elements.length;
	
	for ( i = 0; i < max; i++ )
	{
		if ( (frm.elements[i].type == "checkbox") && (frm.elements[i].checked == true) && (frm.elements[i].name == checkbox_name) )
		{
			temp = temp + frm.elements[i].value + ",";
		}
	}
	
	temp = temp.substring(0,temp.length - 1);
	return temp;
}
function checkChildren(frm, check_child_name, check_parent_name)
{
	var max = frm.elements.length;
	var checkStatus = true;
	for ( i=0; i< max; i++ )
	{
		if ( (frm.elements[i].type == 'checkbox') && frm.elements[i].name == check_child_name && frm.elements[i].checked == false )
		{
			checkStatus = false;
			break;	
		}
	}
	//frm.chk_all.checked = checkStatus;
	$(check_parent_name).checked = checkStatus;
}
function checkAll(checked_parent, frm, check_child_name)
{
	max=frm.elements.length;
	for(i=0;i<max;i++)
	{
		if(frm.elements[i].type=="checkbox" && frm.elements[i].name == check_child_name)
		{	
			frm.elements[i].checked=checked_parent;
		}
	}
}

/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

function checkEmail(str)
{
  var re=/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,4}$/
  if (str.match(re))
  {
  	if(str.indexOf('@')>32) return false;
	return true;
  }
  return false;
}
function trim(val)
{
	return val.replace(/^\s+|\s+$/g,"");
}

var emptyFunction = function(){};

function generateCatchException(object, func)
{
	on_func_error = ".On" + func.substring(0,1).toUpperCase() + func.substring(1) + "Error";

	command =  object + on_func_error + " = function(error) {";
	command += "alert('Error: \\n\\n' + error.message);";
	command += "return true;}\n";

	return command;
}

function callAJAX(object, func, args, callback)
{
	if (args != null && args['id'] != null) {
		ajax_running_indicator =  "<div style='text-align: center; width: 100%; margin-top: 0px;'><img src='";
		ajax_running_indicator += ajax_icon;
		ajax_running_indicator += "'></div>";
		$(args['id']).innerHTML = ajax_running_indicator;
	}

	callback = (callback == null) ? emptyFunction : callback;

	command =  generateCatchException(object,func);
	if (args != null) {
		command += object + "." + func + "(args, callback);";
	} else {
		command += object + "." + func + "(callback);";
	}

	eval(command);
}

function callAJAX_noloading(object, func, args, callback)
{
	callback = (callback == null) ? emptyFunction : callback;

	command =  generateCatchException(object,func);
	if (args != null) {
		command += object + "." + func + "(args, callback);";
	} else {
		command += object + "." + func + "(callback);";
	}

	eval(command);
}

function quickform_ajax_submit(object, func, form)
{
	command =  generateCatchException(object,func);
	command += object + "." + func + "(xoad.html.exportForm('" + form.id + "'), function(result) {";
	command += "alert(result);";
	command += "});";

	eval(command);
}

function paging_ajax_call(object,func,page)
{
	command =  generateCatchException(object,func);
	command += object + "." + func + "(page, function(result) {";
	command += "alert(result);";
	command += "});";

	eval(command);
	return false;
}

function isValidDateTime(datetimeStr)
{
	if (datetimeStr.indexOf(" ")<10){  // if year with 4 number
	dateStr = datetimeStr.substr(0, 6);
	dateStr+= '20'+datetimeStr.substr(6, 2);
	timeStr = datetimeStr.substr(9);} else { // if year with 2 number
	dateStr = datetimeStr.substr(0, 10);
	timeStr = datetimeStr.substr(11);
	}
	return isValidDate(dateStr) && isValidTime(timeStr);
}

function isValidDate(dateStr, format)
{
   dateArr = dateStr.split("/");
   if (isNaN(dateArr[2]) == true){return false;}
   else{
	   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") { // If the year is first
	      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") { // If the year is second
	      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
	      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
	   } else { // The year must be third
	      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
	      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	   }
	   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
	   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
	   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
	   // Check to see if the 3 parts end up making a valid date
	   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 isValidTime(timeStr){
	len = timeStr.length;
	i=0;count=0;
	if (len != 0 && len <= 8){
		temp = new Array();
		temp = timeStr.split(':');
		if (temp.length != 3){return false;}
		else{
			for(i = 0;i < temp.length-1;i++){
				if (isNaN(temp[i]) == true){return false;}
			}
		}
		if ((temp[0] >= 0 && temp[0] <= 23) && (temp[1] >= 0 && temp[1] <= 60) && (temp[2] >= 0 && temp[2] <= 60)){
			return true
		}else{return false;}
	}else{return false;}
}

function switchPanel(panels, selectedPanel)
{
	panels.each(function(item) {
		display = (item == selectedPanel) ? '' : 'none';
		if( $(item) != null )
		{
			$(item).style.display = display;
		}
	});
}

//function htmlspecialchars(str)
//{
//	str = str.replace(/&/g,'&amp;');
//	str = str.replace(/</g,'&lt;');
//	str = str.replace(/>/g,'&gt;');
//	str = str.replace(/"/g,'&quot;');
//	return str.replace(/'/g,'&#039;');
//}


function addslashesHTML(str)
{
	str = str.replace(/&#39;/g, '&#92;\'');
	return str;
}

function escape(str)
{
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	str = str.replace(/\"/g, '&quot;');
	str = str.replace(/\'/g, '&#39;');
	return str;
}

function is_int(variable) { 
	return(parseInt(variable) == variable); 
} 

function addToolTipToImages(){
	$$("img").each( function(link) {
			new Tooltip(link, {backgroundColor: "#659EC7", borderColor: "#FDEEF4", textColor: "white"});			
		});			
}

function showLoading(disable_form_id)
{
    if (disable_form_id != undefined)
    {
        disableHandleForm(disable_form_id,true);
    }
    $('loading_box_message').style.display = "";
    setElementCenterScreen('loading_box_message');
}

function endLoading(disable_form_id)
{
    if (disable_form_id != undefined)
    {
        disableHandleForm(disable_form_id,false);
    }
    $('loading_box_message').style.display = "none";
}
function getWindowHeight()
{
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') 
    {
    	windowHeight = window.innerHeight;
    }
    else 
    {
    	if (document.documentElement && document.documentElement.clientHeight) 
    	{
    		windowHeight = document.documentElement.clientHeight;
    	}
    	else 
    	{
    		if (document.body && document.body.clientHeight) 
    		{
    			windowHeight = document.body.clientHeight;
    		}
    	}
    }
    return windowHeight;
}

function getWindowWidth() 
{
    var windowWidth = 0;
    if (typeof(window.innerWidth) == 'number') 
    {
    	windowWidth = window.innerWidth;
    }
    else 
    {
    	if (document.documentElement && document.documentElement.clientWidth) 
    	{
    		windowWidth = document.documentElement.clientWidth;
    	}
    	else {
    		if (document.body && document.body.clientWidth) 
    		{
    			windowWidth = document.body.clientWidth;
    		}
    	}
    }
    return windowWidth;
}

///////////////////////////////////////////////////////////


//////////////////////////////

function setElementPositionScreen(element_id) 
{
	var ele = document.getElementById(element_id);
   	
	
	
	if (document.getElementById) 
	{
		var contentElement = document.getElementById(element_id);
		contentElement.style.top = 500 + 'px';
		contentElement.style.left = 500 + 'px';
	}
}


function setElementCenterScreen(element_id) 
{
	
	var ScrollTop = document.body.scrollTop;
 
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	
	boxHeight = document.getElementById(element_id).clientHeight;
	
	if (document.getElementById) 
	{
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) 
		{
			var contentElement = document.getElementById(element_id);
			var contentHeight = contentElement.offsetHeight;
			if (windowHeight - contentHeight > 0) 
			{
				//contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
				var top = ScrollTop + ((contentHeight/2) - (boxHeight)) + 160 + 'px';
				//contentElement.style.top = top;
			}
			else 
			{
				contentElement.style.position = 'static';
			}
		}
		
		var windowWidth = getWindowWidth();
		if (windowWidth > 0)
		{
			var contentElement = document.getElementById(element_id);
			var contentWidth = contentElement.offsetWidth;
			if (windowWidth - contentWidth > 0) 
			{
				contentElement.style.left = ((windowWidth / 2) - (contentWidth / 2)) + 'px';
			}
			else 
			{
				contentElement.style.position = 'static';
			}
		}
	}
	
}

function msgBox(message)
{
    $('msgbox').style.display = "";    
    $('msgbox_content').innerHTML = message;
	setElementCenterScreen('msgbox');
}

function closeMsgBox()
{
    $('msgbox').style.display = "none";
}
function setZeroAlert()
{
	$('cnt_alert').innerHTML = 0;
	callAJAX("xoad_handler_reload","inactiveReadNotification",$H({}));
	
	if ( document.getElementById('id_notice_common').className == 'ctn_photo' )
	{
		document.getElementById('id_notice_common').className = 'ctn_photo click';
		document.getElementById('id_notice_list').className = 'cnt_t click';
	}
	else
	{
		document.getElementById('id_notice_common').className = 'ctn_photo';
		document.getElementById('id_notice_list').className = 'cnt_t';
	}
}
function inactiveANotification(noti_id)
{
	callAJAX("xoad_handler_reload","inactiveANotification",$H({noti_id:noti_id}));
}
var g_intPageWishGiftOfFriend = 1;
function onPageWishListAll(p_intPage, p_attach)
{
	g_intPageWishGiftOfFriend = p_intPage;
	listAllGiftWishOfFriends();
}
function listAllGiftWishOfFriends()
{	
	callAJAX("xoad_handler","listAllGiftWish",$H({page: g_intPageWishGiftOfFriend}),listAllGiftWishOfFriends_callback);
	
}

function listAllGiftWishOfFriends_callback(result)
{	
	msgBox(result);
}

