// JavaScript Document
function trim(str){
   return str.replace(/^\s+|\s+$/g,'');
}

function clearText(objID,text){							
		if(document.getElementById(objID).value == text){
			document.getElementById(objID).value = "";										
		}				
	}
	function fillText(objID,text){
		if(document.getElementById(objID).value == ""){
		document.getElementById(objID).value = text;				
		}
	}
	function GetXmlHttpObject(){
		var xmlHttp=null;
		try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
		catch (e){
		//Internet Explorer
		try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		} 
		return xmlHttp;
	}

	function makeVisible(){document.getElementById('back_div').style.display = 'block';document.getElementById('joinBox').style.display = 'block';}
	function makeInVisible(){document.getElementById('joinBox').style.display = 'none';document.getElementById('back_div').style.display = 'none';}
	
	function validateFields(){
	if(document.getElementById("display_name").value == "" || trim(document.getElementById("display_name").value) == 'Name'){
		document.getElementById("ajax_spinner").innerHTML = "Please enter your name.";
		document.getElementById("display_name").focus();return false;}
	else{
		var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		var checkStr = document.getElementById("display_name").value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if(j == checkOK.length){
		allValid = false;
		break;
		}
		}
		if (!allValid){ 
			document.getElementById("ajax_spinner").innerHTML = "Name is invalid.";
			document.getElementById("name").value = "";
			document.getElementById("name").focus();							
			return (false);
		}
	}	
	if(document.getElementById("signup_email").value == "" || trim(document.getElementById("signup_email").value) == 'Email'){
		document.getElementById("ajax_spinner").innerHTML = "Please enter your email.";document.getElementById("signup_email").focus();return false;}
	if(document.getElementById("signup_password").value == ""){
		document.getElementById("ajax_spinner").innerHTML = "Please enter your password";document.getElementById("signup_password").focus();return false;}
	/*if(document.getElementById("signup_username").value == "" || trim(document.getElementById("signup_username").value) == 'Username'){
		document.getElementById("ajax_spinner").innerHTML = "Please enter your username";document.getElementById("signup_username").focus();return false;}
	if(document.getElementById("state_list").value == "none"){
		document.getElementById("ajax_spinner").innerHTML = "Please select your state";document.getElementById("state_list").focus();return false;}	*/		
	regUser();			
}
function selectCity(){
	xmlHttp=GetXmlHttpObject();	
	if(xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
	}
	stateid = document.getElementById("state_list").value;
	url="show_city_party.php?stateid="+stateid+"&mode=city";
	//alert("url="+url);
	xmlHttp.onreadystatechange = stateChangedPlaylist;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChangedPlaylist(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var response_string = xmlHttp.responseText;
		var responseArray = response_string.split('$');
		var cityArray = responseArray[0].split('@');
		
		if(cityArray != ""){
			size = cityArray.length;
			var cityListString = "";
			for(var i =0;i<size;i++){
				var cityid = cityArray[i].split('#'); 
				cityListString = cityListString+"<option value ="+cityid[0]+">"+cityid[1]+"</option>";
			}
		}else{
			cityListString = "blank";
		}
		
		if(cityListString != 'blank'){				
			
			document.getElementById("cityList1").style.display = 'block';
			document.getElementById("cityList1").innerHTML = "<select name='city' id='city' onchange='javascript:selectSubCity();'>"+cityListString+"</select>";
		}else{
			
			document.getElementById("cityList1").style.display = 'none';
			document.getElementById("cityList1").innerHTML = "";
		}				
		document.getElementById("subCityList1").style.display = 'none';
		document.getElementById("subCityList1").innerHTML = " ";		
	}
}
function selectSubCity(){
	xmlHttp=GetXmlHttpObject();
	
	if(xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
	}
	cityid = document.getElementById("city").value;
	url="show_city_party.php?cityid="+cityid+"&mode=subcity";
	//alert("url="+url);
	xmlHttp.onreadystatechange = cityChangedList;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function cityChangedList(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var response_string = xmlHttp.responseText;
		var cityArray = response_string.split('@');
		
		if(cityArray != ""){
			size = cityArray.length;
			var cityListString = "";
			for(var i =0;i<size;i++){
				var cityid = cityArray[i].split('#'); 
				cityListString = cityListString+"<option value ="+cityid[0]+">"+cityid[1]+"</option>";
			}
			document.getElementById("subCityList1").style.display = 'block';
			document.getElementById("subCityList1").innerHTML = "<select id='sub_city' name='sub_city'>"+cityListString+"</select>";
		}else{
			document.getElementById("subCityList1").style.display = 'none';
			document.getElementById("subCityList1").innerHTML = " ";
		}			
	}
}	
function regUser(){
	document.getElementById("ajax_spinner").innerHTML="<img src='images/ajaxSpinner.gif'>";
	xmlHttp=GetXmlHttpObject();	
	if(xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}	
	var dname = document.getElementById("display_name").value;
	var uemail = document.getElementById("signup_email").value;
	var password = document.getElementById("signup_password").value;
	
	if(document.getElementById("signup_agree").checked){
		var terms = document.getElementById("signup_agree").value;
	}
	//var state = document.getElementById("state_list").value;
	var state = "";	
	if(document.getElementById("city")){var city = document.getElementById("city").value;}
	
	if(document.getElementById("referer")){var referer = document.getElementById("referer").value;}	
	
	if(document.getElementById("sub_city")){var sub_city = document.getElementById("sub_city").value;}	
		
	url="new_signup.php?state="+state+"&city="+city+"&sub_city="+sub_city+"&dname="+dname+"&signup_email="+uemail+"&signup_password="+password+"&signup_agree="+terms+"&signup_referer="+referer;	
	//alert(url);
	xmlHttp.onreadystatechange = stateChangedReg;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChangedReg(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){		
	var res = xmlHttp.responseText;
	if(res == ""){
		loc= "http://www.awakind.com/home.php";
		window.location ="home.php";}
	else{
		document.getElementById("ajax_spinner").innerHTML="";
		//alert(res);
		document.getElementById("ajax_spinner").innerHTML = res;
	}							
}
}	
