/**
 * @package: Core
 * @subpackage: Common functions
 * @version: v.1.3.2, $Id: 2009-12-29 14:23:23
 * @author: Mezahir Efendiyev <cengmezo@sirajans.com>
 * @see: http://www.sirajans.com/kiwi
 */

var kiwi_browserIsIE = document.all? true:false;

/**
 * Get element object
 *
 * @param string id
 * @param object winObj
 * @return object
 */

function j_getElement (id , winObj) {
	if ( winObj ) {
		return winObj.document.getElementById(id);
	} else {
		return document.getElementById(id);
	}
}

/**
 * Open browser window
 *
 * @param string linkURL
 * @param string windowID
 * @param integer winWidth
 * @param integer winHeigth
 * @param string winParams
 */

function j_openNewWindow ( linkURL , windowID, winWidth , winHeigth, winParams ) {

	if ( ! winParams ) {
		winParams = 'menubar=0,addressbar=0,toolbar=0,statusbar=0,resizable=1,scrollbars=yes,location=0,DisableSysMenu=0,frameborder=0';
	}

	if ( ! windowID ) {
		windowID = parseInt(Math.random()*1000*1000);
	}

	var screenSizes = j_screenSizes();
	var temp;

	if ( ! winWidth ) {
		winWidth = 0;
	}

	if ( ! winHeigth ) {
		winHeigth = 0;
	}

	// Width
	if ( winWidth <= 0 ) {
		winWidth += screenSizes[0];
	}

	if ( ( winWidth > screenSizes[0] ) || ( winWidth < 1 ) ) {
		winWidth = screenSizes[0];
	}

	temp = parseInt ( ( screenSizes[0] - winWidth ) / 2 );
	winParams += ',width='+winWidth+',left='+temp;

	// Height
	if ( winHeigth <= 0 ) {
		winHeigth += screenSizes[1];
	}

	if ( ( winHeigth > screenSizes[1] ) || ( winHeigth < 1 ) )  {
		winHeigth = screenSizes[1];
	}

	temp = parseInt ( ( screenSizes[1] - winHeigth ) / 2 );
	winParams += ',height='+winHeigth+',top='+temp;

	var newWin = window.open( linkURL , 'kiwiWindow_'+windowID , winParams );
	return newWin;
}

/**
 * Get Screen Dimensions
 *
 * @return array
 */

function j_screenSizes () {

	var screenW = 640, screenH = 480;

	if (parseInt(navigator.appVersion)>3) {
		screenW = screen.width;
		screenH = screen.height;
	} else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled() ) {
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}

	return [screenW , screenH];
}

/**
 * Get window sizes
 *
 * @return array
 */

function j_windowSizes () {

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return [ myWidth, myHeight ];
}

/**
 * Get tag dimensions
 *
 * @param string tagID
 * @return array
 */

function j_objectSizes ( tagID ) {

	var tagWidth = 0 , tagHeight = 0;
	obj = j_getElement (tagID);

	if ( obj ) {
		tagWidth  = obj.offsetWidth;
		tagHeight = obj.offsetHeight;
	}

	return [ tagWidth , tagHeight ];
}

/**
 * Set cookie
 *
 * @param string c_name
 * @param mixed value
 * @param integer expirehours
 */

function j_setCookie(c_name,value,expirehours) {
	c_name = "__kiwicms__" + c_name;
	var exdate=new Date();
	exdate.setHours(exdate.getHours()+expirehours);
	document.cookie=c_name+ "=" +escape(value)+((expirehours==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
 * Get cookie
 *
 * @param string c_name
 * @return mixed
 */

function j_getCookie(c_name) {
 	c_name = "__kiwicms__" + c_name;
 	if (document.cookie.length>0) {
 		c_start=document.cookie.indexOf(c_name + "=");
 		if (c_start!=-1) {
 			c_start=c_start + c_name.length+1;
 			c_end=document.cookie.indexOf(";",c_start);
 			if (c_end==-1) c_end=document.cookie.length;
 			return unescape(document.cookie.substring(c_start,c_end));
 		}
 	}
 	return "";
}

/**
 * Change Image Source
 *
 * @param string imageID
 * @param string imageSRC
 * @param integer fadeDuration
 */

function j_changeImageSource ( imageID , imageSRC , fadeDuration ) {
 	var obj = j_getElement( imageID );
 	if (!obj) {
 		return false;
 	}
 	if ( kiwi_browserIsIE ) {
		obj.style.filter="blendTrans(duration="+fadeDuration+")";
		obj.style.filter="blendTrans(duration=crossFadeDuration)";
		obj.filters.blendTrans.Apply();
		obj.filters.blendTrans.Play();
		obj.src = imageSRC;
 	} else {
		j_changeImageSource_opChange (imageID, 99, imageSRC, 1);
	}
}

/**
 * Change Image Source : Slide Effects with opacity change
 *
 * @param string objID
 * @param integer opacityValue
 * @param string imageSRC
 * @param bolean dW
 */

var kiwi_changeImageSource_timeoutFunct = false;
function j_changeImageSource_opChange ( objID , opacityValue , imageSRC, dW) {
 	obj = j_getElement( objID );
 	if (opacityValue>0) {
 		obj.style.opacity = (opacityValue / 100);
		obj.style.MozOpacity = (opacityValue / 100);
		obj.style.KhtmlOpacity = (opacityValue / 100);
		obj.style.filter = "alpha(opacity=" + opacityValue + ")";
	}
	if (kiwi_changeImageSource_timeoutFunct) {
		clearTimeout (kiwi_changeImageSource_timeoutFunct);
	}
	if (dW && (opacityValue>0)) {
		kiwi_changeImageSource_timeoutFunct = setTimeout("j_changeImageSource_opChange('"+objID+"', "+(opacityValue-5)+", '"+imageSRC+"', 1)", 3);
	} else {
		if (dW) {
			obj.src = imageSRC;
		}
		kiwi_changeImageSource_timeoutFunct = setTimeout("j_changeImageSource_opChange('"+objID+"', "+(opacityValue+5)+", '"+imageSRC+"', 0)", 3);
	}
}

/**
 * Check/Uncheck all boxes
 *
 * @param string formName
 * @param string checkBoxeName
 * @param object checkObj
 */

function j_checkAllBoxes ( formName , checkBoxeName , checkObj ) {

	var currentAssing = checkObj.checked;

	var elts      = (checkBoxeName != '')
				  ? document.forms[formName].elements[checkBoxeName + '[]']
				  : document.forms[formName].elements;
	var elts_cnt  = (typeof(elts.length) != 'undefined')
				  ? elts.length
				  : 0;

	if (elts_cnt) {
		for (var i = 0; i < elts_cnt; i++) {
			if (!elts[i].disabled) {
				elts[i].checked = currentAssing;
			}
		} // end for
	} else {
		if (!elts.disabled) {
			elts.checked = currentAssing;
		}
	} // end if... else

	return true;
}

/**
 * Reload captcha
 *
 * @param object objID
 */

function j_reload_captcha(objID) {
	var obj = j_getElement(objID);
	obj.src = obj.src + (new Date()).getTime();
}

/**
 * Replace all
 *
 * @param string orgString
 * @param string patternFind
 * @param string patternReplace
 * @return object
 */

function j_replaceAll ( orgString, patternFind, patternReplace ) {
	var patternFoundFlag = orgString.indexOf( patternFind );
 	while (patternFoundFlag != -1){
 		orgString = orgString.replace( patternFind, patternReplace );
 		patternFoundFlag = orgString.indexOf( patternFind );
 	}
 	return orgString;
}
