//Functions in this file:
//closeSignUpWindow- closes the sign up modal window and clears all values
//validateSignUp- validates the info submitted for sign up
//processCaptcha- validates captcha and submits user info to the database, then sends a validation email

//START CLOSE SIGN UP WINDOW
function closeSignInErrorWindow(){
document.getElementById('modalError').style.display='none';	
}
function closeEmailError(){
document.getElementById('emailErrorModal').style.display='none';
}

function closeSignUpWindow(){
document.getElementById('signUpModal').style.display='none';
document.getElementById('signUpModalContent').style.display='';
document.getElementById('signUpErrorLoader').style.display='none';
document.getElementById('signUpErrorIn').innerHTML='';
document.getElementById('signUpErrorContainer').style.display='none';
document.signUp.firstName.value = '';
document.signUp.firstName.style.backgroundImage = '';
document.signUp.lastName.value = '';
document.signUp.lastName.style.backgroundImage = '';
document.signUp.emailSignUp.value = 'We Hate SPAM Too!';
document.signUp.emailSignUp.style.backgroundImage = '';
document.signUp.emailSignUp.style.color = '#999999';
document.signUp.passwordSignUp.value = 'password';
document.signUp.passwordSignUp.style.backgroundImage = '';
document.signUp.passwordSignUp.style.color = '#999999';
document.signUp.comfirmPasswordSignUp.value = 'password';
document.signUp.comfirmPasswordSignUp.style.backgroundImage = '';
document.signUp.comfirmPasswordSignUp.style.color = '#999999';
document.signUp.accept.checked = '';
document.signUp.recaptcha_response_field.value = '';
document.signUp.recaptcha_response_field.style.backgroundImage = '';
}
//END CLOSE SIGN UP WINDOW

//START SIGN UP FUNCTION
function validateSignUp(){

document.getElementById('signUpModalContent').style.display='none';
document.getElementById('signUpErrorContainer').style.display='none';
document.getElementById('signUpErrorLoader').style.display='';

setTimeout(function(){
var signUpVar = new signUp;
//Define variables
var firstName = document.signUp.firstName.value;
var lastName = document.signUp.lastName.value;
var email = document.signUp.emailSignUp.value;
var password = document.signUp.passwordSignUp.value;
var confirmPassword = document.signUp.comfirmPasswordSignUp.value;
var accept = document.signUp.accept.checked;
var communityId = document.signUp.communityId.value;
var signUpReturn = signUpVar.validateSignUp(firstName,lastName,email,password,confirmPassword,accept);

if(signUpReturn[0] != false){
	if(signUpReturn[0] != 'email exists'){
		document.getElementById('signUpErrorIn').innerHTML=signUpReturn[1];
		document.getElementById('signUpErrorContainer').style.display='';
	}else{
		closeSignUpWindow();
		document.getElementById('emailErrorModal').style.display='';
	}
}else{
processCaptcha(firstName,lastName,email,password,confirmPassword,communityId);
}

document.getElementById('signUpModalContent').style.display='';
document.getElementById('signUpErrorLoader').style.display='none';

//START change textbox background for errors
document.signUp.firstName.style.backgroundImage = '';
document.signUp.lastName.style.backgroundImage = '';
document.signUp.emailSignUp.style.backgroundImage = '';
document.signUp.passwordSignUp.style.backgroundImage = '';
document.signUp.comfirmPasswordSignUp.value='';
document.signUp.recaptcha_response_field.style.backgroundImage = '';

if(signUpReturn[0] == 'firstName'){
document.signUp.firstName.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}else if(signUpReturn[0] == 'lastName'){
document.signUp.lastName.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}else if(signUpReturn[0] == 'email'){
document.signUp.emailSignUp.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}else if(signUpReturn[0] == 'password'){
document.signUp.passwordSignUp.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}else if(signUpReturn[0] == 'confirm password'){
document.signUp.comfirmPasswordSignUp.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}
//END change textbox background for errors
},500);
}
//END SIGN UP FUNCTION





//START CAPTCHA VALIDATION
function processCaptcha(firstName,lastName,email,password,confirmPassword,communityId){
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
var http = new ActiveXObject("Microsoft.XMLHTTP");
}else{
var http = new XMLHttpRequest();
}

process(firstName,lastName,email,password,confirmPassword,communityId);
var nocache = 0;

