﻿function showContent(id,cls){
    document.location.href="#";
    var cbg = document.getElementById("contentbg");
    cbg.style.width = (screen.width - 20) + "px";
    cbg.style.height = (screen.height - 20) + "px";
    cbg.style.display = "block";
    var c = document.getElementById("content");
    frmW = 640; frmH = 480;
    c.style.paddingLeft = ((screen.width/2)-(frmW/2)) + "px";
    c.style.display = cbg.style.display;
    temp = '<center><a href="#" class="rbutton red" onClick="hideContent()"><span style="width:100px;padding-top: 2px;padding-bottom: 10px;"><b>' + cls + '</b></span></a><br /><br />';
    temp += '<IFRAME id="container" src="showContent.aspx?id=' + String(id) + '" width="' + frmW + '" height="' + frmH + '"></IFRAME>';
    c.innerHTML = temp + '</center>';
}
function showImg(url,cls,hidebtn){
    if (url) {
        document.location.href="#";
        var cbg = document.getElementById("contentbg");
        cbg.style.width = (screen.width - 20) + "px";
        cbg.style.height = (screen.height - 20) + "px";
        cbg.style.display = "block";
        var c = document.getElementById("content");
        c.style.width = cbg.style.width;
        c.style.display = cbg.style.display;
        temp = '<center><span id="clearSpan"></span>';
        if (!hidebtn) { 
            temp += '<a href="#" class="rbutton red" onClick="if(typeof(jg)!=\'undefined\'){jg.clear();jg2.clear();}hideContent();"><span style="width:100px;padding-top: 2px;padding-bottom: 10px;"><b>' + cls + '</b></span></a><br /><br />';
            temp += '<img id="sourceImg" style="border-color:#000000" border="2" src="'+url+'" usemap="#Map" />';
        }
        else {
            cbg.style.backgroundColor = "black";
            c.style.paddingTop = "150px";
            temp += '<img border="0" src="'+url+'" />';
        }
        c.innerHTML = temp + '</center>';
    }
}
function selectStl(url,cls,clr,salonid, space, dimen) {
	if (url) {
		showImg(url,cls);
		c = document.getElementById("content");
		(currImg = new Image()).src = (c.getElementsByTagName("img")[0]).src;
        //alert ('The image size is '+currImg.width+'x'+currImg.height);
        alertText = "";
        if (currentLang == 'fa')
        {
            alertText = "لطفا برای انتخاب غرفه روی محل آن روی نقشه کلیک کنید. با حرکت ماوس روی محل غرفه اطلاعات آن غرفه نمایش داده می شود" + "\n"; 
		    if (space > 0) alertText += "\nمساحت سالن: " + space + " مترمربع";
		    if (dimen) alertText += "\nابعاد سالن: " + dimen.replace("x", "×") + " متر";
        }
        else
        {
            alertText = "Please choose stand by click on its location on map. The stand info will be shown if you move your mouse on its location.\n"; 
		    if (space > 0) alertText += "\nSalon ْSpace: " + space + " m²";
		    if (dimen) alertText += "\nSalon Dimension: " + dimen + " meters";
        }
        alert(alertText); //bayad hatman ye alert biad ta andazeye image load beshe!!!!!!
		s = document.getElementById("markupLayer");
        if (s) {
            jg = new jsGraphics(s);
            jg2 = new jsGraphics(s);
            s.style.display = c.style.display;
            s.style.width = c.style.width;
            currImgwidth = currImg.width;
            s.style.left = (((screen.width - 20)-currImgwidth)/2) + "px" ;
		    eval(Base64.decode(document.getElementById("salon"+salonid+"data").value)); //makes dataObj
			var stallIdArray = dataObj.stallIdList.split(',');
			var tmp = '<map name="Map" id="Map">';
			for (i=0;i<stallIdArray.length;i++) {
				tmp += urlDecode(dataObj.stall[stallIdArray[i]].maptag);
				if (selectedStalls.value.indexOf(stallIdArray[i])!=-1)
				    eval("drawConvas(jg ," + dataObj.stall[stallIdArray[i]].cpoint + ", '', 'images/checkmark.gif', 17, 14)");
			}
			tmp += '</map>';
			c.innerHTML += tmp;
			document.getElementById("clearSpan").innerHTML = '<a href="#" class="rbutton blue" onClick="clearSelected()"><span style="width:100px;padding-top: 2px;padding-bottom: 10px;"><b>' + clr + '</b></span></a> &nbsp;&nbsp;&nbsp; ';
			//alert(c.innerHTML);
		}
	}
}
function hideContent() {
    var cbg = document.getElementById("contentbg");
    var c = document.getElementById("content");
    cbg.style.display = "none";
    c.style.display = cbg.style.display;
    c.innerHTML = "";
	var s = document.getElementById("markupLayer");
    if (s)
        s.style.display = cbg.style.display;
}
function calculateInvoiceTotals(lang,symb) {
    var invTotal = 0, invTax = 0, invSum = 0;
    if (selectedStalls.value.length > 0) {
        var stallIdArray = selectedStalls.value.split(',');
        selectedStallAreas.value = '';
        for (i=0;i<stallIdArray.length;i++) 
            if (stallIdArray[i]) {
                vTotal1 = document.getElementById("stall"+stallIdArray[i]+"tot1").value;
                var oSelect = document.getElementById("stall"+stallIdArray[i]+"typ");
                areaId = oSelect.options[oSelect.selectedIndex].value.split('|')[0];
                selectedStallAreas.value += areaId + ",";
                invSum -= (-1 * vTotal1);
            }
    }
    if (selectedServices.value.length > 0) {
        var srvIdArray = selectedServices.value.split(',');
        for (i=0;i<srvIdArray.length;i++) 
            if (srvIdArray[i]) {
                vtotal1 = document.getElementById("srv"+srvIdArray[i]+"tot1").value;
                invSum -= (-1 * vtotal1);
            }
    }
    invTax = (fairTax * invSum) / 100;
    invTotal = invTax + invSum;
    
    document.getElementById("invoiceSum").innerHTML = formatCurrency(invSum,lang,symb);
    document.getElementById("invoiceTax").innerHTML = formatCurrency(invTax,lang,symb);
    document.getElementById("invoiceTotal").innerHTML = formatCurrency(invTotal,lang,symb);
    invoiceTotalPrc.value = invTotal;
}
function calcStallPrc(sid,lang,symb) {
    var oSelect = document.getElementById("stall"+sid+"typ");
    var oFi = document.getElementById("stall"+sid+"fi");
    var oAmount = document.getElementById("stall"+sid+"area");
    var oTotal = document.getElementById("stall"+sid+"tot");
    var oTotal1 = document.getElementById("stall"+sid+"tot1");
    
    fiprc = oSelect.options[oSelect.selectedIndex].value.split('|')[1];
    oFi.innerHTML = formatCurrency(fiprc,lang,symb);
    oTotal1.value = (fiprc * oAmount.value);
    oTotal.innerHTML = formatCurrency(oTotal1.value,lang,symb);
    if (document.getElementById("invoiceSum"))
        calculateInvoiceTotals(lang,symb);
}
function formatCurrency(num,lang,symb) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    if (lang && lang.toUpperCase() == 'FA')
        return (((sign)?'':'-') + num + ' ' + 'ریال' );
    symbol = '$';
    if (sign!='') symbol = symb;
    return (((sign)?'':'-') + symbol + num + '.' + cents);
}
function clearSelected()
{
    jg.clear();
    selectedStalls.value = "";
}

