/** 
 * @fileoverview Common library.
 *
 * @author manorot tangsaveephan
 * @version 1.0.2 Mar 2007
 */

var IE=(navigator.appName.indexOf('Microsoft') != -1);
var newWindow;
var category_array_id = new Array();

// ========= UTIL : start ==========
/**
   * Open new window if already open then focus it
   *
   * @param {String} url       url to open
   * @param {String} winname       window name
   * @param {String} features       window features
   * @return none
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   */
function makeNewWindow(url, winname, features) {
	if (!newWindow || newWindow.closed)
	{
		newWindow = window.open(url, winname, features);
	} else {
		newWindow.focus();
	}
}
// ========= UTIL : end ==========

// ========= FROM : start ==========

/**
   * Limit user in put in textarea
   *
   * @param {Obj} what         element which want to limit user input length
   * @param {Integer} limit         total limit charater 
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   */
function enforcechar(what,limit){
	if (what.value.length>=limit)
	return false
}

/**
   * Confirm delete data.
   *
   * @param {String} form		form object
   * @return      dialog box to confirm user.
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   */
function confirmDel(form) {
	if (confirm('Â×¹ÂÑ¹¡ÒÃÅº¢éÍÁÙÅ·ÕèàÅ×Í¡äÇé ?'))
	{
		document.form.submit();
	}
}

/**
   * Toggle All Check box in data list
   *
   * @param the_form         form element
   * @param the_element         checkbox element 
   * @param highlight_row		flag for highlight row
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 May 2006
   * @requires #markAllRows
   * @requires #unMarkAllRows
   */
function checkAll(the_form,the_element, highlight_row) {
	if (highlight_row == null) { highlight_row = true;}
	var elts = document.forms[the_form].elements[the_element]

    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
	if(document.forms[the_form].checkboxall.checked == true) {
		/*
		 if (elts_cnt) {
			 for (var i = 0; i < elts_cnt; i++) {
				elts[i].checked = true;
			 } // end for
		 } else {
			elts.checked        = true;
		 } // end if... else
		 */
		markAllRows('content_list', highlight_row);
	} else {
		/*
		if (elts_cnt) {
			for (var i = 0; i < elts_cnt; i++) {
				elts[i].checked = false;
			} // end for
		 } else {
			elts.checked        = false;
		} 
		*/
		unMarkAllRows('content_list', highlight_row);
	}
}
// ========= FROM : end ==========

// ========= MENU : start ==========
/**
   * toggle menu show/hide
   *
   * @param {integer} menu_group_id         id for group menu
   * @param {text} menu_group_img		image for group menu to display
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #initMenuCategory
   */
function toggleMenu(menu_group_id, menu_group_img) {
	Effect.toggle(menu_group_id,'slide', {duration: 0.30});
	imgElem = $(menu_group_img);
	imgName = imgElem.src.substring(imgElem.src.lastIndexOf('/')+1, imgElem.src.length);
	if (imgName == 'icon_minus.gif') {
		imgElem.src = 'images/icon_plus.gif';
		setCookie(menu_group_id, "none,images/icon_plus.gif");
	} else if (imgName == 'icon_plus.gif') {
		imgElem.src = 'images/icon_minus.gif';
		setCookie(menu_group_id, "block,images/icon_minus.gif");
	}
}

/**
   * Inititialize menu
   *
   * @param {array} category_array_id         category id array
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #toggleMenu
   */
function initMenuCategory(category_array_id) {
	var my_category;
	for (i=0;i<category_array_id.length;i++ )
	{
		my_category = getCookie("category_" + category_array_id[i]);
		if (my_category != null)
		{
			var my_category_array = my_category.split( ",");
			category_style = my_category_array[0];
			category_control = my_category_array[1];

			if (category_control == undefined)
			{
				category_control = "images/icon_plus.gif";
				category_style = "none";
			}

			target = document.getElementById("category_" + category_array_id[i]);
			
			try
			{
				if (target)
				{
					// alert("style current = " + target.style.display);
					// alert("id = " + category_array_id[i] + ", style = " + category_style + " , control = " + category_control);
					target.style.display = category_style;
					document.images["control_" + category_array_id[i]].src = category_control;
				}
			}
			catch (e) { }
		}
	}
}
// ========= MENU : end ==========

// ========= TOOLTIP : start ==========
/**
   * Show tooltip for menu
   *
   * @param current         current object
   * @param e         event
   * @param text         tooltip text
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #hidetip
   */
function showtip(current,e,text){
	if (document.all||document.getElementById){
		thetitle=text.split('<br>')
		if (thetitle.length>1){
			thetitles=''
			for (i=0;i<thetitle.length;i++)
				thetitles+=thetitle[i]
				current.title=thetitles
		}
		else
			current.title=text
		}
	else if (document.layers){
		document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>')
		document.tooltip.document.close()
		document.tooltip.left=e.pageX+5
		document.tooltip.top=e.pageY+5
		document.tooltip.visibility="show"
	}
}

