function popupWin(url, popW, popH) {

	popW = (popW == null) ? 400 : popW;

	popH = (popH == null) ? 400 : popH;



	var left = (screen.width/2)-(popW/2);

	var top = (screen.height/2)-(popH/2);



	window.open(url, "_blank", "scrollbars=yes,menubar=no,status=no,resizable=no,toolbar=no,top="+top+",left="+left+",width="+popW+",height="+popH);

}



function setCookie(cName, cValue){

	var argv = setCookie.arguments;

	var argc = setCookie.arguments.length;

	var expires = (argc > 2) ? argv[2] : null;

	var path = (argc > 3) ? argv[3] : null;

	var domain = (argc > 4) ? argv[4] : null;

	var secure = (argc > 5) ? argv[5] : false;

	document.cookie = cName + "=" + escape (cValue) +

	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +

	((path == null) ? "" : ("; path=" + path)) +

	((domain == null) ? "" : ("; domain=" + domain)) +

	((secure == true) ? "; secure" : "");	

}



function getCookie(keyName){

	var nameEQ = keyName + "=";

	var ca = document.cookie.split (';');

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

	{

		var c = ca [i];

		while (c.charAt (0) == ' ') c = c.substring (1, c.length);

		if (c.indexOf (nameEQ) == 0) return c.substring (nameEQ.length, c.length);

	}

	return null;

}



function deleteCookie( name, path, domain ) {

	if (getCookie(name)){

		document.cookie = name + "=" +

		(( path) ? ";path=" + path : "") +

		(( domain) ? ";domain=" + domain : "" ) +

		";expires=Thu, 01-Jan-1970 00:00:01 GMT";

	}

}



function redirect(loc){

	document.location.replace(loc);

}



function sprintf(){

	if (!arguments || arguments.length < 1 || !RegExp){

		return;

	}

	var str = arguments[0];

	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;

	var a = b = [], numSubstitutions = 0, numMatches = 0;

	while (a = re.exec(str)){

		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];

		var pPrecision = a[5], pType = a[6], rightPart = a[7];

		

		numMatches++;

		if (pType == '%'){

			subst = '%';

		}

		else{

			numSubstitutions++;

			if (numSubstitutions >= arguments.length){

				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');

			}

			var param = arguments[numSubstitutions];

			var pad = '';

			if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);

			else if (pPad) pad = pPad;

			var justifyRight = true;

			if (pJustify && pJustify === "-") justifyRight = false;

			var minLength = -1;

			if (pMinLength) minLength = parseInt(pMinLength);

			var precision = -1;

			if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));

			var subst = param;

			if (pType == 'b') subst = parseInt(param).toString(2);

		  	else if (pType == 'c') subst = String.fromCharCode(parseInt(param));

		  	else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;

		  	else if (pType == 'u') subst = Math.abs(param);

		  	else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);

		  	else if (pType == 'o') subst = parseInt(param).toString(8);

		  	else if (pType == 's') subst = param;

		  	else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();

		  	else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();

		}

		str = leftpart + subst + rightPart;

	}

	return str;

}



function swap_search(value){

	$('search-box').style.display = value == 0 ? 'inline' : 'none';

	$('search-link').hreflang = value == 0 ? 1 : 0;

}



function getElementsByClassName(parent, cl, tag) {

	if(!tag){

		tag = "*";

	}

	var retnode = [];

	var myclass = new RegExp('\\b'+cl+'\\b');

	var elem = parent.getElementsByTagName(tag);

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

		var classes = elem[i].className;

		if (myclass.test(classes)) retnode.push(elem[i]);

	}

	return retnode;

};



function checkAll(form, ch) {

	getElementsByClassName(form, 'searchItem', 'input').each(function(el){el.checked = ch;selectclass(el);});

}



//$ = typeof($)!='undefined' ? $:function(id){return document.getElementById(id)};

$ = typeof($)!='undefined' ? $:function(id){return document.getElementById(id)};

_ = function(name){

	var res = Array();

	if(!document.all) {

		return document.getElementsByName(name);

	} else {

		// in ie document.getElementsByName just works for form elements. this is the fix up code

		var obj = document.getElementsByName(name);

		if (obj.length) return obj;

		var elementList = getAllChilds(document);

		var cnt = 0;

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

			if(elementList[i].name == name) res[cnt++] = elementList[i];

		}

	}

	return res;

}