function process(){
var urlchallenge= Recaptcha.get_challenge();
var urlresponse= Recaptcha.get_response();
nocache = Math.random();
http.open('get', 'scripts/captcha.cfm?challenge='+urlchallenge+'&response='+urlresponse+'&nocache='+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}

function insertReply(){
if(http.readyState == 4){
var response = http.responseText;

var validCheck = response.match('security');
if (validCheck >= 0){
	//Submit sign up information to the database/send validation email
	var completeSignUpVar = new signUp;
	var completeSignUpReturn = completeSignUpVar.submitSignUp(firstName,lastName,email,password,communityId);
	displayMessage();
}else{
	//Return captcha error
	document.getElementById('signUpModalContent').style.display='';
	document.getElementById('signUpErrorLoader').style.display='none';
	
	Recaptcha.reload();
	document.getElementById('signUpErrorIn').innerHTML=response;
	document.getElementById('signUpErrorContainer').style.display='';
	document.signUp.recaptcha_response_field.style.backgroundImage = 'url("images/tbInvalidBg.png")';
}

}
}
}
//END CAPTCHA VALIDATION

//Start of display message after sign up
function displayMessage(){
	closeSignUpWindow();
	document.getElementById('displayMessageModal').style.display='';
}
function clearMessage(){
	document.getElementById('displayMessageModal').style.display='none';
}


function deleteProfileImage(userID){
	var profileImageVar = new profileImage;
	profileImageVar.removeProfileImage(userID);
	document.editImage.submit()
}




//START CHANGE PROFILE IMAGE
function changeProfileImage(){
document.getElementById('profileImageModalContent').style.display='none';
document.getElementById('profileImageErrorContainer').style.display='none';
document.getElementById('profileImageLoader').style.display='';
document.getElementById('profileImageUploadButtons').style.display='none';

var imageVal = document.getElementById('profileImageFrame').contentWindow.document.profileImageForm.newImage.value;

setTimeout(function(){
if(imageVal == ''){
document.getElementById('profileImageErrorIn').innerHTML='Please select an image to upload.';
document.getElementById('profileImageErrorContainer').style.display='';
document.getElementById('profileImageLoader').style.display='none';
document.getElementById('profileImageModalContent').style.display='';
document.getElementById('profileImageUploadButtons').style.display='';
}else{
document.getElementById('profileImageFrame').contentWindow.document.profileImageForm.submit();
}
},500);

}
//END CHANGE PROFILE IMAGE


function displayProfileImage(userID){
document.getElementById('profileImageModal').style.display='';
scroll(0,0);
document.getElementById('profileImageFrame').src='scripts/frameProfileImage.cfm?id='+userID;
}

function displayEditProfileImage(userID){
document.getElementById('profileImageModal').style.display='';
scroll(0,0);
document.getElementById('profileImageFrame').src='scripts/frameProfileImage.cfm?edit=yes&id='+userID;
}


function submitCropImage(){
	getFrameDocument(document.getElementById('profileImageFrame')).cropImage.submit()
}


function getFrameDocument (fram) {
 var d = (fram.contentDocument) ? fram.contentDocument : ((fram.contentWindow) ? fram.contentWindow.document : null);
 return d;}


 
//START CLOSE PROFILE MODAL
function closeProfileImageModal(userID){
document.getElementById('profileImageModal').style.display='none';
document.getElementById('profileImageModalContent').style.display='';
document.getElementById('profileImageErrorContainer').style.display='none';
document.getElementById('profileImageLoader').style.display='none';
document.getElementById('profileImageFrame').height='60';
document.getElementById('profileImageCropButtons').style.display='none';
document.getElementById('profileImageUploadButtons').style.display='';

document.getElementById('profileImageFrame').contentWindow.document.profileImageForm.reset();

if(userID != 0){
for(i=1;i<=4;i++){
document.getElementById('profileImageDisplay'+i).innerHTML='<img src="images/avatar.jpg" style="border:1px solid #9ea9b9;"/>';

//START Set values for the hidden image input fields
top.document.forms['addFanProfile'].image.value = 'images/avatar.jpg';
top.document.forms['addMusicianProfile'].image.value = 'images/avatar.jpg';
top.document.forms['addVenueProfile'].image.value = 'images/avatar.jpg';
top.document.forms['addPromoterProfile'].image.value = 'images/avatar.jpg';
//END Set values for the hidden image input fields
}

var profileImageVar = new profileImage;
profileImageVar.deleteProfileImage(userID);
document.getElementById('profileImageFrame').src='scripts/frameProfileImage.cfm?id='+userID;
}}
//END CLOSE PROFILE MODAL











