function gebi(idname)
{
		return document.getElementById(idname);
}
function setid(id, inner)
{
		gebi(id).innerHTML = inner;
}
function getid(id)
{
		return gebi(id).innerHTML;
}
function StringIndexOf_persian(str,value,start) {
    var def=str.indexOf(value,start)
    var min;
	switch(value){
	  case 'ا':
        var a1=str.indexOf('آ',start)
        var a2=str.indexOf('أ',start)
        var a3=str.indexOf('ء',start)
        min=Math.min((def<0?Infinity:def),(a1<0?Infinity:a1),(a2<0?Infinity:a2),(a3<0?Infinity:a3))
        break;
	  case 'و':
        var a1=str.indexOf('ؤ',start)
        min= Math.min((def<0?Infinity:def),(a1<0?Infinity:a1))
        break;
	  case 'ي':
        var a1=str.indexOf('ئ',start)
        min= Math.min((def<0?Infinity:def),(a1<0?Infinity:a1))
        break;
	  default:
        return def;
	}
    return (min==Infinity?-1:min);
}
function ArrayToString2(arr){
    var ctr = '';
	for (var i = 0; i < arr.length; i++) {
		ctr += (ctr == "" ? "'" : ",'") + arr[i] + "'"
	}
	return ctr;
}
function ArrayIndexOf(arr,value) {
  	var ctr = -1;
  	for (var i = 0; i < arr.length; i++) {
  		// use === to check for Matches. ie., identical (===), ;
  		if (arr[i] == value) {
  			return i;
  		}
  	}
  	return ctr;
}
function frameWindow(iframeId) {
	f = document.getElementById(iframeId);
	try {
		return f.contentWindow
	} catch (exp) {
		return f;
	}
}

function openurl(url) {
		popupWin = window.open(url,
				'open_window',
				'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent,fullscreen=yes , left=0, top=0')
		popupWin.focus()
}

