
var errorFlag = 'no';
var suHeight  = '1300px';
var saveDOB   = '';
var gAjaxFile = '';


/* ----------------------------------------- */
function submitProfileForm(ajaxFile, signupType, checkMode) {

	errorFlag = 'no';
	gAjaxFile = ajaxFile;
	
	// account
	if ( checkMode == 'all' || checkMode == 'ac' || checkMode == 'newAdmin' ) {
		if( document.signup.loginName.value.length < 1 ){
			document.getElementById('errLoginName').innerHTML = "Please enter your login name";document.signup.loginName.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if( document.signup.loginName.value.length > 64 ){
			document.getElementById('errLoginName').innerHTML = "Login name too long";document.signup.loginName.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if( document.signup.loginName.value.length < 6 ){
			document.getElementById('errLoginName').innerHTML = "Login name too short";document.signup.loginName.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else {
			document.getElementById('errLoginName').innerHTML = "";	suHeight  = '1150px';
		}

		document.getElementById('errPass1').innerHTML = "";
		document.getElementById('errPass2').innerHTML = "";
		if( document.signup.password.value.length < 1 ){
			document.getElementById('errPass1').innerHTML = "Please enter your password";document.signup.password.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if( document.signup.password.value.length > 20 ){
			document.getElementById('errPass1').innerHTML = "Password too long";document.signup.password.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if( document.signup.password.value.length < 6 ){
			document.getElementById('errPass1').innerHTML = "Password too short";document.signup.password.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if( document.signup.password.value != document.signup.password2.value){
			document.getElementById('errPass2').innerHTML = "Passwords are not the same";document.signup.password.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		}

		if ( checkMode != 'newAdmin' ) { 
			if(document.getElementById('passwordQuestion').value == 0){
				document.getElementById('errSecQuestion').innerHTML = "Please select security question";document.signup.passwordQuestion.focus();
				errorFlag = 'yes'; suHeight = suHeight + 20;
			} else {
				document.getElementById('errSecQuestion').innerHTML = "";
			}

			if(document.signup.passwordAnswer.value.length < 1){
				document.getElementById('errSecAnswer').innerHTML = "Please enter your security question's answer";document.signup.passwordAnswer.focus();
				errorFlag = 'yes'; suHeight = suHeight + 20;
			} else {
				document.getElementById('errSecAnswer').innerHTML = "";
			}
		}	
	}



	// name
	if ( checkMode == 'all' || checkMode == 'nm' || checkMode == 'newAdmin' ) {
	
		if(document.signup.firstName.value.length < 1){
			document.getElementById('errFName').innerHTML = "Please enter your first name";document.signup.firstName.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else { 
			document.getElementById('errFName').innerHTML = "";
		}
		if(document.signup.lastName.value.length < 1){
			document.getElementById('errLName').innerHTML = "Please enter your last name";document.signup.lastName.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else { 
			document.getElementById('errLName').innerHTML = "";
		}
		
		if ( checkMode != 'newAdmin' ) { 
			if(document.getElementById('gender').value.length < 1){
				document.getElementById('errGender').innerHTML = "Please select your gender";
				errorFlag = 'yes'; suHeight = suHeight + 20;
			} else { 
				document.getElementById('errGender').innerHTML = "";
			}
			/*if ( document.signup.dobM.value > 0 || document.signup.dobD.value > 0 || document.signup.dobY.value > 0) { */
				var fullDate = document.signup.dobM.value + '/' + document.signup.dobD.value + '/' + document.signup.dobY.value;
				var cdRetVal = checkDate(fullDate);
				if( cdRetVal == true){
					saveDOB = document.signup.dobY.value + '/' + document.signup.dobM.value + '/' + document.signup.dobD.value;
					document.getElementById('dob').value = fullDate;
					document.getElementById('errDOB').innerHTML = "";
				} else {
					document.getElementById('errDOB').innerHTML = "Date of birth: " + cdRetVal;
					errorFlag = 'yes'; suHeight = suHeight + 20;
				}	
			/*}*/
		}
		
	}


	// contact
	if ( checkMode == 'all' || checkMode == 'co' || checkMode == 'newAdmin' ) {
		if( document.signup.email1.value.length < 1 ){
			document.getElementById('errEmail1').innerHTML = "Please enter primary email address";document.signup.email1.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else if(!validEmail(document.signup.email1.value)){
			document.getElementById('errEmail1').innerHTML = "Invalid primary email address format";document.signup.email1.focus();
			errorFlag = 'yes'; suHeight = suHeight + 20;
		} else {
			document.getElementById('errEmail1').innerHTML = "";
		}
		if ( checkMode != 'newAdmin' ) { 
			if( document.signup.email2.value.length > 0 ){
			   if(!validEmail(document.signup.email2.value)){
				  document.getElementById('errEmail2').innerHTML = "Invalid additional email address format";document.signup.email1.focus();
				  errorFlag = 'yes'; suHeight = suHeight + 20;
			   } else { 
				document.getElementById('errEmail2').innerHTML = "";
			   }
			}
		}
	}


	// addrress
	if ( signupType == 'member'  && ( checkMode == 'all' || checkMode == 'ad' ) ) {
	}

	

	// options
	if ( signupType == 'member'  && ( checkMode == 'all' || checkMode == 'op' ) ) {
	}


	if ( checkMode == 'all' ) { 
		var objSubmitted 		= new Object();
		objSubmitted.loginName		= document.getElementById('loginName').value;
		objSubmitted.verifyCode		= document.getElementById('verifyCode').value;
		objSubmitted.verifyText		= document.getElementById('verifyText').value;
		var theUrl			= ajaxFile + '?at=su&rnd=' + Math.random();
		var strSubmitted 		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitVerifyAll, failure:handleAjaxError, argument:[] }; 
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);
	
	} else if ( checkMode == 'newAdmin' && errorFlag == 'no' ) { 	
		var objSubmitted 		= new Object();
		objSubmitted.loginName		= document.signup.loginName.value;
		objSubmitted.password		= document.signup.password.value;
		objSubmitted.firstName		= document.signup.firstName.value;
		objSubmitted.lastName		= document.signup.lastName.value;
		objSubmitted.email1		= document.signup.email1.value;
		var theUrl			= ajaxFile + '?at=saveNewAdmin&rnd=' + Math.random();
		var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitSaveNewAdmin, failure:handleAjaxError, argument:[] };
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

	} else if ( checkMode == 'ac' ) { 	
		if ( errorFlag == 'yes' ) { 
			var objSubmitted 		= new Object();
			objSubmitted.loginName		= document.getElementById('loginName').value;
			var theUrl			= ajaxFile + '?at=ver_ac&rnd=' + Math.random();
			var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
			var callback 			= { success:handleSubmitVerifyAccount, failure:handleAjaxError, argument:[] };
			var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);
		} else {
			var objSubmitted 		= new Object();
			objSubmitted.uid		= document.signup.uID.value;
			objSubmitted.loginName		= document.signup.loginName.value;
			objSubmitted.origLoginName	= document.signup.origLoginName.value;
			objSubmitted.password		= document.signup.password.value;
			objSubmitted.passwordQuestion	= document.signup.passwordQuestion.value;
			objSubmitted.passwordAnswer	= document.signup.passwordAnswer.value;
			var theUrl			= ajaxFile + '?at=save_ac&rnd=' + Math.random();
			var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
			var callback 			= { success:handleSubmitSaveAccount, failure:handleAjaxError, argument:[] };
			var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);
		}
	
	} else if ( checkMode == 'nm' && errorFlag == 'no') { 
		var objSubmitted 		= new Object();
		objSubmitted.uid		= document.signup.uID.value;
		objSubmitted.firstName		= document.signup.firstName.value;
		objSubmitted.middleName		= document.signup.middleName.value;
		objSubmitted.lastName		= document.signup.lastName.value;
		objSubmitted.dob		= saveDOB;
		objSubmitted.gender		= document.signup.gender.value;
		var theUrl			= ajaxFile + '?at=save_nm&rnd=' + Math.random();
		var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitSaveName, failure:handleAjaxError, argument:[] };
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

	} else if ( checkMode == 'co' && errorFlag == 'no' ) { 
		var objSubmitted 		= new Object();
		objSubmitted.uid		= document.signup.uID.value;
		objSubmitted.email1		= document.signup.email1.value;
		objSubmitted.email2		= document.signup.email2.value;
		objSubmitted.phone1		= document.signup.phone1.value;
		objSubmitted.phone2		= document.signup.phone2.value;
		objSubmitted.phone3		= document.signup.phone3.value;
		objSubmitted.im			= document.signup.im.value;
		objSubmitted.web		= document.signup.web.value;
		objSubmitted.cmp		= document.signup.cmp.value;
		var theUrl			= ajaxFile + '?at=save_co&rnd=' + Math.random();
		var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitSaveContact, failure:handleAjaxError, argument:[] };
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

	} else if ( checkMode == 'ad' && errorFlag == 'no' ) { 
		var objSubmitted 		= new Object();
		objSubmitted.uid		= document.signup.uID.value;
		objSubmitted.address1		= document.signup.address1.value;
		objSubmitted.address2		= document.signup.address2.value;
		objSubmitted.city		= document.signup.city.value;
		objSubmitted.state		= document.signup.state.value;
		objSubmitted.zip		= document.signup.zip.value;
		objSubmitted.country		= document.signup.country.value;
		var theUrl			= ajaxFile + '?at=save_ad&rnd=' + Math.random();
		var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitSaveAddress, failure:handleAjaxError, argument:[] };
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

	} else if ( checkMode == 'op' && errorFlag == 'no' ) { 
		var objSubmitted 		= new Object();
		objSubmitted.uid		= document.signup.uID.value;
		var optValue = ( document.signup.optInInfo.checked == true ? 'yes' : 'no' );
		objSubmitted.optInInfo		= optValue;
		var theUrl			= ajaxFile + '?at=save_op&rnd=' + Math.random();
		var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
		var callback 			= { success:handleSubmitSaveOptions, failure:handleAjaxError, argument:[] };
		var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

	} 
			
	
}



/* ----------------------------------------- */
function handleSubmitVerifyAll(o){

	var response 	= o.responseText.parseJSON();

	if ( response.result == 'success' && errorFlag == 'no' ) { 
		document.signup.submit();
	} else if(response.result == 'fail') {
		document.getElementById('buttonJoin').innerHTML = '&nbsp;Submit&nbsp;'; 
		if(response.result1 == 'fail') {
			document.getElementById('errCaptcha').innerHTML = response.message1;
			 suHeight = suHeight + 60;
		} else { 
			document.getElementById('errCaptcha').innerHTML = "";
		}
		if(response.result2 == 'fail') {		
			document.getElementById('errLoginName').innerHTML = response.message2;
		} else { 
			document.getElementById('errLoginName').innerHTML = "";
		}
	}

	/* document.getElementById('signup').style.height = suHeight;  */
	
}

/* ----------------------------------------- */
function handleSubmitVerifyAccount(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('errLoginName').innerHTML = response.message;	
		scrollFunc(0,300); 
	} 
}