function findObject(obj){ //find an object from id, name, or its classname 

	if(typeof(obj)=='object' && obj) return obj;

	var src = $(obj);

	if(typeof(src)=='object' && src) return src;

	src = _(obj)[0];

	if(typeof(src)=='object' && src) return src;

	var elementList = getAllChilds(document);

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

		if(elementList[i].className == obj) return elementList[i];

	}

	return null;

}





function setSub(source, destination, opts, cnt, def){	

	if(opts) {
		//var src = findObject(source);
		//var dest = findObject(destination);
		var src = document.getElementById(source);
		var dest = document.getElementById(destination);

		if(!src || !dest) {
			return;	
		}
		var pid = (src.selectedIndex >= 0) ? src.options[src.selectedIndex].value : 0;
		var cnt2= 0;
		while(dest.options.length && dest.options[cnt]) {
			if(dest.options.length==1){
				dest.options[cnt] = new Option('', '');
				break;
			} else  {
				dest.options[cnt] = null;	//remove all options
			}
		}
		if(opts[pid]) {
			while(opts[pid][cnt2]) {
				ar = opts[pid][cnt2++].split("*:-)");
				dest.options[cnt++] = new Option(ar[1], ar[0]);		//add new options
				//alert(ar[0]);
			}
			if(def){
				dest.value = def;
			}
		}
	}
}



function delComboSelected(combo){

	var elSel = document.getElementById(combo);

	var i;

	for (i = elSel.length - 1; i>=0; i--) {

		if (elSel.options[i].selected) {

		  elSel.remove(i);

		}

	}

}



function calctime(){

	var currenttime = new Date();

	var hours = currenttime.getHours();

	var minutes = currenttime.getMinutes();

	var seconds = currenttime.getSeconds();

	if(hours > 12 )hours=hours-12;

	if (hours == 0)hours = 12;

	if (hours < 10)hours = "0" + hours;

	if (minutes < 10)minutes = "0" + minutes;

	if (seconds < 10)seconds = "0" + seconds;

	var clocklocation = document.getElementById('digitalclock');

	clocklocation.innerHTML = hours + ":" + minutes + ":" + seconds;

	setTimeout("calctime()", 1000);

}



function cheknull(){

	var opt = document.getElementById('form1').frm_printitem;

	var i = 0;

	for (var intLoop = 0; intLoop < opt.length; intLoop++)

		if ((opt[intLoop].selected) || (opt[intLoop].checked)) i++;

	if (i == 0){		

		alert('خطا : هیچ رکوردی انتخاب نشده است');

		return false;

	}

	else return true;

}



function overclass(ttrr){

	if(ttrr.className=='listTd2') ttrr.className = 'over2';

	if(ttrr.className=='listTd1') ttrr.className = 'over1';

	if(ttrr.className=='select2') ttrr.className = 'sover2';

	if(ttrr.className=='select1') ttrr.className = 'sover1';

}

function lastclass(ttrr){

	if(ttrr.className=='over2') ttrr.className = 'listTd2';

	if(ttrr.className=='over1') ttrr.className = 'listTd1'

	if(ttrr.className=='sover2') ttrr.className = 'select2';

	if(ttrr.className=='sover1') ttrr.className = 'select1'

}

function selectclass(cc){

	ttrr = cc.parentNode.parentNode;

	if(cc.checked)	if(ttrr.className=='over2'||ttrr.className=='select2'||ttrr.className=='listTd2') ttrr.className = 'select2'; else ttrr.className = 'select1';

	else 			if(ttrr.className=='sover2'||ttrr.className=='select2'||ttrr.className=='listTd2')ttrr.className = 'listTd2'; else ttrr.className = 'listTd1';

}



function DisplayArchive(){

	var n = document.getElementById('archive').style.display;

	document.getElementById('archive').style.display = (n=='none') ? 'inline' : 'none';	

	document.getElementById('btnarchive').src = (n=='none') ? '../images/btn2.png' : '../images/btn.png'; 

}


