// GENERAL FUNCTIONS

var formdata;				//data of the form
var ctresponse;			//precess.php response
var mailresponse;		//sendmail.php response

function preloadSajax() {
      preload_image_object = new Image();
      image_url = new Array();
      image_url[0] = "images/sjwaiteml.png";
      image_url[1] = "images/sjeml.png";
      image_url[2] = "images/sjfail.png";
      image_url[3] = "images/ajax-loader.gif";
       var i = 0;
       for(i=0; i<=3; i++)
         preload_image_object.src = image_url[i];
}



// GENERAL EVENTS

$("#sjstatus").ajaxError(function(event, request, settings){
   showError(event, request, settings);
 });


function showWait() {
  $("#sjstatus").hide(100);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjwait'><div id='w1'></div></div><div id='sjwaitmsg'>Please wait while data is being processed. It may take some time depending on the speed of the network and current server loads.</div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").fadeIn(200);
}

function showWaitEmail() {
  $("#sjstatus").hide(100);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjwait'><div id='w1'></div></div><div id='sjwaitmsg'>Please wait while email is being sent. It may take some time depending on the speed of the network and current server loads.</div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").fadeIn(200);
}

function showDone(msg) {
	var mailMsg;

	if (mailresponse != "ok") {mailMsg='<b>WARNING: Email to you could not be sent.</b>'} else {mailMsg='<b>NOTE: An email has been sent to you.</b>'};

  $("#sjstatus").hide(100);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjdone'></div><div id='sjdonemsg'><p>Thank you for requesting information about the Arabic Flagship Program. We will be in touch soon to answer you inquiry and/or provide the information that you have requested. </p></div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").show(300);
}

function showError(caller, error, advise) {
  $("#sjstatus").hide(100);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjfail'></div><div id='sjfailmsg'><b>Failed: </b>"+ caller +"<br/><b>Reason: </b>"+error+"<br/><b>"+ advise +"</b><br/></div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").show(300);
  $(".2enable").show(300);
  $("#sajax").show();
}

// GENERAL AREA


//send data to a remote server or curl
function sndData(url, data) {
	data=encodeURI(data);
	showWait();
	$.ajax({
  type: 'POST',
  url: url,
  data: data,
  dataType: 'text',
  success: function(msg){
     cbSend(msg); //callback
   },
  error: function(caller, status, error ){
     showError( caller , status, error );
   }
});
}

//send email via sendmail.php
function sndEmail(to, from, subject, message) {
	var localData = 'to='+to+'&from='+from+'&subject='+subject+'&'+message;
  //alert(localData);
	scroll(0,0);
  $("#sajax").hideObj();
	showWaitEmail();
	$.ajax({
  type: 'POST',
  url: "/js/sendmail.php",
  data: localData,
  dataType: 'text',
  success: function(msg){
     cbMail(msg);
   },
  error: function(caller, status, error ){
     showError( caller , status, error );
   }
});
}

//callback from sndData if success
function cbSend(data) {
		ctresponse = data;
		sndEmail(formdata);
}

function cbMail(data) {
			if (data=="1") {
				mailresponse="ok";
				showDone();
  				//setTimeout ('window.location.href="http://www.google.com"',3000);
			} else {
				showError("Email sending", "Email could not be sent", "Please contact us and tell us about this problem.");
			}
}


jQuery.fn.showObj = function(speed) {
	if(!speed){speed=500};
  return this.each(function() {
   	$(this).fadeTo(1, 0.01);
    $(this).slideDown(speed/2,function() {$(this).fadeTo(speed,1)});
  });
 
};

jQuery.fn.hideObj = function(speed) {
	if(!speed){speed=500};
  $(this).fadeTo(speed, 0.01, function() {$(this).slideUp(speed/2)});
};

