var http_request = false;
function makePOSTRequest(url, parameters, targetId, type) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     //alert('Cannot create XMLHTTP instance');
     alert('There was a temporary problem, please try again!');
     return false;
  }

	http_request.onreadystatechange = function() { alertContents(targetId, type); };
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents(targetId, type) {

  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        result = http_request.responseText;
				document.getElementById(targetId).innerHTML = result;
     } else {
        //alert('There was a problem with the request.' +http_request.status);
        alert('There was a temporary problem, please try again!');
     }
  }else{
		//show loading image
		if(type == 'normal'){
      document.getElementById(targetId).innerHTML = '<table align="center" height="150"><tr class="processIndicator"><td align="center"><img src="../images/waiting.gif" align="center"><br><br><b>Registering your account</b></td></tr></table>';
    }
		if(type == 'addToCart'){
      document.getElementById(targetId).innerHTML = '<table align="center" height="150"><tr class="processIndicator"><td align="center"><img src="../images/waiting.gif" align="center"><br><br><b>Updating Your Cart</b></td></tr></table>';
    }
		if(type == 'deleteFromCart'){
      //document.getElementById(targetId).innerHTML = '<table align="center" height="150"><tr class="processIndicator"><td align="center"><img src="../images/waiting.gif" align="center"><br><br><b>Updating Your Cart</b></td></tr></table>';
    }
		if(type == 'delImg'){
      document.getElementById(targetId).innerHTML = '<div><img src="/clients/scribanteplant/images/waiting.gif" align="absmiddle"> Deleting...</div>';
    }
		if(type == 'contactForm'){
      document.getElementById(targetId).innerHTML = '<table align="center"><tr class="processIndicator"><td align="center"><img src="../images/waiting.gif" align="center"><br>Sending...</td></tr></table>';
    }
		if(type == 'loadImage'){
      document.getElementById(targetId).innerHTML = '<table align="center" height="150"><tr class="processIndicator"><td align="center"><img src="../images/waiting_bar.gif" align="center"><br>Loading Image</td></tr></table>';
    }
		//catch expired timeout
	}
}

function get(obj,fieldName,targetId) {
	var poststr = fieldName+"="+encodeURI( document.getElementById(fieldName).value );
  makePOSTRequest('post.php', poststr, targetId);
}



 function getAr(obj,fieldNameArray,targetId,type) {
    
    if(type == ''){    
      type = 'normal';
    }
    
		var poststr='';
		for(i=0;i<fieldNameArray.length; i++) {
   		if(poststr==''){
			 	var poststr = fieldNameArray[i]+"="+encodeURI( document.getElementById(fieldNameArray[i]).value );
				 }
				else{
				var poststr = poststr+"&"+fieldNameArray[i]+"="+encodeURI( document.getElementById(fieldNameArray[i]).value );
				}
			}
			//alert(poststr);
      if(type == 'normal'){
        makePOSTRequest('../ajax/process.php', poststr, targetId, type);
      }
      if(type == 'addToCart'){
        makePOSTRequest('../ajax/process.php', poststr, targetId, type);
      }
      if(type == 'deleteFromCart'){
        makePOSTRequest('../ajax/process.php', poststr, targetId, type);
      }
      if(type == 'delImg'){
        makePOSTRequest('/clients/scribanteplant/ajax/process.php', poststr, targetId, type);
      }
      if(type == 'contactForm'){
        makePOSTRequest('../ajax/process.php', poststr, targetId, type);
      }
      if(type == 'loadImage'){
        makePOSTRequest('../ajax/process.php', poststr, targetId, type);
      }
}

function validateForm(){
  if(document.signupForm.email.value == ''){
    alert('Please enter your e-mail address');
    return false;
  }
  return true;         
}
