var subindustries = new Array(
	new Array("(please select industry)"),
	new Array("no answer", "Dairy", "Farming", "Fishery", "Ranching"),
	new Array("no answer", "Arts and Crafts", "Design", "Fine Art", "Motion Pictures", "Museums and Institutions", "Music", "Performing Arts"),
	new Array("no answer", "Architecture \x26 Planning", "Building Materials", "Civil Engineering", "Construction"),
	new Array("no answer", "Apparel", "Consumer Electronics", "Consumer Goods", "Cosmetics", "Food Production", "Furniture", "Retail", "Sporting Goods", "Supermarkets", "Tobacco"),
	new Array("no answer", "Management Consulting", "Market Research", "Marketing and Advertising", "Professional Training \x26 Coaching", "Public Relations and Communications", "Staffing and Recruiting"),
	new Array("no answer", "Education Management", "Higher Education", "Primary\x2FSecondary Education"),
	new Array("no answer", "Accounting", "Banking", "Financial Services", "Insurance", "Investment Banking", "Investment Management", "Real Estate", "Venture Capital \x26 Private Equity"),
	new Array("no answer", "Executive Office", "Government Administration", "International Affairs", "Judiciary", "Law Enforcement", "Legislative Office", "Military", "Political Organization", "Public Policy", "Public Safety"),
	new Array("no answer", "Computer Hardware", "Computer Networking", "Computer Software", "Defense \x26 Space", "Information Technology and Services", "Internet", "Nanotechnology", "Semiconductors", "Telecommunications"),
	new Array("no answer", "Law Practice", "Legal Services"),
	new Array("no answer", "Automotive", "Aviation", "Chemicals", "Electrical\x2FElectronic Manufacturing", "Machinery", "Mining \x26 Metals", "Oil \x26 Energy", "Paper \x26 Forest Products", "Railroad Manufacture", "Shipbuilding", "Textiles", "Utilities"),
	new Array("no answer", "Broadcast Media", "Newspapers", "Online Media", "Printing", "Publishing", "Writing and Editing"),
	new Array("no answer", "Biotechnology", "Hospital \x26 Health Care", "Medical Equipment", "Medical Practice", "Pharmaceuticals", "Veterinary"),
	new Array("no answer", "Fund-Raising", "Non-Profit Management", "Program Development"),
	new Array("no answer", "Computer Games", "Entertainment", "Events Services", "Food \x26 Beverages", "Gambling \x26 Casinos", "Hospitality", "Leisure \x26 Travel", "Recreational Facilities and Services", "Restaurants", "Sports"),
	new Array("no answer", "Scientific Research"),
	new Array("no answer", "Civic \x26 Social Organization", "Consumer Services", "Environmental Services", "Individual \x26 Family Services", "Information Services", "Libraries", "Religious Institutions", "Translation"),
	new Array("no answer", "Airlines\x2FAviation", "Logistics and Supply Chain", "Maritime", "Package\x2FFreight Delivery", "Transportation\x2FTrucking\x2FRailroad", "Warehousing"));

function updateSubIndustry(category, selectedIndex, subindustries) {
	var profForm = document.getElementById('personalInfo');
	var valueArray = subindustries[category];
	profForm.subindustry.options[0] = null;
	for (i=0; i < valueArray.length; i++){
		profForm.subindustry.options[i] = new Option(valueArray[i]);
	}
	//populateDropDown(document.professionalForm.subindustry,subindustries[category], selectedIndex);
}

function accoChange(){
	var form = document.getElementById('hbtiInfo');
	if (form.acco[0].checked){
		renderSubDiv();
	}
	else{
		var bHostel= document.getElementById('bHostels');
		var gHostel= document.getElementById('gHostels');	
		bHostel.style.display = 'none';
		gHostel.style.display = 'none';
	}
	
}

function renderSubDiv(){
	var bHostel= document.getElementById('bHostels');
	var gHostel= document.getElementById('gHostels');
	if(isMale()){
		bHostel.style.display = 'inline';
		gHostel.style.display = 'none';
	}
	else{
		bHostel.style.display = 'none';
		gHostel.style.display = 'inline';
	}
}

function renderNext(index){
	if (validate(index)){
		if (index !=0)
		{
			var reg_information = document.getElementById('reg_information_' + index);
			reg_information.style.display = 'none';
			var next_reg_information = document.getElementById('reg_information_' + (index + 1));
			next_reg_information.style.display = 'inline';
		}
		if(index == 0){	
			updateSubIndustry(17, 0, subindustries)
		}
		if(index == 1){
			renderSubDiv();
		}
	}
	accoChange();
}