function inpvalidate(e, acceptchars, be)
{
		var key;
		var keychar;
		if (window.event)
				key = window.event.keyCode;
		else if (e)
				key = e.which;
		else
				return true;
		keychar = String.fromCharCode(key);
		if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (key==46) || (key>=37 && key<=40))
				return true;
		else if ((acceptchars.indexOf(keychar) > -1))
				return be;
		else
				return !be;
}
function isenter(e)
{
		var key;
		var keychar;
		if (window.event)
				key = window.event.keyCode;
		else if (e)
				key = e.which;
		else
				return false;
		if (key==13)
				return true;
		return false;
}
function trim (str)
{
		var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
		for (var i = 0; i < str.length; i++)
		{
				if (whitespace.indexOf(str.charAt(i)) === -1)
				{
						str = str.substring(i);
						break;
				}
		}
		for (i = str.length - 1; i >= 0; i--)
		{
				if (whitespace.indexOf(str.charAt(i)) === -1)
				{
						str = str.substring(0, i + 1);
						break;
				}
		}
		return whitespace.indexOf(str.charAt(0)) === -1? str: '';
}
function adadFarsi(adad)
{
		var ret = "";
		for(i = 0; i<adad.length; i++)
		{
				code = adad.charCodeAt(i);
				if(code>=48 && code<=57)
						ret += String.fromCharCode(code + 1584);
				else
						ret += adad.substr(i, 1);
		}
		return ret;
}
function shamsi(time, monthOption, weekOption, hourOption) //phptime
{

		var weeks = new Array("يكشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه", "شنبه")
		var months = new Array("فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دي", "بهمن", "اسفند");

		var date = new Date(time * 1000);
		year = date.getYear();
		month = date.getMonth()+1;
		day = date.getDate();
		arrMonths =[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		arrStart =[21, 20, 21, 21, 22, 22, 23, 23, 23, 23, 22, 22]


		if (year % 4 == 0)
		{
				for (i = 2; i < 12; i++)
						arrStart[i]--;
				arrMonths[1]++;
				if (month == 1) arrStart[11]++;
		}
		else if (year % 4 == 1)
		{
				arrStart[0]--;
				arrStart[1]--;
				if (month == 1) arrStart[11]--;
		}
		year = month <= 3? year - 622: year - 621;
		if (month == 3 && day >= arrStart[2]) year++;

		if (year<0)year = year+2010-110

				if (day < arrStart[month - 1])
		{
				i = month == 1? 11: month - 2;
				day = day - arrStart[i] + arrMonths[i] + 1;
				month -= 3;
		}
		else
		{
				day = day - arrStart[month - 1] + 1;
				month -= 2;
		}
		hour = date.getHours();
		min = date.getMinutes();
		sec = date.getSeconds();

		if(hour<10)hour = "0"+hour
				if(min<10)min = "0"+min
				//if(sec<10)sec="0"+sec
				if (month <= 0) month += 12;

		if(weekOption==1)
				week = weeks[date.getDay()]+' '
				else
						week = ''

						if(hourOption==1)
						hour2 = ' '+hour+':'+min+':'+sec
						else
								hour2 = ''

								if(monthOption==1)
								month = ' '+months[month-1]+' '
								else
										month = '/'+month+'/'
										return week+day+month+year+hour2;
} //document.write(shamsi(1271325610))

function in_array(what, where) {
		var a = false;
		for(var i = 0; i<where.length; i++) {
				if(what == where[i]) {
						a = true;
						break;
				}
		}
		return a;
}

// -----------   qtip jquery

function renderAllTips(themeName) {

		$('a[tip=1], .isqtip').each(function() // Select all elements with the "tooltip" attribute
				{
						var sqtip;
						var tit = $.trim($(this).attr('subTitle'));
						var body = $.trim($(this).attr('title'));
						$(this).attr({'title': ''})
						var targetCorner = $.trim($(this).attr('tipTargetCorner'));
						var tooltipCorner = $.trim($(this).attr('tipTooltipCorner'));


						var widthmax = $.trim($(this).attr('tipWidthMax'));
						if(!themeName)themeName = 'blue'

								sqtip = $(this).qtip(
										{content:
												{
												text: "<span style='font-size:11px;'>"+body+"</span>",
												title: {
														text: (tit!=null && tit.length!=0)? '&nbsp;' + tit + '&nbsp;': false /*,button: 'Close'*/

														}
												},
										show: {solo: false, delay: 0
                                                },
										hide: {delay: 100,
                                            fixed :  true, leave: false
                                                } ,
										position: {
												target: 'event',
												my: (tooltipCorner!=null && tooltipCorner.length!=0)? tooltipCorner: 'topLeft',
                                                viewport: $(window), // Keep it on-screen at all times if possible
                                                   adjust: {
                                                      x: 10,  y: 10
                                                   }
                                             	},
										style: {
                                            		classes: "ui-tooltip-rounded"
                                            	},
                                        events: {
                                            		render: function(){
                                                        $('.qtip').hide()
                                                  	    if(typeof setAllPrettyPhoto == 'function')
                                                            setAllPrettyPhoto();
                                                    }
                                                }
										}
								);

				}
		);
}
//------------------------------   TableClass

function TableClass(divId, noAutoNum) {
		this.index = TableClass.instances.length;
		TableClass.instances[this.index] = this;
		this.divid = divId
		this.elements
		this.cols
		this.noautonum = noAutoNum
		this.notnumber
		this.order
		this.ordersoo//=1
		this.show = function(array2D, array2D_View) {
				if(!array2D || array2D.length==0)
						return document.getElementById(this.divid).innerHTML = 'نتيجه خالي است..!!!';
				this.elements = cloneObject(array2D)
				var elemview = cloneObject(array2D_View)
				this.order = ''
				this.cols = {}
				var nottime = {}
				this.notnumber = {}
				for(var element in this.elements) {
						for(var attribute in this.elements[element]) {
								if(!this.cols[attribute])
										this.cols[attribute] =[]
										var val = this.cols[attribute][element] = this.elements[element][attribute]
										if(!(val>0||val<=0))
										this.notnumber[attribute] = 1
										if(!(val>=100000000 && val<=2000000000))
										nottime[attribute] = 1
						}
				}
				for(var element in this.elements) {
						for(var attribute in this.cols) {
								if(!this.cols[attribute][element]) {
										this.elements[element][attribute] =[]
										this.elements[element][attribute][0] = this.elements[element][attribute][1] = ''
								}
								else {
										var val = this.cols[attribute][element]
										this.elements[element][attribute] =[]
										this.elements[element][attribute][0] = val
										if(val!='' && !nottime[attribute] || (attribute=='time'||attribute=='expire'||attribute=='now' )) {
												// if(attribute=='time'||attribute=='expire'||attribute=='now'||attribute.match(/time$/)||attribute.match(/^time/))
												val = shamsi(val, 1, 1, 1)
												elemview[element][attribute] = val
										}
										this.elements[element][attribute][1] = elemview[element][attribute]
								}
						}
				}
				return this.reshow()
		}
		this.reshow = function() {
				var htm_table = ''
				if(this.elements.length>10)countrow = 'تعداد: '+this.elements.length; else countrow = 'رديف';
				htm_table += '<tr>'+(this.noautonum? '': '<th><font style="font-size:5px;">'+countrow+'</font></th>') // مورد چندم
				for(var colname in this.cols)
						htm_table += '<th><a href="javascript:void(0)" onclick="return TableClass.instances['+this.index+'].sort(\''+colname+'\');">'+colname+(this.order==colname? (this.ordersoo>0? '<img src="./im/all/arrowup.gif"/>': '<img src="./im/all/arrowdown.gif"/>'): '')+'</a></th>'
						for(var element in this.elements) {
						htm_table += '<tr>'+(this.noautonum? '': '<th>'+(1+parseInt(element))+'</th>')
						for(var attribute in this.cols) {
								var val = this.elements[element][attribute][1]
								htm_table += '<td>'+val+'</td>'
						}
						htm_table += '</tr>'
				}
				htm_table = '<table border="0">'+htm_table+'</table>'
				return document.getElementById(this.divid).innerHTML = htm_table
		}
		this.sort = function(colname) {
				if(this.order==colname) {
						this.elements.reverse()
						this.ordersoo *= -1
				}
				else {
						if(this.notnumber[colname])
								this.elements.sort(function(a, b)
										{
												return a[colname][0]<b[colname][0]? -1: (a[colname][0]>b[colname][0]? 1: 0)
										}
								)
								else
										this.elements.sort(function(a, b)
												{
														return a[colname][0]-b[colname][0]
												}
										)
										this.order = colname
										this.ordersoo = 1
				}
				this.reshow()
		}
}
TableClass.instances =[];

function cloneObject(what) {
  return jQuery.extend(true, [], what);
}

//------
function readable_time(timestamp, now, num_times)
{
		if(!num_times)
				num_times = 2;
		var times =[['31536000', 'سال'],['2592000', 'ماه'],['604800', 'هفته'],['86400', 'روز'],['3600', 'ساعت'],['60', 'دقيقه'],['1', 'ثانيه']];

		var secs = now - timestamp;

		if(secs<0) {
				laststr = ' ديگر '
				secs = secs*-1
		} else {
				laststr = ' پيش '
		}
		if (secs == 0) {
				secs = 1;
		}
		var count = 0;
		var time = '';
		for (i in times)
		{
				var key = times[i][0]
				var value = times[i][1]
				//alert(key)
				if (secs >= key)
				{
						var s = '';
						time += Math.floor(secs / key);
						time += ' ' + value + s;
						count++;
						secs = secs % key;
						if (count > num_times - 1 || secs == 0)
								break;
						else
								time += ' و';
				}
		}
		return time + laststr;
}

//sdocument.write(readable_time(phptime()-60*60*3-112))

function rand(min, max) {
    return parseInt(Math.random() * (max-min+1), 10) + min;
}


///   values

sm_info = [{},{'count':256,'factor':50,'start':-1300,'olduemtiaz':-50},{'count':132,'factor':300,'start':5000,'olduemtiaz':-300},{'count':153,'factor':1000,'start':10000,'olduemtiaz':-1000},{'count':56,'factor':5000,'start':100000,'olduemtiaz':-5000}]