function drawConvas(canvas ,x, y, color, path, w, h) {
	x -= 7; y += 70;
	if (color) { 
		canvas.setColor(color); 
		canvas.fillOval(x,y,w,h); 
	}  
	if (path) 
		canvas.drawImage(path,x,y,w,h);
	canvas.paint();
}

function sign(identity)
{
	if (selectedStalls.value.indexOf(identity) == -1)
	    selectedStalls.value += identity + ",";
}

function tipTxt(dir, code, desc, space, dimen, prc) {
	s = "<div dir=\'" + dir + "\'><p>";
    var spa = prc.split(';');
	if (dir.toLowerCase() == 'rtl')
	{
		s += "<li>عنوان: " + Base64._utf8_decode(code) + "</li>";
		s += "<li>مساحت: " + space + " مترمربع</li>";
		s += "<li>ابعاد: <span dir=ltr>" + dimen + "</span> متر</li>";
	    s += "<li>قیمت کل: </li>";
        for (i=0;i<spa.length-1;i+=2)
            if (spa[i])
                s += "&nbsp;&nbsp;&nbsp;" + Base64._utf8_decode(spa[i]) + ": " + Base64._utf8_decode(spa[i+1]) + "<br />";
		if (desc) s += "<li> " + Base64._utf8_decode(desc) + "</li>";
		s += "</p><p>برای انتخاب غرفه، کلیک کنید</p>";
	}
	else
	{
		s += "<li>Title: " + code + "</li>";
		s += "<li>Space: " + space + " m&#0178;</li>";
		s += "<li>Dimension: " + dimen + " meters</li>";
	    s += "<li>Total Price: </li>";
        for (i=0;i<spa.length-1;i+=2)
            if (spa[i])
                s += "&nbsp;&nbsp;&nbsp;" + spa[i] + ": " + Base64._utf8_decode(spa[i+1]) + "<br />";
		if (desc) s += "<li> " + desc + "</li>";
		s += "</p><p>Click to choose this stand</p>";
	}
	s += "</div>";
	return s;
}

