/**
 * @package: Core
 * @subpackage: Common functions
 * @version: v.1.4.2, $Id: 2011-05-30 20:53:46
 * @author: Mezahir Efendiyev <admin@cengmezo.com>
 * @see: http://www.froqqie.com
 */

var froq_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 = j_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 = j_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 = j_parseInt ( ( screenSizes[1] - winHeigth ) / 2 );
	winParams += ',height='+winHeigth+',top='+temp;
	var newWin = window.open( linkURL , 'froqWindow_'+windowID , winParams );
	return newWin;
}

/**
 * Parse to integer
 *
 * @param mixed v
 * @return integer
 */

function j_parseInt (v) {
	v = parseInt(v);
	if (isNaN (v)) {
		v = 0;
	}
	return v;
}

/**
 * Get Screen Dimensions
 *
 * @return array
 */

function j_screenSizes () {

	var screenW = 640, screenH = 480;

	if (j_parseInt(navigator.appVersion)>3) {
		screenW = screen.width;
		screenH = screen.height;
	} else if (navigator.appName == "Netscape" && j_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 ];
}


/**
 * Get Scroll X-Y Values
 *
 * @return array
 */

function j_position_scrollXY() {
 	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}

	return [ scrOfX, scrOfY ];
}

/**
 * Get element position
 * @param object obj
 * @return array
 */

function j_position_element(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

/**
 * Set cookie
 *
 * @param string c_name
 * @param mixed value
 * @param integer expirehours
 */

function j_setCookie(c_name,value,expirehours) {
	c_name = "__froqqie__" + 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 = "__froqqie__" + 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 ( froq_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 froq_changeImageSource_timeoutFunct = false;
function j_changeImageSource_opChange ( objID , opacityValue , imageSRC, dW) {
 	var 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 (froq_changeImageSource_timeoutFunct) {
		clearTimeout (froq_changeImageSource_timeoutFunct);
	}
	if (dW && (opacityValue>0)) {
		froq_changeImageSource_timeoutFunct = setTimeout("j_changeImageSource_opChange('"+objID+"', "+(opacityValue-5)+", '"+imageSRC+"', 1)", 3);
	} else {
		if (dW) {
			obj.src = imageSRC;
		}
		froq_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
 */

var v_last_captcha_tag = false;
var v_last_captcha_img = new Image();

function j_reload_captcha(objID) {

	j_showLoadingDiv();
	v_last_captcha_tag = objID;
	var obj = j_getElement(objID);

	v_last_captcha_img = new Image();
	v_last_captcha_img.src = obj.src + (new Date()).getTime();
	v_last_captcha_img.onload = j_reload_captcha_loaded;
}

function j_reload_captcha_loaded() {
	var obj = j_getElement(v_last_captcha_tag);
	obj.src = v_last_captcha_img.src;
	j_showLoadingDiv(true);
}

/**
 * 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;
}

/**
 * Centerize div
 *
 * @param string tagID
 * @param boolean accToScreensize
 */

function j_adjustDiv_centerize (tagID, accToScreensize) {

	var divSizes = j_objectSizes(tagID);
	var marginSizes = accToScreensize ? j_screenSizes() : j_windowSizes();

	var l = 0;
	var t = 0;

	l = j_parseInt((marginSizes[0] - divSizes[0])/2);
	t = j_parseInt((marginSizes[1] - divSizes[1])/2);

	if (l<1) {
		l = 1;
	}

	if (t<1) {
		t = 1;
	}

	var scrollValues = j_position_scrollXY();
 	t += scrollValues[1];

	obj = j_getElement (tagID);
	if (obj) {
		obj.style.left=l+"px";
		obj.style.top=t+"px";
	}
}

/**
 * Show/Hide loading div
 *
 * @param boolean isHide
 */

function j_showLoadingDiv (isHide) {
	var obj = j_getElement('froqTag_loadingDiv');
	if (obj) {
		if (isHide) {
			obj.style.display='none';
		} else {
			obj.style.display='block';
			j_adjustDiv_centerize('froqTag_loadingDiv');
		}
	}
}

/**
 * Small loading icon
 *
 * @param boolean isHide
 */

function j_showLoadingIcon (isHide, pLeft, pRight) {
	var obj = j_getElement('froqTag_loadingIcon');
	if (obj) {
		if (isHide) {
			obj.style.display='none';
		} else {
			obj.style.left=pLeft+"px";
			obj.style.top=pRight+"px";
			obj.style.display='block';
		}
	}
}

/**
 * Ajax link loader
 *
 * @param string cLink
 * @param string cDivID
 */

var v_content_loader_lastObj = '';
var v_content_loader_lastLink = '';
var v_content_loader_lastDiv = '';
var v_content_loader_lastFunc = '';

function j_content_loader (cObj, cLink, cDivID, cFunc) {

	// Check prev requests
	if (v_content_loader_lastDiv!='') {
		alert('Please wait...');
		return false;
	}

	// Show progress info
	if (cObj) {
		var p = j_position_element(cObj);
		j_showLoadingIcon(false, p[0], p[1]);
	} else {
		j_showLoadingDiv();
	}

	// Set current request params
	if (cFunc == undefined) {
		cFunc = '';
	}

	v_content_loader_lastObj = cObj;
	v_content_loader_lastLink = cLink;
	v_content_loader_lastDiv = cDivID;
	v_content_loader_lastFunc = cFunc;

	// code for IE7+, Firefox, Chrome, Opera, Safari
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();

	// Code for IE6, IE5
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	// Respond
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			if (cObj) {
				j_showLoadingIcon(true);
			} else {
				j_showLoadingDiv(true);
			}

			if (v_content_loader_lastFunc!='') {
				eval (v_content_loader_lastFunc+"(xmlhttp.responseText, v_content_loader_lastObj, v_content_loader_lastLink, v_content_loader_lastDiv);");
			} else {
				j_getElement(v_content_loader_lastDiv).innerHTML=xmlhttp.responseText;
			}

			v_content_loader_lastDiv = '';
			v_content_loader_lastFunc = '';
		}
	}

	// Request link
	xmlhttp.open("GET",cLink,true);
	xmlhttp.send();
}

/**
 * Form functions : Get checked radio box value
 *
 * @param string tagID
 * @return string
 */

function j_form_getValue_radio(obj) {
	if (!obj) {
		return "";
	}
	var l = obj.length;
	if (l==undefined) {
		if (obj.checked) {
			return obj.value;
		} else {
			return "";
		}
	} else {
		for(var i = 0; i < l; i++) {
			if(obj[i].checked) {
				return obj[i].value;
			}
		}
	}
	return "";
}
