// Functions used in new user registration (addRecord)

registerNS("UserReg");

UserReg.updateCountry = function(srce, dst) {
		
	var sel = document.getElementById(srce);
	var tag = document.getElementById(dst);
	
	if(sel.value > 0) {
		tag.innerHTML = "(" + sel.options[sel.selectedIndex].text + ")";
	} else {
		tag.innerHTML = "(please select your country above)";
	}
	
}


UserReg.checkEmail = function(emailBox){
	
	if($F('originalEmail') == $F('email1')){
		return;
	}

	var url = xmlurl + '/checkUserEmail.php';
	var pars = 'email=' + emailBox.value;
	
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: UserReg.warnEmail
			});
}


UserReg.warnEmail = function(originalRequest){

	//alert(originalRequest.responseText);
	var xml = originalRequest.responseXML.documentElement;
	
	if(xml.getElementsByTagName('id_ind')[0] && xml.getElementsByTagName('id_ind')[0].firstChild.data != $F('id_ind')) {
		
		var popupText = "There is already a user called " + xml.getElementsByTagName('fname')[0].firstChild.data + " " + xml.getElementsByTagName('sname')[0].firstChild.data + " using this email address!\n";
		
		if($F('id_ind')){ // We are editing, not adding a new user
			popupText += "If this is the person you are trying to add, click OK to view and edit the existing information.\n" +
									 "If this is not the person you are trying to add, click Cancel to continue entering information.\n";
		} else {
			popupText += "Click OK to view and edit this user's information.\n" +
									 "Click Cancel to continue entering information.\n";
		}
		
		//if(!popup){
		//	popupText += "\n(Note that if you click OK a new window is opened, which may be blocked by your browser. If the window doesn't appear, click on the link below the email address)";
		//}
		
		if(confirm(popupText)) {
			//window.location = reviewurl + xml.getElementsByTagName('id_ind')[0].firstChild.data;
			submitForm(xml.getElementsByTagName('id_ind')[0].firstChild.data);
		}
		
		// Add an error under the email address box saying the same thing
		var errordiv = document.getElementById('emailerror');
		var errorHTML = "Someone else is already using this email address. <a ";
		//if(!popup) errorHTML +=	"target='reviewwin' ";
		errorHTML += "href='#' onClick='submitForm(\"" + xml.getElementsByTagName('id_ind')[0].firstChild.data + "\")'>[View their details]</a>";
		errordiv.innerHTML = errorHTML;
		errordiv.style.display = 'block';
		

	}
	
}

UserReg.checkName = function(){
	
	if(!UserReg.isNew) return;
	
	if($F('fname') == '' || $F('sname') == ''){
		return;
	}

	var url = xmlurl + '/checkUserName.php';
	var pars = 'fname=' + $F('fname') + '&sname=' + $F('sname');
	
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: UserReg.warnName
			});
}



UserReg.warnName = function(originalRequest){

	var xml = originalRequest.responseXML.documentElement;
	
	var numMatches = xml.getElementsByTagName('num_matches')[0].firstChild.data;
	
	if(numMatches == 0) return;
	
	var output;
	
	if(numMatches == 1)
		output = "There is already someone ";
	else
		output = "There are already " + numMatches + " people ";
	
	output += "in the system with this name! "
	
	if(numMatches == 1){
		output += "Could this be the person you were trying to add? "
		output += "<a style='cursor:pointer' onclick='submitForm(\"" + xml.getElementsByTagName('id_ind')[0].firstChild.data + "\")'>[View the details of " +
							xml.getElementsByTagName('fname')[0].firstChild.data + " " + xml.getElementsByTagName('sname')[0].firstChild.data	+ "]</a>";
	} else {
		output += "Is one of them the person you were trying to add? "
		for(var i = 0; i < numMatches; i++){
			output += "<br /><a style='cursor:pointer' onclick='submitForm(\"" + xml.getElementsByTagName('id_ind')[i].firstChild.data + "\")'>[View Details of "+
			          xml.getElementsByTagName('fname')[i].firstChild.data + " " + xml.getElementsByTagName('sname')[i].firstChild.data + " " + (i+1) + "]</a>";
		}
	}
		
		
		
	var errordiv = document.getElementById('nameWarning');
	errordiv.innerHTML = output;
	errordiv.style.display = 'block';
	
}



// Sets the innerHTML of the passed element to be a non-breaking space and hides the element.
// Used to get rid of error messages when the corresponding field is modified
UserReg.clearContents = function(elementid){
	var elem = document.getElementById(elementid);
	elem.innerHTML = "&nbsp;";
	elem.style.display = 'none';
}