/* ----------------------------------------- */
function handleSubmitSaveNewAdmin(o){
	var response 	= o.responseText.parseJSON();
	var result  = response.result;
	var message = response.message;

	if ( response.result == 'success' ) { 
		document.getElementById('addAdminTabMsg').innerHTML = '<br>New Admin was added!';
		document.getElementById('addAdminTabMsg').style.color = 'green';
		getAdminsList(gAjaxFile);
		document.getElementById('addAdminTabButtons').innerHTML = "<a class='rButton' href=javascript:closeTab('NewAdmin')>&nbsp;&nbsp;&nbsp;Close&nbsp;&nbsp;</a>";
		document.getElementById('addAdminButton').style.display = 'block';
		document.getElementById('addAdminButton').style.visibility = 'visible';
	} else { 
		document.getElementById('addAdminTabMsg').innerHTML = '<br>Error adding new admin: '+message;
		document.signup.loginName.focus();
		scrollFunc(0,300); 
	}
}


/* ----------------------------------------- */
function handleSubmitSaveAccount(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('accSavBut').innerHTML += '&nbsp;&nbsp;&nbsp;Error saving Account changes!';
		document.signup.loginName.focus();
		scrollFunc(0,300); 
	} else if ( response.result == 'success' || response.result == 'noChange' ) { 
		location.href='home.php?t=1&m=ac';
		
	}
}

