function getViewportDimensions() {
    var intH = 0, intW = 0;
    
    if(self.innerHeight) {
       intH = window.innerHeight;
       intW = window.innerWidth;
    } 
    else {
        if(document.documentElement && document.documentElement.clientHeight) {
            intH = document.documentElement.clientHeight;
            intW = document.documentElement.clientWidth;
        }
        else {
            if(document.body) {
                intH = document.body.clientHeight;
                intW = document.body.clientWidth;
            }
        }
    }
    intH=intH+2*document.body.scrollTop;
    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}
function Go2Center(element)
{
 var viewport = getViewportDimensions();
 var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - element.offsetWidth) / 2, 10);
 var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - element.offsetHeight) / 2, 10);
 element.style.left = left + 'px';
 element.style.top = top + 'px';
}

//******************************************************************************************************
//******************************************************************************************************


function RemoveElement(elemName)
{
document.body.removeChild(document.getElementById(elemName));
}


function AjaxObject101() {
    this.createRequestObject = function() {    //This is called at the very bottom -- it sets this.http to the Ajax request object (ro)
        var ro;    //This will hold the request object -- either Microsoft.XMLHTTP or XMLHttpRequest
        try {    //Let's use a try/catch system just in case something goes wrong -- we can at least default back to the XMLHttpRequest object
            ro = new XMLHttpRequest();
        }
        catch (e) {
            ro = new ActiveXObject("Microsoft.XMLHTTP"); //We'll at least try the IE6 and lower version    
        }
        return ro;    //Now that we've taken care of cross-browser compatibility, this.http will represent the request object for this instance of AjaxObject
    }
    this.sndReq = function(action, url, data) { //This function will start the Ajax process, and is called manually by the user
        if (action.toUpperCase() == "POST") { //Post method
            this.http.open(action,url,true); //The URL includes any GET or POST variables you want
            this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); //Encode the data
            this.http.onreadystatechange = this.handleResponse; //This is the callback function when the state of the http object changes
            this.http.send(data);    //Actually start the request process -- this will contact the URL via the action specified, and wait to call this.handleResponse
        }
        else {    //'get' method is default
            this.http.open(action,url + '?' + data,true); //The URL includes any GET or POST variables you want
            this.http.onreadystatechange = this.handleResponse; //This is the callback function when the state of the http object changes
            this.http.send(null);    //Actually start the request process -- this will contact the URL via the action specified, and wait to call this.handleResponse
            
        }    

        //action is either 'get' or 'post'
        //url can contain GET variables -- like myajax.php?action=test
    }
    this.handleResponse = function() { //This function is called when this.http's state changes
        if ( me.http.readyState == 4) { //State of 4 means the connection is done (data was transferred)
            if (typeof me.funcDone == 'function') { me.funcDone();} //Execute the funcDone function if it's been set by the user
            if(me.http.responseText==undefined)
            {
                alert("Nothing to show!");
                return;
            } 
            var rawdata = me.http.responseText.split("|"); //If there aren't any |'s in the string, it will just grab the entire string and put it into items[0]
            for ( var i = 0; i < rawdata.length; i++ ) { //Loop through the rawdata[] array
                var item = (rawdata[i]).split("=>");    //Split each item into id=>value where id is item[0] and the value is item[1]
                if(item.length<2)
                        alert(item);
                else if (item[0] != "") {    //If it is a valid HTML id
                    if(item[0]=="%A%")  //Show Alert
                        alert(item[1]);
                    else if(item[0]=="%NW%") //Show in New Window
                    {
                        newwindow=window.open();
                        if(!newwindow)
                            alert('را غير فعال کنيد يا بر روي نوار زرد رنگ کليک راست کرده و اولين گزينه را انتخاب کنيد Popup Blocker لطفا . ');
                        newdocument=newwindow.document;
                        newdocument.write(item[1]);
						//newwindow.location.reload( false );;
						
                    }
					else if(item[0]=="%JS%")
					{
						var headID = document.getElementsByTagName("head")[0];         
						var s = document.createElement('script');
						s.type = 'text/javascript';
						s.src=item[1];
						headID.appendChild(s);
					}
					else if(item[0]=="%js%")
					{
						eval(item[1]);
                    }
					else if(item[0]=="%PC%") //Close PopUp window
                            me.PopupClose(item[1]);
                    else if(item[0].substr(0,4)=="%PO%") //Open PopUp Winodw
                        me.Popup(item[0].substring(4),item[1]);                        
                    else if (item[1].substr(0,3) == "%V%" ) { //When sending a response, if you want to change the .value of an item (rather than .innerHTML), preface your response with %V%
                        document.getElementById(item[0]).value = item[1].substring(3);  //Set the value property of the given HTML item to the value of item[1]
                    }
                    else {

							if(document.getElementById(item[0])==undefined)
                            {
                                alert("There is no <"+item[0]+"> item.");
                                return;
                            }
                            document.getElementById(item[0]).innerHTML = item[1]; //Set the innerHTML property of the given HTML item to the value of item[1]
							
                    }
                    //item[0] is the id of the HTML element, item[1] is the value of that HTML element
                }
            }
        }
        if ((me.http.readyState == 1) && (typeof me.funcWait == 'function')) { //This code will run if funcWait is pointed at a function -- this could call a spinner icon, or a "Please wait..." label
            me.funcWait();
        }
    }
    
    this.funcWait = function(){
        var viewport = getViewportDimensions();
        var Coverdiv=document.createElement("div");
        Coverdiv.id="CoveringDiv";
        Coverdiv.className="spotlight";
        Coverdiv.style.zIndex=2000;
        
        //Coverdiv.style.height=document.documentElement.scrollHeight + 'px';//viewport.height + "px"; 
        //Coverdiv.style.width='200%';//document.documentElement.scrollWidth + 'px';//viewport.width + "px"; 
        document.body.appendChild(Coverdiv);
        
        
        var Wdiv=document.createElement("div");
        var WidtH=200;
        var HeighT=150;
        Wdiv.id="WaitingDiv";
        Wdiv.style.position="absolute";
        Wdiv.style.background="rgb(223,232,246)";
        //Wdiv.style.opacity=0.7;
        //Wdiv.style.filter = "alpha(opacity=70)";
        Wdiv.style.zIndex=2001;
        //Wdiv.style.width=WidtH+"px";
        //Wdiv.style.height=HeighT+"px";
        Wdiv.innerHTML='<center><br><img  src="./images/wait.gif"  /><br><br><span class=TDButt>...لطفا چند لحظه منتظر بمانيد<br><br></span>';
        document.body.appendChild(Wdiv);
        Go2Center(Wdiv);
       
    }

    this.funcDone = function(){
       RemoveElement("WaitingDiv"); 
        RemoveElement("CoveringDiv");
    }    
    
    this.PopupClose=function(DivName){
                        RemoveElement(DivName);
                        RemoveElement("BlockModal");
                        }
    this.Popup=function(DivName,Content){

        var BlockModal=document.createElement("div");
        BlockModal.id="BlockModal";
        BlockModal.className="spotlight";
        document.body.appendChild(BlockModal);


        var Pdiv=document.createElement("div");
        Pdiv.id=DivName;
        Pdiv.style.position="absolute";
        //Pdiv.innerHTML='<div class=HeaderTitle>salam</div><div>'+Content+'</div>';
        Pdiv.innerHTML='    <DIV class="x-panel x-form-label-right" id=ext-comp-1003 style="WIDTH: 100%;"><DIV class=x-panel-tl><DIV class=x-panel-tr><DIV class=x-panel-tc>'+
                       '    <DIV class="x-panel-header x-unselectable"><div  style="width:15px;float:left"><img id=CloseButton class=TDButt alt=close onclick="RemoveElement(\''+Pdiv.id+'\');RemoveElement(\'BlockModal\');" src="./images/close.gif"></div><div class=MainHeader style="text-align:right;dir:rtl" id="PopupTitle" ></div></DIV></DIV></DIV></DIV>'+
                       '<DIV class=x-panel-bwrap id=ext-gen19><DIV class=x-panel-ml><DIV class=x-panel-mr><DIV class=x-panel-mc>'+'<div>'+Content+'</div>'+
                       '</DIV></DIV></DIV><DIV class=x-panel-bl><DIV class=x-panel-br><DIV class=x-panel-bc></DIV></DIV></DIV></DIV></DIV>'    ;

        Pdiv.style.width=400+"px";
        Pdiv.style.height=0+"px";
        Pdiv.style.zIndex=1003;
        Pdiv.style.opacity=0.9;
        Pdiv.style.filter = "alpha(opacity=90)";
        Pdiv.style.border=2+"px";
        document.body.appendChild(Pdiv);
        Go2Center(Pdiv);
        //document.getElementById('CancelButton').onclick=function(){document.body.removeChild(Pdiv);}
        //document.getElementById(DivName).onkeypress=function(){if(event.keyCode==27){document.body.removeChild(Pdiv);RemoveElement("CoveringDiv");}}
        //document.body.onkeypress=function(){if(event.keyCode==27){document.body.removeChild(Pdiv);RemoveElement("CoveringDiv");}}
    }
    
    var me = this;    //Unfortunately, this is necessary because this.http won't work in the callback function 'handleResponse' (we have to use me.http instead)
    this.http = this.createRequestObject(); //http holds the request object (ro), which is returned from the createRequestObject() function -- this is automatically run when you make a new AjaxObject()
    
}