//START COMPLETE SIGN UP
function completeSignUp(type,email){
document.forms['add'+type+'Profile'].gender.disabled = true;
document.forms['add'+type+'Profile'].year.disabled = true;
document.forms['add'+type+'Profile'].month.disabled = true;
document.forms['add'+type+'Profile'].day.disabled = true;
if(type != 'Fan'){ document.forms['add'+type+'Profile'].phone.disabled = true; }

document.forms['add'+type+'Profile'].name.disabled = true;

if(type == 'Venue'){
	document.forms['add'+type+'Profile'].longitude.disabled = true;
	document.forms['add'+type+'Profile'].latitude.disabled = true;
}

document.forms['add'+type+'Profile'].website.disabled = true;
document.forms['add'+type+'Profile'].country.disabled = true;
document.forms['add'+type+'Profile'].address.disabled = true;
document.forms['add'+type+'Profile'].city.disabled = true;
document.forms['add'+type+'Profile'].state.disabled = true;
document.forms['add'+type+'Profile'].regionProvince.disabled = true;
document.forms['add'+type+'Profile'].zip.disabled = true;
if(type != 'Musician'){document.forms['add'+type+'Profile'].description.disabled = true;}
document.forms['add'+type+'Profile'].submitButton.disabled = true;
document.getElementById(type.toLowerCase()+'ProfileImageButtons').style.display = 'none';
document.getElementById(type.toLowerCase()+'ProfileSubmitLoader').style.display = '';
var i=1;
if(type == 'Musician'){
for(i=1;i<=5;i++){ document.getElementById('instrument'+i).disabled = true; }
for(i=1;i<=5;i++){ document.getElementById('genre'+i).disabled = true; }
}
//Define variables
var image = document.forms['add'+type+'Profile'].image.value;
var gender = document.forms['add'+type+'Profile'].gender.value;
	var year = document.forms['add'+type+'Profile'].year.value;
	var month = document.forms['add'+type+'Profile'].month.value;
	var day = document.forms['add'+type+'Profile'].day.value;
var birthdate = month +"-"+ day +"-"+ year;

if(type != 'Fan'){ var phoneNum = document.forms['add'+type+'Profile'].phone.value; }else{ var phoneNum = ''; }
var name = document.forms['add'+type+'Profile'].name.value;

if(type == 'Venue'){
	var longitude = document.forms['add'+type+'Profile'].longitude.value;
	var latitude = document.forms['add'+type+'Profile'].latitude.value;
}else{
	var longitude = "";
	var latitude = "";
}
var communityId = document.forms['add'+type+'Profile'].communityId.value;
var website = document.forms['add'+type+'Profile'].website.value;
var country = document.forms['add'+type+'Profile'].country.value;
var address = document.forms['add'+type+'Profile'].address.value;
var city = document.forms['add'+type+'Profile'].city.value;
var state = document.forms['add'+type+'Profile'].state.value;
var regionProvince = document.forms['add'+type+'Profile'].regionProvince.value;
var zip = document.forms['add'+type+'Profile'].zip.value;
if(type != 'Musician'){
	var description = document.forms['add'+type+'Profile'].description.value;
}else{
	var description = ""
}
var userID = document.forms['add'+type+'Profile'].userID.value;
var userType = document.forms['add'+type+'Profile'].type.value;
var instrument=new Array();
var genre=new Array();
if(type == 'Musician'){for(i=1;i<=5;i++){ instrument[i]=document.getElementById('instrument'+i).value; }
for(i=1;i<=5;i++){ genre[i]=document.getElementById('genre'+i).value; }
}else{for(i=1;i<=5;i++){ instrument[i]=0; }
for(i=1;i<=5;i++){ genre[i]=0; }
}

//Ajax
setTimeout(function(){
var signUpVar = new signUp;
var signUpReturn = signUpVar.completeSignUp(image, name, gender, birthdate, phoneNum, website, country, address, city, state, regionProvince, zip, description, userID, userType, instrument[1], instrument[2], instrument[3], instrument[4], instrument[5], genre[1], genre[2], genre[3], genre[4], genre[5],longitude,latitude,communityId);

//Reset form label colors
if(type != 'Fan'){ document.getElementById(type.toLowerCase()+'NameLabel').style.color = '#333333'; }
document.getElementById(type.toLowerCase()+'GenderLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'BirthDateLabel').style.color = '#333333';
if(type != 'Fan'){ document.getElementById(type.toLowerCase()+'PhoneLabel').style.color = '#333333'; }
document.getElementById(type.toLowerCase()+'WebsiteLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'CountryLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'AddressLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'CityLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'StateLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'RegionProvinceLabel').style.color = '#333333';
document.getElementById(type.toLowerCase()+'PostalCodeLabel').style.color = '#333333';
document.getElementById('musicianInstrumentLabel').style.color = '#333333';
document.getElementById('musicianGenreLabel').style.color = '#333333';

//Error handling
if(signUpReturn[0] != false){
errorJS(signUpReturn[0],signUpReturn[1]);
var label = signUpReturn[0].split(' ').join('');
document.getElementById(type.toLowerCase() +''+ label +'Label').style.color = '#ff0000';

if(type != 'Fan'){ document.forms['add'+type+'Profile'].name.disabled = false; }
document.forms['add'+type+'Profile'].gender.disabled = false;
document.forms['add'+type+'Profile'].year.disabled = false;
document.forms['add'+type+'Profile'].month.disabled = false;
document.forms['add'+type+'Profile'].day.disabled = false;
if(type != 'Fan'){ document.forms['add'+type+'Profile'].phone.disabled = false; }
document.forms['add'+type+'Profile'].website.disabled = false;
document.forms['add'+type+'Profile'].country.disabled = false;
document.forms['add'+type+'Profile'].address.disabled = false;
document.forms['add'+type+'Profile'].city.disabled = false;
document.forms['add'+type+'Profile'].state.disabled = false;
document.forms['add'+type+'Profile'].regionProvince.disabled = false;
document.forms['add'+type+'Profile'].zip.disabled = false;
if(type != 'Musician'){document.forms['add'+type+'Profile'].description.disabled = false;}
document.forms['add'+type+'Profile'].submitButton.disabled = false;
document.getElementById(type.toLowerCase()+'ProfileImageButtons').style.display = '';
document.getElementById(type.toLowerCase()+'ProfileSubmitLoader').style.display = 'none';
if(type == 'Musician'){
for(i=1;i<=5;i++){ document.getElementById('instrument'+i).disabled = false; }
for(i=1;i<=5;i++){ document.getElementById('genre'+i).disabled = false; }
}
}else{

window.location='profile.cfm?id='+signUpReturn[1]+'&profile=yes';
}

},500);
}
//END COMPLETE SIGN UP


function resetPasssword(){
	var signUpVar = new signUp;
	document.getElementById('resetModalContent').style.display='none'
	document.getElementById('resetErrorLoader').style.display=''
	document.getElementById('resetErrorIn').innerHTML='';
	document.getElementById('resetErrorContainer').style.display='none';
	var email = document.resetPasswordForm.email.value;
	var pass = signUpVar.getPassword(email);
	if(pass[0] != false){
		document.getElementById('resetErrorIn').innerHTML=pass[1];
		document.getElementById('resetErrorContainer').style.display='';
		document.getElementById('resetModalContent').style.display=''
		document.getElementById('resetErrorLoader').style.display='none'
	}else{
		signUpVar.sendPasswordEmail(email,pass[1]);
		document.getElementById('resetModalContent').style.display=''
		document.getElementById('resetErrorLoader').style.display='none'
		document.getElementById('resetModal').style.display='none';
		document.getElementById('sendPasswordModal').style.display=''
	}

}

function sendLink(email){
	var signUpVar = new signUp;
	document.getElementById('signUpModalContent').style.display='none'
	document.getElementById('signUpErrorLoader').style.display=''
	document.getElementById('signUpErrorIn').innerHTML='';
	document.getElementById('signUpErrorContainer').style.display='none';
	signUpVar.sendAnotherLink(email);
	document.getElementById('signUpModalContent').style.display=''
	document.getElementById('signUpErrorLoader').style.display='none'
	document.getElementById('signUpModal').style.display='none'
	document.getElementById('displayMessageModal').style.display=''


}

