﻿function getHTTPObject() {
    try{
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e){
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e){
        xmlhttp = false;
      }
    }	
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function getPos(theObj){
	  x = y = 0;
	  h = theObj.offsetHeight;
	  w = theObj.offsetWidth;
	  while(theObj){
	    x += theObj.offsetLeft;
	    y += theObj.offsetTop;
	    theObj = theObj.offsetParent;
	  }
		return {height:h,width:w,x:x,y:y}
}

function loadFile(file,div,data){

	rv = getPos(div);

	opaciti = 70;
    document.getElementById('loading-div').style.opacity = opaciti / 100;
    document.getElementById('loading-div').style.MozOpacity = opaciti / 100;
    document.getElementById('loading-div').style.KhtmlOpacity = opaciti / 100;
    document.getElementById('loading-div').style.filter = 'alpha(opacity=' + opaciti + ')';

	document.getElementById('loading-div').style.visibility = 'visible';
	document.getElementById('loading-div').style.top    = rv.y;
	document.getElementById('loading-div').style.left   = rv.x;
	document.getElementById('loading-div').style.width  = rv.width;
	document.getElementById('loading-div').style.height = rv.height;
	
	loading = "<table align=center height="+rv.height+"><tr><td align=center style='color:white'><img alt='لطفا کمی صبر کنید' src='images/loading/loading.gif'></td></tr></table>";
	document.getElementById('loading-div').innerHTML = loading;
	data = "rand=" + parseInt(Math.random()*999999999999999) + data ;
	xmlhttp = getHTTPObject();
	xmlhttp.open("POST", file);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById('loading-div').style.width  = '5px';
			document.getElementById('loading-div').style.height = '5px';
			document.getElementById('loading-div').style.top  = '5px';
			document.getElementById('loading-div').style.left = '5px';
			document.getElementById('loading-div').style.position = 'absolute';
			document.getElementById('loading-div').innerHTML = '';
			document.getElementById('loading-div').style.visibility = 'hidden';
			div.innerHTML = xmlhttp.responseText;
		}
	}	
	xmlhttp.send(data);
return false;
}

function submitFormData(formname,file,div){
	tmp_data = "";
	form_len = document.forms[formname].elements.length;
   	for(i=0; i<form_len; i++){
    	tmp_data = tmp_data + "&" + document.forms[formname].elements[i].name + '=' + document.forms[formname].elements[i].value;
   	}
   	return loadFile(file,div,tmp_data);
}