//******************************************************************************************************
//******************************************************************************************************
function show(action)
{
    var ao = new AjaxObject101(); 
    //scroll(0,0);
//    ao.funcWait = Waiting; 
//    ao.funcDone = DoneWaiting; 
    ao.sndReq('get','./index.php','action='+action);

}

function Form2URI(FormName) { 
    out=''; 
    if(!document.getElementById ) return; 
     var f = document.getElementById(FormName);  // Step through the form elements  
     for( var i = 0; i < f.elements.length; i++ )  
    {    
        if(i!=0)out+='&';
        if((f.elements[i].type=='radio')&&(f.elements[i].checked==false))
            continue;
        if(f.elements[i].type=='checkbox')
			f.elements[i].value=(f.elements[i].checked)?1:0;			
        if(f.elements[i].type=='file')
            out+=f.elements[i].name+'='+  f.elements[i];
        else
            out+=f.elements[i].name+'='+ encodeURI( f.elements[i].value );
        } 
return out;
}


function SendForm(action,FormName)
{
    
    var ao = new AjaxObject101(); 
    ao.sndReq('get','./index.php','action='+action+'&'+Form2URI(FormName));
}

//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************
/*

 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Copyright (C) Paul Johnston 1999 - 2000.
 * Updated by Greg Holt 2000 - 2001.
 * See http://pajhome.org.uk/site/legal.html for details.
 */

