// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject(){
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
			// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
			try{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}catch (e) {}
		}
	}

	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}


function checkms() {
  var mails = document.getElementById('inputEmail').value;
  var my_url = 'checkmail.php?mails=' + mails;
  xmlHttp.open("GET", my_url, true);
  xmlHttp.onreadystatechange = checkm;
  xmlHttp.send(null);
}

function checkm(){
   if (xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      var mytext = xmlHttp.responseText;
      if(mytext == 'error')
      document.getElementById('errdiv').innerHTML = 'This mail is taken';
    }
  } else {
  document.getElementById('errdiv').innerHTML = "";
  }
}

function clearcheck() {
 if(document.getElementById('errdiv'))
	document.getElementById('errdiv').value ='';

}



/////////////////////////////////////////////////////////////////////getOperators()
// read a file from the server
function getOperators(idname){
	// only continue if xmlHttp isn't void
	if (xmlHttp){
	// try to connect to the server
		try{
			// initiate reading a file from the server
			id = document.getElementById(idname).value;
			urls = "getoperators.php?id=" + id;
			xmlHttp.open("GET", urls , true);
			xmlHttp.onreadystatechange = op_StateChange;
			xmlHttp.send(null);
		}catch (e){
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}


// function called when the state of the HTTP request changes
function op_StateChange(){
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4){
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200){
			try{
				// do something with the response from the server
				getOp_Response();
			}catch(e){
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		} else{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function getOp_Response(){
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	responseArray = xmlRoot.getElementsByTagName("option");
	if(document.getElementById)
	var rem = document.getElementById('selectOps');
	var browserName=navigator.appName;
	var newone; 
	rem.innerHTML= '';
  	for (var i=0; i<responseArray.length; i++){
		text = responseArray.item(i).firstChild.data;
		val = responseArray[i].getAttribute('value');
		if (browserName=="Netscape"){ 
 			rem.innerHTML +="<option value='" + val + "'>" + text + "</option>";
		}else{ 
 			if (browserName=="Microsoft Internet Explorer"){
			newone = new Option(text,val);
			rem.add(newone,1);
 			} else {
				return false;
   			}
		 }
	}
}

/////////////////////////////////////////////////////////////////////getOperators()
// read a file from the server
function getOperators2(idname){
	// only continue if xmlHttp isn't void
	if (xmlHttp){
	// try to connect to the server
		try{
			// initiate reading a file from the server
			id = document.getElementById(idname).value;
			urls = "getoperators.php?id2=" + id;
			xmlHttp.open("GET", urls , true);
			xmlHttp.onreadystatechange = op2_StateChange;
			xmlHttp.send(null);
		}catch (e){
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}


// function called when the state of the HTTP request changes
function op2_StateChange(){
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4){
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200){
			try{
				// do something with the response from the server
				getOp2_Response();
			}catch(e){
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		} else{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function getOp2_Response(){
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	responseArray = xmlRoot.getElementsByTagName("option");
	if(document.getElementById)
	var rem = document.getElementById('inputNumber');
	var browserName=navigator.appName;
		text = responseArray.item(0).firstChild.data;
		if(text != '-')
 			rem.value = text;
 		else
 			rem.value = '';
}

/////////////////////////////////////////////////////////////////////getBookmark()
// read a file from the server
function getBookmark(idname){
	// only continue if xmlHttp isn't void
	if (xmlHttp){
	// try to connect to the server
		try{
			// initiate reading a file from the server
			id = document.getElementById(idname).value;
			urls = "getoperators.php?id3=" + id;
			xmlHttp.open("GET", urls , true);
			xmlHttp.onreadystatechange = ob_StateChange;
			xmlHttp.send(null);
		}catch (e){
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}


// function called when the state of the HTTP request changes
function ob_StateChange(){
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4){
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200){
			try{
				// do something with the response from the server
				getOb_Response();
			}catch(e){
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		} else{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function getOb_Response(){
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	responseArray = xmlRoot.getElementsByTagName("option");
	if(document.getElementById)
	var rem = document.getElementById('inputNumber');
	var browserName=navigator.appName;
		text = responseArray.item(0).firstChild.data;
		if(text != '-')
 			rem.value = text;
 		else
 			rem.value = '';
}
//====================================================================== service 

function validData(){
	// only continue if xmlHttp isn't void
	if (xmlHttp){
	// try to connect to the server
		try{
			// initiate reading a file from the server
			urls = "getdata.php";
			xmlHttp.open("GET", urls , true);
			xmlHttp.onreadystatechange = validData_StateChange;
			xmlHttp.send(null);
		}catch (e){
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}


// function called when the state of the HTTP request changes
function validData_StateChange(){
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4){
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200){
			try{
				// do something with the response from the server
				validData_Response();
			}catch(e){
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		} else{
			// display status message
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function validData_Response(){
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	responseArray = xmlRoot.getElementsByTagName("option");
	var broi = document.getElementById('sentm');
	var text = responseArray.item(0).firstChild.data;
 	if(parseInt(broi.value) % 20 == 0){
 		document.getElementById('dataname').innerHTML = '';
 	}
 	document.getElementById('dataname').innerHTML += "<br />" + text;
 	broi.value = parseInt(broi.value) + 1;
}

//====================================================================== form validate

function upperMe(form,localME) {
  var inputStr = form.inputMessage.value;
  var fromStr = form.sender.value;
  var fromlength = fromStr.length;
  if (fromlength == 0)
    strlength = inputStr.length;
  else
    strlength = inputStr.length + fromlength + 6;
  if (strlength > 120 )
    if (fromlength == 0)
       form.inputMessage.value = inputStr.substring(0,120);
    else
       form.inputMessage.value = inputStr.substring(0,120 - fromlength - 6);
  if (fromlength == 0)
    form.num.value = 120 - form.inputMessage.value.length;
  else
    form.num.value = 120 - form.inputMessage.value.length - fromlength - 6;
  localME.focus();
}

function put_it(tt){
	var form = document.SMForm;
	 var inputStr = form.inputMessage.value;
	form.inputMessage.value = inputStr + ' ' + tt + ' '; 
	
	 var inputStr = form.inputMessage.value;
  var fromStr = form.sender.value;
  var fromlength = fromStr.length;
  if (fromlength == 0)
    strlength = inputStr.length;
  else
    strlength = inputStr.length + fromlength + 6;
  if (strlength > 120 )
    if (fromlength == 0)
       form.inputMessage.value = inputStr.substring(0,120);
    else
       form.inputMessage.value = inputStr.substring(0,120 - fromlength - 6);
  if (fromlength == 0)
    form.num.value = 120 - form.inputMessage.value.length;
  else
    form.num.value = 120 - form.inputMessage.value.length - fromlength - 6;
}

function reloadData(aID){
	var rem = document.getElementById(aID);
	var stringArray = rem.value;
	stringArray = stringArray.split('_'); 
	window.location.href = 'index.php?cID=' + stringArray[0] + '&oID=' + stringArray[1];
}

function clearop(){
	document.getElementById('selectBookmark').selectedIndex='0';
	document.getElementById('inputNumber').value ='';
	
}

function clearop2(){
	document.getElementById('selectBookmark').selectedIndex='0';
}

function validateForm(oForm){
	var errdiv = document.getElementById('errdiv');
 	//oForm refers to the form which you want to validate
 	oForm.onsubmit = function(){
 		var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 		var regex2 = /^([a-zA-Z0-9_\.\-])+$/;
 		var regex3 = /^([a-zA-Zà-ÿÀ-ß0-9])+$/;
 		if(oForm.elements['inputEmail'].value.length<1){
 			errdiv.innerHTML = "You cannot leave the email field empty";
 			return false;
 		} else if(!regex.test(oForm.elements['inputEmail'].value)){
 			errdiv.innerHTML = "Invalid email address format";
 			return false;
 		}
 		
 		if(oForm.elements['inputPass1'].value.length<1 || oForm.elements['inputPass2'].value.length<1){
 			errdiv.innerHTML = "You cannot leave the passwords fields empty";
 			return false;
 		} else if(!regex2.test(oForm.elements['inputPass1'].value) || !regex2.test(oForm.elements['inputPass2'].value)){
 			errdiv.innerHTML = "Password must contain only Letters, Numbers, '_', '.' and '-'";
 			return false;
 		} else if(oForm.elements['inputPass1'].value != oForm.elements['inputPass2'].value){
 			errdiv.innerHTML = "Password and confirm Password does not match!";
 			return false;
 		}
 		
 		if(oForm.elements['inputNickname'].value.length<1){
 			errdiv.innerHTML = "You cannot leave the Nickname field empty";
 			return false;
 		} else if(!regex3.test(oForm.elements['inputNickname'].value)){
 			errdiv.innerHTML = "NickName must contain only Letters and Numbers";
 			return false;
 		}
 	
 		if(oForm.elements['inputCountry'].selectedIndex == 0){
 			errdiv.innerHTML = "You must choose Country";
 			return false;
 		} else {
 		
 		}
 		
 		return true;
 	}
 }

function loopData(){
	validData();
	setTimeout('loopData()', 3000);
}

function pause(millisecondi){
	var now = new Date();
	var exitTime = now.getTime() + millisecondi;
	while(true){
		now = new Date();
		if(now.getTime() > exitTime) return;
	}
}