function renderPrevious(index){
	var reg_information = document.getElementById('reg_information_' + index);
	reg_information.style.display = 'none';
	var next_reg_information = document.getElementById('reg_information_' + (index - 1));
	next_reg_information.style.display = 'inline';
	if(index == 3){
		if(isMale()){
			renderSubDiv();
		}
	}
	accoChange();
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function isMale(){
	var form = document.getElementById('personalInfo');
	if (form.gender[0].checked)
		return true;
	else
		return false;
}

function validate(index){
	if (index == 0)
		return validateAccountInformation();
	else if(index == 1)	
	 	return validatePersonalInformation();
	else if(index == 2)
		return validateHBTIInformation();
	else 
		return false;
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function validateAccountInformation(){
	var form = document.getElementById('accountInfo');
	if (trim(form.firstName.value) == ""){
		 alert("Please enter a value for the \"First Name\" field.");
		 return false;
	}
	if (trim(form.lastName.value) == ""){
		 alert("Please enter a value for the \"Last Name\" field.");
		 return false;
	}
	if (trim(form.email.value) == ""){
		 alert("Please enter a value for the \"Email\" field.");
		 return false;
	}
	if (!isValidEmail(form.email.value, true)){
		alert("Please enter a valid \"Email\"");
		 return false;
	}
	if (trim(form.password.value) == ""){
		 alert("Please enter a value for the \"Password\" field.");
		 return false;
	}	
	
	if (form.password.value.length < 4 ||  form.password.value.length > 8){
		 alert("Password length should not be greated than 8 chars and not less than 4 chars.");
		 return false;
	}	
	if (form.password.value != form.repassword.value){
		 alert("Passwords are not matching. Please enter again.");
		 return false;
	}
	if (isUserExists()){
		 return false;
	}
	return true;
}

function isUserExists(){
	var form = document.getElementById('accountInfo');
	// Check is account email already exist
	$.getJSON("process.check.registration.php",{ email: form.email.value}, function(data){
		if (data.result == 'true'){
			alert("User already exist. Go to login page for signing-in.");	
			return true;
		}
		else{
			var reg_information = document.getElementById('reg_information_' + 0);
			reg_information.style.display = 'none';
			var next_reg_information = document.getElementById('reg_information_' + 1);
			next_reg_information.style.display = 'inline';
		}
	});
	
}

function validatePersonalInformation(){
	var form = document.getElementById('personalInfo');
	if (trim(form.city.value) == ""){
		 alert("Please enter a value for the \"City\" field.");
		 return false;
	}
	if (trim(form.country.value) == ""){
		 alert("Please enter a value for the \"Country\" field.");
		 return false;
	}
	if (trim(form.hometown.value) == ""){
		 alert("Please enter a value for the \"Home Town\" field.");
		 return false;
	}
	if (trim(form.companyName.value) == ""){
		 alert("Please enter a value for the \"Company Name\" field.");
		 return false;
	}
	return true;
}

function validateHBTIInformation(){
	var form = document.getElementById('hbtiInfo');
	return true;
}

function submitAll(){
	if(validateHBTIInformation()){
		var reg_information = document.getElementById('reg_information_2');
		// Make a div for loading... with gif image
		
		//Post for registration
		var accountForm = document.getElementById('accountInfo');
		var personalForm = document.getElementById('personalInfo');
		var hbtiForm = document.getElementById('hbtiInfo');
		
		var firstYearHostel;
		var firstYearHostelRoom;
		var secondYearHostel;
		var secondYearHostelRoom;
		var thirdYearHostel;
		var thirdYearHostelRoom;
		var finalYearHostel;
		var finalYearHostelRoom;
		var gender;
		
		for (i=personalForm.gender.length-1; i > -1; i--) {
			if (personalForm.gender[i].checked) {
				gender = personalForm.gender[i].value;
			}
		}
		if (gender == 'Male'){
			for (i=hbtiForm.fyhostel.length-1; i > -1; i--) {
				if (hbtiForm.fyhostel[i].checked) {
					firstYearHostel = hbtiForm.fyhostel[i].value;
				}
			}
			firstYearHostelRoom=hbtiForm.fyhostelRoomnumber.value;
			for (i=hbtiForm.syhostel.length-1; i > -1; i--) {
				if (hbtiForm.syhostel[i].checked) {
					secondYearHostel = hbtiForm.syhostel[i].value;
				}
			}
			secondYearHostelRoom=hbtiForm.syhostelRoomnumber.value;
			for (i=hbtiForm.tyhostel.length-1; i > -1; i--) {
				if (hbtiForm.tyhostel[i].checked) {
					thirdYearHostel = hbtiForm.tyhostel[i].value;
				}
			}
			thirdYearHostelRoom=hbtiForm.tyhostelRoomnumber.value;
			for (i=hbtiForm.finalyhostel.length-1; i > -1; i--) {
				if (hbtiForm.finalyhostel[i].checked) {
					finalYearHostel = hbtiForm.finalyhostel[i].value;
				}
			}
			finalYearHostelRoom=hbtiForm.finalyhostelRoomnumber.value;
		}
		else{
			firstYearHostel='GH';
			firstYearHostelRoom=hbtiForm.ghfirstyearRoomnumber.value;
			secondYearHostel='GH';
			secondYearHostelRoom=hbtiForm.ghsecondyearRoomnumber.value;
			thirdYearHostel='GH';
			thirdYearHostelRoom=hbtiForm.ghthirdyearRoomnumber.value;
			finalYearHostel='GH';
			finalYearHostelRoom=hbtiForm.ghfinalyearRoomnumber.value;
		}
		
		$.ajax({
			type: "POST",
			url: "process.registration.php",
			data: {
				firstname: accountForm.firstName.value, 
				lastname: accountForm.lastName.value, 
				nickname: accountForm.nickName.value, 
				email: accountForm.email.value, 
				password: accountForm.password.value, 
				gender: gender, 
				relation_status: personalForm.relation_status.value, 
				birth_date: personalForm.birth_date.value, 
				birth_month: personalForm.birth_month.value, 
				birth_year: personalForm.birth_year.value, 
				city: personalForm.city.value, 
				country: personalForm.country.value, 
				hometown: personalForm.hometown.value, 
				phone: personalForm.phone.value, 
				industry: personalForm.industry.value, 
				subindustry: personalForm.subindustry.value, 
				companyName: personalForm.companyName.value, 
				batch: hbtiForm.batch.value, 
				branch: hbtiForm.branch.value, 
				srNumber: hbtiForm.srNumber.value, 
				acco: hbtiForm.acco.value, 
				fyHostel: firstYearHostel, 
				fyHostelRoom: firstYearHostelRoom, 
				syHostel: secondYearHostel, 
				syHostelRoom: secondYearHostelRoom, 
				tyHostel: thirdYearHostel, 
				tyHostelRoom: thirdYearHostelRoom, 
				fiyHostel: finalYearHostel,
				fiyHostelRoom: finalYearHostelRoom
			},
			dataType: "json",
			success: function(msg){
				if(msg && msg.result && msg.result=="success"){
					var reg_done = document.getElementById('registration_complete');
					reg_information.style.display = 'none';
					reg_done.style.display = 'inline'; 
				}
				else {
					
				}
			},
			error: function(msg){
				var reg_error = document.getElementById('registration_error');
				reg_information.style.display = 'none';
				reg_error.style.display = 'inline'; 
			}
		});
		/*
		$.post("process.registration.php", 
			   { firstname: accountForm.firstName.value, lastname: accountForm.lastName.value, nickname: accountForm.nickName.value, email: accountForm.email.value, password: accountForm.password.value, gender: gender, relation_status: personalForm.relation_status.value, birth_date: personalForm.birth_date.value, birth_month: personalForm.birth_month.value, birth_year: personalForm.birth_year.value, city: personalForm.city.value, country: personalForm.country.value, hometown: personalForm.hometown.value, phone: personalForm.phone.value, industry: personalForm.industry.value, subindustry: personalForm.subindustry.value, companyName: personalForm.companyName.value, batch: hbtiForm.batch.value, branch: hbtiForm.branch.value, srNumber: hbtiForm.srNumber.value, acco: hbtiForm.acco.value, fyHostel: firstYearHostel, fyHostelRoom: firstYearHostelRoom, syHostel: secondYearHostel, syHostelRoom: secondYearHostelRoom, tyHostel: thirdYearHostel, tyHostelRoom: thirdYearHostelRoom, fiyHostel: finalYearHostel,fiyHostelRoom: finalYearHostelRoom },
			   function(data){
			   
		});
		
		var reg_done = document.getElementById('registration_complete');
		reg_information.style.display = 'none';
		reg_done.style.display = 'inline'; 
		*/
	}
}