/*
 * Convert a 32-bit number to a hex string with ls-byte first
 */
SSID="<?php echo $SSID; ?>";
var hex_chr = "0123456789abcdef";
function rhex(num)
{
  str = "";
  for(j = 0; j <= 3; j++)
    str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr.charAt((num >> (j * 8)) & 0x0F);
  return str;
}

/*
 * Convert a string to a sequence of 16-word blocks, stored as an array.
 * Append padding bits and the length, as described in the MD5 standard.
 */
function str2blks_MD5(str)
{
  nblk = ((str.length + 8) >> 6) + 1;
  blks = new Array(nblk * 16);
  for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(i = 0; i < str.length; i++)
    blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[nblk * 16 - 2] = str.length * 8;
  return blks;
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * These functions implement the basic operation for each round of the
 * algorithm.
 */
function cmn(q, a, b, x, s, t)
{
  return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t)
{
  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t)
{
  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t)
{
  return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t)
{
  return cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Take a string and return the hex representation of its MD5.
 */
function MD5(str)
{
  x = str2blks_MD5(str);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = ff(c, d, a, b, x[i+10], 17, -42063);
    b = ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = ff(d, a, b, c, x[i+13], 12, -40341101);
    c = ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = gg(c, d, a, b, x[i+11], 14,  643717713);
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = gg(c, d, a, b, x[i+15], 14, -660478335);
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = hh(b, c, d, a, x[i+14], 23, -35309556);
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = hh(d, a, b, c, x[i+12], 11, -421815835);
    c = hh(c, d, a, b, x[i+15], 16,  530742520);
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = ii(c, d, a, b, x[i+10], 15, -1051523);
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = ii(d, a, b, c, x[i+15], 10, -30611744);
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}


function hash(password,challenge) {

      var hash1 = MD5(password)+ challenge;
      var hash2 = MD5(hash1);
      return hash2;
    }

//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************

    
function Browse(inp,siz)
    {
        var ifUpload;
        var confirmUpload;
        ifUpload = document.frames["frUpload"].document.form2; 
        ifUpload.id.value=inp;

		ifUpload.MAX_FILE_SIZE.value=siz*1000;
        ifUpload.myFile.click();
        if(ifUpload.myFile.value=='')
            return;
		var st=ifUpload.myFile.value.toLowerCase();
		var ext=st.match(/\.jpg+$/);	
		if(!ext){
		alert('فایلی بارگزاری نشد!\n'+'فقط فایل تصویر از نوع jpg قابل قبول است')
		return;
		}
		var ao = new AjaxObject101();
		ao.funcWait();
		ifUpload.btnSubmit.click();
    }    

//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************
//******************************************************************************************************
 	
var startx,starty;
var startml,startmt;
var IsMoving;
var imageElem;
var ROS=0;
function getElementLeft(elem) {
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
}


function getElementTop(elem) {
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	
}


function zoom(a)
{
windW=parseInt(a.parentNode.currentStyle['width']);
windH=parseInt(a.parentNode.currentStyle['height']);

pox=event.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft)-getElementLeft(a.offsetParent);
poy=event.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)-getElementTop(a.offsetParent);

if(ROS==0)
{	
	CurX=pox;
	CurY=poy;
	}
else if(ROS==1)
{
	CurY=windH-pox;
	CurX=poy;
}
else if(ROS==2)
{
	CurX=windW-pox;
	CurY=windH-poy;
}
else
{
	CurX=windW-poy;
	CurY=pox;
}

W_2=Math.floor(windW/2);
H_2=Math.floor(windH/2);

//alert(CurX+"\n"+CurY);

Xp=CurX-parseInt(a.style.marginLeft);
Yp=CurY-parseInt(a.style.marginTop);

//alert(Xp+"\n"+Yp);

ml=2*Xp-W_2;
mt=2*Yp-H_2;


if(ml<0) a.style.marginLeft=0;
else if(ml>(2*(a.width-W_2))) a.style.marginLeft=-2*(a.width-W_2);
else a.style.marginLeft=-ml;

if(mt<0) a.style.marginTop=0;
else if(mt>(2*(a.height-H_2))) a.style.marginTop=-2*(a.height-H_2);
else a.style.marginTop=-mt;

a.width=a.width*2;
a.height=a.height*2;


}


