//##################################################################################
//## FORM SUBMIT WITH AJAX                                                        ##
//## @Author: Simone Rodriguez aka Pukos <http://www.SimoneRodriguez.com>         ##
//## @Version: 1.2                                                                ##
//## @Released: 28/08/2007                                                        ##
//## @License: GNU/GPL v. 2 <http://www.gnu.org/copyleft/gpl.html>                ##
//##################################################################################


function ajaxform(thisform,formhandler,respdiv,ajxTag)
{
    //General Purpose Ajax form submitter.
    //Written by Carl(bag) @ Thybag.co.uk

    // Set up data variable
    var formdata = "";

    // Set up Ajax request variable
    try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}


if (ajxTag!="onlineuser" && ajxTag!="logout" && ajxTag!="accessResources" && ajxTag!="checkdomain" )
{


    // Loop through form fields
    for (i=0; i < thisform.length; i++)
    {
      
  //Build Send String
         if(thisform.elements[i].type == "text"){ //Handle Textbox's
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "textarea"){ //Handle textareas
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
                 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
         }else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
                  if(thisform.elements[i].checked==true){
                     formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
                  }
         }else{
                  //finally, this should theoretically this is a select box.
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }
    }
}
    //Send Ajax Request

if (ajxTag=="logincheck")
    xmlhttp.onreadystatechange = loginCheck;
else
	if (ajxTag=="register")
		xmlhttp.onreadystatechange = userRegister;
	else
		if (ajxTag=="onlineuser")
			xmlhttp.onreadystatechange = fn_onlineuser;
		else
			if (ajxTag=="logout")
				xmlhttp.onreadystatechange = fn_logout;
			else
				if (ajxTag=="accessResources")
					xmlhttp.onreadystatechange = fn_accessResources;
				else
					if (ajxTag=="contactus")
						xmlhttp.onreadystatechange = fn_contactus;
					else
						if (ajxTag=="forgetpassword")
							xmlhttp.onreadystatechange = fn_forgetpassword;
						else
							if (ajxTag=="checkdomain")
								xmlhttp.onreadystatechange = fn_checkdomain;
							else
								if (ajxTag=="upload")
									xmlhttp.onreadystatechange = fn_upload;
								else
									if (ajxTag=="registerAWB")
										xmlhttp.onreadystatechange = fn_userRegister_AWB;
									else
											alert("No ajax function");


    //Make connection
    xmlhttp.open("POST", formhandler);
    //Set Headers
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //Send data

    xmlhttp.send(formdata);
    //stops form from submitting normally
    return false;
}

function fn_upload()
{

 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
			
						//alert(resultText);
						document.getElementById('output').innerHTML=resultText;


			}}


}

function fn_checkdomain()
{

 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
			
						//alert(resultText);
						if (resultText=='available')
						document.getElementById('logstatus').innerHTML="<div class='successBox'>Congratulations, this domain is available</div>";

						if (resultText=='notavailable')
						document.getElementById('logstatus').innerHTML="<div class='failBox'>Alas, this domain is not available</div>";
						if (resultText=='notresolved')
						document.getElementById('logstatus').innerHTML="<div class='failBox'>Alas, this domain is not resolved! Try again</div>";


			}}


}


function fn_forgetpassword(){

 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
			document.getElementById('logstaus').innerHTML='';

						alert(resultText);

			}}

}

function fn_contactus(){

 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
			
						alert(resultText);

			}}

}



function fn_accessResources(){

 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
			
				if(resultText=='1')
					//window.open('http://www.acescope.com/downloads/resman/RMSetup.zip');

					window.open('http://www.acescope.com/downloaddemo.asp');
				else
				if(resultText=='2')
					window.open('http://www.acescope.com/shopping/shopping.asp?action=add&item=1&count=1');
				else
					alert(resultText);

			}}

}

function fn_logout(){
               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
				alert(resultText);

				
					document.getElementById('loguser').innerHTML='';
					document.getElementById('offline').className='';
					document.getElementById('online').className='dispNone';	
					
                      	


			}}
    }

function fn_onlineuser(){
               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
				//alert(resultText);

				if (resultText!='Guest' && resultText!='')
				{
				//alert("["+resultText+"]");
				if (resultText.length > 6)
					document.getElementById('loguser').innerHTML=resultText.substring(0,5)+"..";
				else
					document.getElementById('loguser').innerHTML=resultText;

					document.getElementById('offline').className='dispNone';
					document.getElementById('online').className='';	
					
				}
				else
				{
				//alert("{"+resultText+"}");


					document.getElementById('loguser').innerHTML='';
					document.getElementById('offline').className='';
					document.getElementById('online').className='dispNone';	
				}			
                      	


			}}
    }



function userRegister(){
               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;

				resArray=resultText.split('|')

				if (resArray[0]=='yes')
				{
	                      	alert(resArray[1]);		
					//respdiv.innerHTML="Resigter Sucessful, Sign In";


					if (resArray[2].length > 6)
						document.getElementById('loguser').innerHTML=resArray[2].substring(0,5)+"..";
					else
						document.getElementById('loguser').innerHTML=resArray[2];


					document.getElementById('offline').className='dispNone';
					document.getElementById('online').className='';
					hidemodal();




				}
				else
				{
                      		alert(resArray[1]);
					//respdiv.innerHTML="Sorry! Resigteration unsucessful";
				}			
                      	


			}}
    }

function fn_userRegister_AWB(){

               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.

				resultText=xmlhttp.responseText;

				resArray=resultText.split('|')

				if (resArray[0]=='yes')
				{
	                      	alert(resArray[1]);		
					//respdiv.innerHTML="Resigter Sucessful, Sign In";


					if (resArray[2].length > 6)
						document.getElementById('loguser').innerHTML=resArray[2].substring(0,5)+"..";
					else
						document.getElementById('loguser').innerHTML=resArray[2];


					//document.getElementById('offline').className='dispNone';
					//document.getElementById('online').className='';
					//hidemodal();




				}
				else
				{
                      		alert(resArray[1]);
					//respdiv.innerHTML="Sorry! Registeration unsucessful";
				}			
                      	


			}}
    }


function loginCheck(){
               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
                   {{   //What to do once the form is submitted - to inform the user.
				resultText=xmlhttp.responseText;
				resArray=resultText.split('|')
				document.getElementById('hid').innerHTML=resArray[0];
				//alert(resArray[0]);
					document.getElementById('loguser').innerHTML='';
				if (resArray[0]=='yes')
				{

					if (resArray[2].length > 6)
						document.getElementById('loguser').innerHTML=resArray[2].substring(0,5)+"..";
					else
						document.getElementById('loguser').innerHTML=resArray[2];


					document.getElementById('offline').className='dispNone';
					document.getElementById('online').className='';

	                      	alert(resArray[1]);
				}
				else
				{
                      		alert(resArray[1]);
				}			
                      	


				respdiv.innerHTML="Resigter Sucessful, Sign In";

			}}
    }