/**
   * Hide tooltip for menu
   *
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #showtip
   */
function hidetip(){
	if (document.layers)
		document.tooltip.visibility="hidden"
}
// ========= TOOLTIP : end ==========

// ========= COOKIE : start ==========
/**
   * utility function to retrieve a future expiration date in proper format;
   * pass three integer parameters for the number of days, hours,
   * and minutes from now you want the cookie to expire; all three
   * parameters required, so use zeros where appropriate
   *
   * @param days		number of days
   * @param hours		number of hours
   * @param minutes		number of minutes
   * @return expire date
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   * @deprecated
   */
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof minutes == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

/**
   * utility function called by getCookie()
   *
   * @param offset		offset value
   * @return cookie value
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   * @deprecated
   */
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

/**
   * Primary function to retrieve cookie by name
   *
   * @param name       cookie name
   * @return null
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   * @deprecated
   */
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

/**
   * store cookie value with optional details as needed
   *
   * @param name		cookie name
   * @param value		cookie value
   * @param expires		cookie expire date
   * @param path		cookie path
   * @param domain		cookie domain
   * @param secure		cookie secure
   * @return none
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   * @deprecated
   */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
   * Remove the cookie by setting ancient expiration date
   *
   * @param name       cookie name
   * @param path       cookie path
   * @param domain       cookie domain
   * @return none
   * @author	manorot tangsaveephan
   * @version 1.0.0 Mar 2007
   * @deprecated
   */
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
// ========= COOKIE : end ==========

// ========= MISC : start ==========
/**
   * Redirect url from drop down list
   *
   * @param myObj		drop down list object
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #redirect
   */
function gone(myObj)
{
	location=myObj.options[myObj.selectedIndex].value
}

/**
   * Redirect url
   *
   * @param url		url to redirect
   * @return      none
   * @author		manorot tangsaveephan
   * @version	1.0.0 Mar 2007
   * @see #gone
   */
function redirect(url)
{
	location.replace(url);
}
// ========= MISC : end ==========

/**
 * mark or unmark all checkbox in the given container
 * if mark one or more , will enabled mark button
 *
 * @param    container_id    DOM element cantain checkbox
 * @return none
 * @author	manorot tangsaveephan
 * @version 1.0.0 Mar 2007
 * @requires #checkTotalCheck
 */
function toggleMarkRows( container_id ) {
    var rows = document.getElementById(container_id);
    var checkbox;
	var total_checked;

	checkbox = rows.getElementsByTagName( 'input' )[0];

	if ( checkbox && checkbox.type == 'checkbox' ) {
		if (checkbox.checked == false)
		{
			rows.style.backgroundColor = '';
		} else {
			rows.style.backgroundColor = '#F4E7EA';
		}
	}

	if (checkTotalCheck('content_list') > 0)
	{
		document.getElementById('batchdel_button').disabled = false;
	} else {
		document.getElementById('batchdel_button').disabled = true;
	}
}

/**
 * check total checkbox that is checked in the cantainer
 * 
 * @param    container_id    DOM element cantain checkbox
 * @return total checked
 * @author	manorot tangsaveephan
 * @version 1.0.0 Mar 2007
 */
function checkTotalCheck(container_id) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var checkbox;
	var total_checked = 0;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            if (checkbox.checked == true)
            {
				total_checked++;
            }
        }
    }
    return total_checked;	
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container_id    DOM element
 * @param highlight		highlight flag
 * @return none
 * @author	manorot tangsaveephan
 * @version 1.0.0 Mar 2007
 * @see #unMarkAllRows
 */
function markAllRows( container_id , highlight) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var checkbox;

    for ( var i = 1; i < rows.length; i++ ) {
		
        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
			if ((checkbox.name == 'id[]') && (checkbox.disabled == false))
			{
				checkbox.checked = true;
				if (highlight)
				{
					rows[i].style.backgroundColor = '#F4E7EA';
				}
			}
        }
    }

	document.getElementById('batchdel_button').disabled = false;
    return true;
}

/**
 * unmarks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container_id    DOM element
 * @param highlight		highlight flag
 * @return none
 * @author	manorot tangsaveephan
 * @version 1.0.0 Mar 2007
 * @see #markAllRows
 */
function unMarkAllRows( container_id , highlight) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            checkbox.checked = false;
			if (highlight)
			{
				rows[i].style.backgroundColor = '';
			}
        }
    }
	if (document.getElementById('batchdel_button') != null)
	{
		document.getElementById('batchdel_button').disabled = true;
	}
    return true;
}