var time_variable;
 
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

function isValidURL(url){
	url2=url.toLowerCase();
	if ((url2.indexOf("snappyurl.com") == -1) && (url2.indexOf("snappyurl.co.uk") == -1)) {
	   var RegExp = /^(((https?):)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(\.)?(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+,_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+,_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    	if(RegExp.test(url)){
        	return true;
	    }else{
    	    return false;
	    }
	} else {
		return false;
	}
}

function ajaxFunction() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var txtname = document.getElementById("URL");
	var strname = txtname.value;
	if (strname.length == 0) 
	{
		step3();
		return false;
	}
	if (isValidURL(strname) == 0) {
		alert("Please enter a valid URL.");
		step3();
		return false;
	}
	xmlhttp.open("POST","/_includes/php/submit.php",true); //calling submit.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    //xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	/*strname=strname.replace(/[&]/g,"{$#$}");
	strname=strname.replace(/[%]/g,"{$$##$$}");
	strname=strname.replace(/[+]/g,";[;[");*/

	document.snappyChange.snappysubmit.disabled=true;
	document.snappyChange.snappysubmit.value="Please Wait!";
	document.getElementById("snappysubmit").className = "button";
    xmlhttp.send(strname); //Posting txtname to PHP File
  }
}
 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		 if (xmlhttp.responseText != "")
		 {
		    document.snappyChange.snappyurl.value=xmlhttp.responseText; //Update the HTML Form element 
			document.snappyChange.snappysubmit.disabled=false;
			document.snappyChange.snappycopy.disabled=false;
			document.snappyChange.snappyemail.disabled=false;
			document.snappyChange.snappygo.disabled=false;
			document.snappyChange.snappysubmit.value="Get your snappyURL now!";
			step2();
		 }
		 else
		 {
			 alert("Invalid or inaccessible URL, please try again!");
			 step3();
		 }
     }
     else {
        alert("Error during AJAX call. Please try again");
		document.snappyChange.snappysubmit.disabled=false;
		document.snappyChange.snappysubmit.value="Get your snappyURL now!";
		step3();
     }
   }
}

function GoToURL() {
	window.status=('Connecting....');
	var URLis;
	URLis = document.getElementById("snappyurl");
	URLis = URLis.value;
	var location=("http://" + URLis);
	this.location.href = location;
	window.status=('Connecting to ' + URLis + '  Please wait........');
}

function GoToEmail() {
	var emailis;
	emailis = 'mailto:?Subject=I am sending you this email from SnappyURL - the URL shortening website!&Body=http://' + document.getElementById("snappyurl").value;
	this.location.href=emailis;
}