function reset(e)
{

e.style.marginLeft=0;
e.style.marginTop=0;
//e.width=parseInt(e.parentNode.width);
//e.height=parseInt(e.parentNode.height);
e.width=parseInt(e.parentNode.currentStyle['width']);
e.height=parseInt(e.parentNode.currentStyle['height']);
}

function MoveImage(e)
{
imageElem.style.cursor="url(./images/closedhand.cur)";
if (e == null) { e = window.event;}

//Xmov=event.clientX-startx;
//Ymov=event.clientY-starty;
pox=event.clientX-startx;
poy=event.clientY-starty;

if(ROS==0)
{	
	Xmov=pox;
	Ymov=poy;
	}
else if(ROS==1)
{
	Xmov=poy;
	Ymov=-pox;
}
else if(ROS==2)
{
	Xmov=-pox;
	Ymov=-poy;
}
else
{
	Xmov=-poy;
	Ymov=pox;
}






windW=parseInt(imageElem.parentNode.currentStyle['width']);
windH=parseInt(imageElem.parentNode.currentStyle['height']);
NmargL=startml+Xmov;
NmargT=startmt+Ymov;

if(((Xmov>0)&&(NmargL<0))||((Xmov<0)&&(Math.abs(NmargL)<imageElem.width-windW)))
		imageElem.style.marginLeft=NmargL;

if(((Ymov>0)&&(NmargT<0))||((Ymov<0)&&(Math.abs(NmargT)<imageElem.height-windH)))
		imageElem.style.marginTop=NmargT;

		

if((Math.abs(Xmov)>4)||(Math.abs(Ymov)>4))
	IsMoving=true;
return false;
}
function Mrelease(e)
{
imageElem.style.cursor="url(./images/zoom.cur)";
e.onmousemove=null;
if(IsMoving)
	return;
if(event.button==1)
	zoom(e);
else if(event.button==2)
	reset(e);
}

function Mclick(e)
{
startml=parseInt(e.style.marginLeft);
startmt=parseInt(e.style.marginTop);
startx=event.clientX;
starty=event.clientY;
IsMoving=false;
imageElem=e;
e.onmousemove=MoveImage;
}
function ROR(){
	ROS=ROS+1;
	if(ROS==4)ROS=0;
	ghaab.style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation='+ROS+')';}
function ROL(){
	ROS=ROS-1;
	if(ROS==-1)ROS=3;
	ghaab.style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation='+ROS+')';}