function tipTxtResv(dir, code, str, space, dimen, prc)
{
	s = "<div dir=\'" + dir + "\'><p>";
    var spa = prc.split(';');
	if (dir.toLowerCase() == 'rtl')
	{
		s += "<li>عنوان: " + Base64._utf8_decode(code) + "</li>";
		s += "<li>مساحت: " + space + " مترمربع</li>";
		s += "<li>ابعاد: <span dir=ltr>" + dimen + "</span> متر</li>";
	    s += "<li>قیمت کل: </li>";
        for (i=0;i<spa.length-1;i+=2)
            if (spa[i])
                s += "&nbsp;&nbsp;&nbsp;" + Base64._utf8_decode(spa[i]) + ": " + Base64._utf8_decode(spa[i+1]) + "<br />";
		if (str) s += "</p><p>این غرفه، توسط " + Base64._utf8_decode(str) + ".رزرو شده است" + "</li>";
		else s += "</p><p>این غرفه رزرو شده است" + "</li>";
	}
	else
	{
		s += "<li>Title: " + code + "</li>";
		s += "<li>Space: " + space + " m&#0178;</li>";
		s += "<li>Dimension: " + dimen + " meters</li>";
	    s += "<li>Total Price: </li>";
        for (i=0;i<spa.length-1;i+=2)
            if (spa[i])
                s += "&nbsp;&nbsp;&nbsp;" + spa[i] + ": " + Base64._utf8_decode(spa[i+1]) + "<br />";
		if (str) s += "</p>This stand has reserved by " + str + "</li>";
		else s += "</p><p>This stand has been reserved.</p>";
	}
	s += "</div>";
	return s;
}

function fakeyenter(evt,field)
{
    var key = (evt.which) ? evt.which : event.keyCode;
//    var e = evt? evt : window.event; 
//    if(!e) return; 
//    var key = 0; 
//    if (e.keyCode) { key = e.keyCode; } // for moz/fb, if keyCode==0 use 'which' 
//    else if (typeof(e.which)!= 'undefined') { key = e.which; } 

//    // usage ==>  onKeyPress="return fakeyenter(event,this)"
    var chars = ' !"#$%،گ)(×+و-./0123456789:ک,=.؟@ِذ}ىُيلا÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>ّ';
    if (key>31 && key<128)
    {
        if (document.all)
          window.event.keyCode=chars.charCodeAt(key-32);
        else
        {
          field.value += String.fromCharCode(chars.charCodeAt(key-32));
          return false;
        }
    }
}

function urlDecode(str){
    str=str.replace(new RegExp('\\+','g'),' ');
    return unescape(str);
}
function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

function getStyle(el, property) {
	if (!el) { return null; }
	if (el.currentStyle) {
		var tmp = property.split('-');
		property = tmp[0];
		for (var i = 1; i < tmp.length; i++) {
			property += tmp[i].slice(0, 1).toUpperCase() + tmp[i].slice(1);
		}
		return el.currentStyle[property];
	} else if (window.getComputedStyle) {
		return document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
	}
	return null;
}

var Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = Base64._utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = Base64._utf8_decode(output);
		return output;
	},
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

function CookieHandler() {
	this.setCookie = function (name, value, seconds) {
		if (typeof(seconds) != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}
	this.getCookie = function (name) {
		name = name + "=";
		var carray = document.cookie.split(';');
		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
		return null;
	}
	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
}

//You can attach this drag handler to any number of elements on page and you will be able to drag them all. 
//The most importaint thing is that these elements must be posisioned relatively or absolutely.
var DragHandler = {
	// private property.
	_oElem : null,
	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;
		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();
		return oElem;
	},
	// private method. Begin drag process.
	_dragBegin : function(e) {
		var oElem = DragHandler._oElem = this;
		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
		oElem.dragBegin(oElem, x, y);
		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},
	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
		oElem.drag(oElem, x, y);
		return false;
	},
	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		oElem.dragEnd(oElem, x, y);
		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}
