function expand(id,dsp) {
	var obj = document.getElementById(id);
	if(obj.style.display == 'none') {
		obj.style.display = dsp;
	} else {
		obj.style.display = 'none';
	}
}

function clearStatus(id) {
	document.getElementById('error_'+id).innerHTML = '';
}

function clearPageStatus() {
	document.getElementById('page_status').innerHTML = '';
}

function loadingIMG(status) {
	status.innerHTML = '<img src="images/loading.gif" />';
}

function check_passwords() {
	var pass1 = document.getElementById('pass1').value;
	var pass2 = document.getElementById('pass2').value;
	if(pass1.length < 5) {
		document.getElementById('error_pass').innerHTML = 'The password must contain at least 5 characters';
		return false;
	}
	if(pass1 != pass2) {
		document.getElementById('error_pass').innerHTML = 'Passwords don\'t match';
		return false;
	}
	document.getElementById('error_pass').innerHTML = '';
	return true;
}

function check_mail() {
	var e = document.getElementById('mail').value;
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			document.getElementById('error_mail').innerHTML = 'Invalid e-mail address';
			return false;
		}	
	} 
	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!e.match(re) && e.match(re_two)) {
		return true;		
	}
	document.getElementById('error_mail').innerHTML = 'Invalid e-mail address';
	return false;
}

function chPass() {
	loadingIMG(document.getElementById('error_pass'));
	var password = document.getElementById('pass1').value;
	AJAXRequest("POST","uedit.php?cmd=chpass&password="+password,"cmd=chpass&password="+password,processChPass,true);
}

function chMail() {
	loadingIMG(document.getElementById('error_mail'));
	var mail = document.getElementById('mail').value;
	AJAXRequest("POST","uedit.php?cmd=chmail&mail="+mail,"cmd=chmail&mail="+mail,processChMail,true);
}

function processChMail(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			document.getElementById('error_mail').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
	}
}

function processChPass(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			document.getElementById('error_pass').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
	}
}

function initProfileEdit(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			document.getElementById('status').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
		if(xmlObject.responseXML.getElementsByTagName('day')[0].firstChild.nodeValue) {
			document.getElementById('day').value = xmlObject.responseXML.getElementsByTagName('day')[0].firstChild.nodeValue;
			document.getElementById('month').value = xmlObject.responseXML.getElementsByTagName('month')[0].firstChild.nodeValue;
			document.getElementById('year').value = xmlObject.responseXML.getElementsByTagName('year')[0].firstChild.nodeValue;
			document.getElementById('loc').value = xmlObject.responseXML.getElementsByTagName('location')[0].firstChild.nodeValue;
			document.getElementById('about').value = xmlObject.responseXML.getElementsByTagName('about')[0].firstChild.nodeValue;
			for(i=0;i<document.getElementById('country').options.length;i++) {
				if(xmlObject.responseXML.getElementsByTagName('country')[0].firstChild.nodeValue == document.getElementById('country').options[i].value) {
					document.getElementById('country').selectedIndex = i;
				}
			}
		}
	}
}

function saveProfileEdit() {
	day=document.getElementById('day').value;
	month=document.getElementById('month').value;
	year=document.getElementById('year').value;
	loc=document.getElementById('loc').value;
	about=document.getElementById('about').value;
	country=document.getElementById('country').options[document.getElementById('country').selectedIndex].value;
	AJAXRequest("POST","uedit.php","cmd=savepe&day="+day+"&month="+month+"&year="+year+"&loc="+loc+"&about="+about+"&country="+country,saveProfileEditStatus,true);
}

function saveProfileEditStatus(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			document.getElementById('status').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
	}
}

function writeReviewStage1() {
	document.getElementById('cnt').innerHTML = '<h1>Write review ...</h1> \
							<ul> \
							<li><a href="/write_review/1">...on people</a></li> \
							<li><a href="/write_review/2">...on places</a></li> \
							<li><a href="/write_review/3">...on things</a></li> \
							</ul>';
}

function changeNotify() {
	var notify = 0;
	if(document.getElementById('notify').checked == true) {
		notify = 1;
	}
	AJAXRequest("POST","./uedit.php","cmd=notify&n="+notify,changeNotifyStatus,true);
}

function changeNotifyStatus(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			document.getElementById('error_mail').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
	}
}