/* ----------------------------------------- */
function handleSubmitSaveName(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('namSavBut').innerHTML += '&nbsp;&nbsp;&nbsp;Error saving Name changes!';
		document.signup.firstName.focus();
		scrollFunc(0,450); 
	} else if ( response.result == 'success' || response.result == 'noChange' ) { 
		saveDOB = response.saveDOB;
		location.href='home.php?t=1&m=nm';
		
	}
}

/* ----------------------------------------- */
function handleSubmitSaveContact(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('conSavBut').innerHTML += '&nbsp;&nbsp;&nbsp;Error saving Contact changes!';
		document.signup.email1.focus();
		scrollFunc(0,600); 
	} else if ( response.result == 'success' || response.result == 'noChange' ) { 
		location.href='home.php?t=1&m=co';
		
	}
}

/* ----------------------------------------- */
function handleSubmitSaveAddress(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('addSavBut').innerHTML += '&nbsp;&nbsp;&nbsp;Error saving Address changes!';
		document.signup.address1.focus();
		scrollFunc(0,750); 
	} else if ( response.result == 'success' || response.result == 'noChange' ) { 
		location.href='home.php?t=1&m=ad';
		
	}
}

/* ----------------------------------------- */
function handleSubmitSaveOptions(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'fail') {
		document.getElementById('optSavBut').innerHTML += '&nbsp;&nbsp;&nbsp;Error saving Options changes!';
		document.signup.optInInfo.focus();
		scrollFunc(0,950); 
	} else if ( response.result == 'success' || response.result == 'noChange' ) { 
		location.href='home.php?t=1&m=op';
		
	}
}



/* ----------------------------------------- */
function pickGender(gender){
	document.getElementById('gender').value = gender;
}


/* ----------------------------------------- */
function refreshCaptcha( ajaxFile ) {
	var objSubmitted 		= new Object();
	objSubmitted.ajaxFile		= ajaxFile;
	var theUrl			= ajaxFile + '?at=refreshCaptcha&rnd=' + Math.random();
	var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
	var callback 			= { success:handleRefreshCaptcha, failure:handleAjaxError, argument:[] };
	var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);
	
}

/* ----------------------------------------- */
function handleRefreshCaptcha(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'success') {
		var ajaxFile   = response.ajaxFile;
	   	var verifyCode = response.verifyCode;
	   	var urlPic     = response.urlPic;
		document.getElementById('captchaContent').innerHTML = "<input type='hidden' id='verifyCode' value='" + verifyCode + "'><img src='" + urlPic + "'>&nbsp;&nbsp;<a class='inputMsg' href='javascript:void(0)' onclick=javascript:refreshCaptcha('" + ajaxFile + "')>Get new image</a>";
	}
}






