
function validateAndSubmit(pmPref, obj){
	var vPassword 	= $('#txtPassword').val();
	var vConfirmPwd = $('#txtConfirmPwd').val(); 
	var vEmail		= $('#txtEmail').val(); 
	var vZipCode 	= trim($('#txtZipcode').val()); 
	var vFname 	= trim($('#txtFname').val()); 
	var vLname 	= trim($('#txtLname').val());
	gender = '';
	var profile_reminder = 0;
	$('input[name="radio_gender"]').each( function()
	{
		var obj = $(this);
		if ( obj.is(":checked") )
			gender = obj.val();
	});
	if ( $('#chkremind').is(":checked") )	profile_reminder = $('#chkremind').val();
	vTerms = vFreshReward = 0;
	if(ge('chkTerms').checked)  vTerms = 1 ;

	aErrFields = new Array('err_Email', 'err_Password', 'err_ConfirmPwd', 'err_Zipcode', 'err_Terms', 'err_Fname','err_Lname');
	clearAllErrorFields(aErrFields);
	var error = 0;
	var focusfld = '';
	if( !hasValue(vEmail, 'err_Email', 'txtEmail', 'Please enter the email address') )	error = 1;
	else if ( !isValidEmail(vEmail) ){
		$('#err_Email').html('Please enter the valid email address');
		focusfld = 'txtEmail';
		error = 1;
	}
	
	if( !hasValue(vZipCode, 'err_Zipcode', 'txtZipcode', 'Please enter the zipcode') ){
		if(!error)	focusfld = 'txtZipcode';
		error = 1;
	}
	else if ( !isZipcode(vZipCode) ){
		$('#err_Zipcode').html('Please enter the valid zipcode');
		if(!error)	focusfld = 'txtZipcode';
		error = 1;
	}
	
	if( !hasValue(vPassword, 'err_Password', 'txtPassword', 'Please enter the password') ){
		if(!error)	focusfld = 'txtPassword';
		error = 1;
	}
	else if ( vPassword.length < 6 ){
		$('#err_Password').html('Password should be minimum of 6 characters');
		if(!error)	focusfld = 'txtPassword';
		error = 1;
	}
	if( !hasValue(vConfirmPwd, 'err_ConfirmPwd', 'txtConfirmPwd', 'Please enter the confirm password') ){
		if(!error)	focusfld = 'txtConfirmPwd';
		error = 1;
	}
	else if ( vConfirmPwd.length < 6 ){
		$('#err_ConfirmPwd').html('Confirm password should be minimum of 6 characters');
		if(!error)	focusfld = 'txtConfirmPwd';
		error = 1;
	}
	else if ( vPassword != vConfirmPwd){
		$('#err_ConfirmPwd').html('confirm password doesnt match with your password');
		if(!error)	focusfld = 'txtConfirmPwd';
		error = 1;
	}
	if( !hasValue(vFname, 'err_Fname', 'txtFname', 'Please enter the first name') ){
		if(!error)	focusfld = 'txtFname';
		error = 1;
	}
	else if ( vFname.length < 3 ){
		$('#err_Fname').html('First name should be minimum of 3 characters');
		if(!error)	focusfld = 'txtFname';
		error = 1;
	}
	else if ( !isPersonName(vFname) ){
		$('#err_Fname').html('Please enter the valid name');
		if(!error)	focusfld = 'txtFname';
		error = 1;
	}
	
	if( !hasValue(vLname, 'err_Lname', 'txtLname', 'Please enter the last name') ){
		if(!error)	focusfld = 'txtLname';
		error = 1;
	}
	else if ( vLname.length < 3 ){
		$('#err_Lname').html('Last name should be minimum of 3 characters');
		if(!error)	focusfld = 'txtLname';
		error = 1;
	}
	else if ( !isPersonName(vLname) ){
		$('#err_Lname').html('Please enter the valid name');
		if(!error)	focusfld = 'txtLname';
		error = 1;
	}
	
	if(vTerms == 0){
		$('#err_Terms').html('Please agree to the Terms of Use');
		error = 1;
	}	
	if(error) {
		if(ge(focusfld))	ge(focusfld).focus();
		popupresize();
		return 0;	
	}
	ge('cmdComplete').disabled = true;
	//ge('cmdStart').disabled = true;
	ge('divProcess').innerHTML = 'Please wait while we create your Celebrations account';
	vData = 'preference='+pmPref+'&password='+vPassword+'&email='+vEmail+'&zipcode='+vZipCode;
	vData += '&terms='+vTerms+'&gender='+gender+'&profile_reminder='+profile_reminder+'&firstname='+vFname+'&lname='+vLname+'&rand='+Math.random();
	popupresize();
	$.ajax({
		type : "POST",
		url	 : g_site_path+"ajax/processSignup",
		data : vData,
		dataType : 'xml',
		success: function(xml){
			$(xml).find('response').each( function(){
				var vMemId  = parseInt($(this).find('memid').text());
				var status  = parseInt($(this).find('status').text());
				var message = $(this).find('message').text();
				var rurl = $(this).find('redurl').text();
				
				ge('divProcess').innerHTML = '';
				if(status == 2) {
					$('#err_Email').html('Please enter a valid email address');
					ge('txtEmail').focus();
					ge('txtEmail').select();
				} else if ( status == 0 ) {					
					var refId	= $(this).find('message').attr('id');
					if ( refId == "email" ){
						$('#err_Email').html(message);
						ge('txtEmail').focus();
						ge('txtEmail').select();
					} /*else if( refId == "firstname" ){
						$('#err_Fname').html(message);
						ge('txtFname').focus();
						ge('txtFname').select();
					} else if( refId == "lastname" ){
						$('#err_Lname').html(message);
						ge('txtLname').focus();
						ge('txtLname').select();
					}*/
				} else if( isdefined( obj ) && isdefined( obj.log_succ_callback) ) {
					if( !obj.log_succ_callback.match('savepreference') ) {
						nopopup();
					}
					g_lin = '1';
					setTimeout(obj.log_succ_callback, 20);
				} else if(status == 1){
					var vMessage = '<div class="content-area-white" style="width:550px;"><table align="center" cellpadding="0" cellspacing="0" border="0" ><tr><td colspan="3"><b>Thank you for joining Celebrations.com!</b></td></tr><tr><td height="3%">&nbsp;</td></tr><tr><td ><a  href="javascript:;" onclick="defaultprofilepage('+vMemId+');"><img src='+g_site_path+'skin/images/btn_returntotheside.gif border="0"></a></td><td>&nbsp;</td><td id="td_create_profile"><a  href="'+rurl+'" ><img src='+g_site_path+'skin/images/btn_createyourprofile.gif border="0"></a></td></tr><tr><td height="5%">&nbsp;</td></tr></table></div>'
					d = new dialog( {title:"&nbsp;",width:700, height:530,onclose:'closeDialog()', ismodel:true, isdrag:true, bgclass:'content-area-white'});
					d.sethtml('<span class="warning">loading...</span>');
					d.show();	
					d.sethtml( vMessage );
					
					vRedURL = $('#hidRegRedURL').val();
					if( vRedURL != '' ) $('#td_create_profile').html( '' ); 
					
				}
			});
		}
	});
}
function closeDialog() 
{
	var vFname 	= trim($('#txtFname').val()); 
	var vLname 	= trim($('#txtLname').val());
	vRedURL = $('#hidRegRedURL').val();
	vRedURL = vRedURL == '' ? g_site_path+"profile/"+vFname+vLname : vRedURL;	
	setTimeout("window.location.href = '"+vRedURL+"'",500);
	d.close();
}

function profilepage()
{
	vRedURL = $('#hidRegRedURL').val();
	//vRedURL = vRedURL == '' ? g_site_path+"profile" : vRedURL;
	setTimeout("window.location.href = '"+vRedURL+"'",500);
	closeDialog()
}
function defaultprofilepage(pmMemId)
{
	
	vQueryString = 'vMemId='+pmMemId+'&rand='+Math.random()
	d.sethtml('<span class="warning">loading...</span>');
	d.show();	
	$.ajax({
	type : "POST",
	url	 :g_site_path+"ajax/profile-avatar",
	data : vQueryString,
	dataType: "html",
	success: function(html){		
	//alert(html)
	d.sethtml( html );
	d.resizeDialog();
	}
	});				
}
function fillUsrUrl(pmUserName){
	ge('txtUsrUrl').value = g_site_path + pmUserName;
}
function addMyDate(another){
	frm = document.frmMyDate;
	
	var ocsn = trim($('#comOccasion').val()); 
	var personDay = trim($('#person_day').val());
	var personMonth = trim($('#person_month').val()); 
	var personYr = trim($('#person_year').val()); 
	var personEmail = trim($('#txtPersonEmail').val()); 
	var personNote	= trim($('#txtPersonNote').val()); 
	var personName	= trim($('#txtPersonName').val()); 	
	
	var relLen = frm.chkRel.length;
	var relation = '';
	for(index = 0; index < relLen; index++){
		if(frm.chkRel[index].checked){
			relation = 	frm.chkRel[index].value;
			break;
		}
	}
	aErrFields = new Array('err_dob','err_occasion', 'err_person_date', 'err_rel', 'err_person_email' );
	clearAllErrorFields(aErrFields);
	var error = 0;
	var err_person_date = 0;
	var focusfld = '';
	vData = '';
	action = 'processMyDate';
	if(!another) {
		response = finishMyDate(); 
		if(response.error == 1) {
			focusfld = response.data;
			error = 1;
		}
		else vData = response.data;
		action = 'finishMyDate';
	}
	
	if( !hasValue(ocsn, 'err_occasion', 'comOccasion', 'Please select the occasion') ){
		if(!error)	focusfld = 'comOccasion';
		error = 1;
	}
	if( !hasValue(personName, 'err_person_date', 'txtPersonName', 'Please enter the persons name') ){
		if(!error)	focusfld = 'txtPersonName';
		err_person_date = 1;
		error = 1;
	}
	if (!err_person_date && personName.length < 3 ){
		$('#err_person_date').html('Persons name should be minimum of 3 characters');
		if(!error)	focusfld = 'txtPersonName';
		err_person_date = 1;
		error = 1;
	}
	if ( !err_person_date && !isPersonName(personName) ){
		$('#err_person_date').html('Please enter the valid persons name');
		if(!error)	focusfld = 'txtPersonName';
		err_person_date = 1;		
		error = 1;
	}
	if(!err_person_date && !hasValue(personDay, 'err_person_date', 'person_day', 'Please select the day') ){
		if(!error)	focusfld = 'person_day';
		error = 1;
		err_person_date = 1;
	}
	if(!err_person_date && !hasValue(personMonth, 'err_person_date', 'person_month', 'Please select the month') ){
		if(!error)	focusfld = 'person_month';
		error = 1;	
		err_person_date = 1;
	}
	if(!hasValue(relation, 'err_rel', 'chkRel', 'Please select the relationship') ){
		if(!error)	focusfld = 'chkRel';
		error = 1;
	}
	//Commented because Email to notify should be optional
	/*if(!hasValue(personEmail, 'err_person_email', 'txtPersonEmail', 'Please enter the email address') ){
		if(!error)	focusfld = 'txtPersonEmail';
		error = 1;	
	}*/
	if ( !isValidEmail(personEmail) && personEmail != '' ){
		$('#err_person_email').html('Please enter their valid email address');
		if(!error)	focusfld = 'txtPersonEmail';
		error = 1;
	}
	if(error){
		if(ge(focusfld))	ge(focusfld).focus();
		return;
	}
	//if(!hasValue(personNote, 'err_person_note', 'txtPersonNote', 'Please enter the notes') ) return;	
	ge('cmdExit').disabled = true;
	ge('cmdNext').disabled = true;
//	ge('cmdAdd').disabled = true;		
	$('#spanAddOne').css("display", "none");	
	$('#spanAddAnother').css("display", "none");
	
	ge('err_occasion').innerHTML = 'Please wait... while adding your date';
	vData += 'ocsn='+ocsn+'&personname='+personName+'&personday='+personDay+'&personmonth='+personMonth+'&personyr='+personYr;
	vData += '&personemail='+personEmail+'&personnote='+personNote+'&relation='+relation+'&rand='+Math.random();	
//	alert( g_site_path+"ajax/processMyDate" + vData );
	$.ajax({
		type : "POST",
		url	 : g_site_path+"ajax/"+action,
		data : vData,
		dataType : 'xml',
		error: function(req, err, obj) {
        	alert ("Error in Ajax call: " + err + " - " + req.status); 			 
		},
		success: function(xml){
			$(xml).find('response').each( function(){
				var message = $(this).find('message').text();				
				$('#err_occasion').html(message);
				ge('cmdExit').disabled = false;
				ge('cmdNext').disabled = false;
				ge('comOccasion').value = '';
				ge('person_day').value = '';				
				ge('person_month').value = '';				
				ge('person_year').value = '';				
				ge('txtPersonEmail').value = '';		
				ge('txtPersonNote').value = '';				
				ge('txtPersonName').value = '';	
				frm.chkRel[index].checked = false;
				if(another == 1){
//					ge('cmdAdd').disabled = false;	
					$('#spanAddOne').css("display", "block");	
					$('#spanAddAnother').css("display", "block");
					setTimeout(function(){$('#err_occasion').html('');},3000);
				}
				else{
					var vRedURL = $(this).find('redurl').text();
					setTimeout("window.location.href = '"+vRedURL+"'",3000);
				}
			});
		}
	});
	
}
function finishMyDate(pref){
	frm = document.frmMyDate;
	var dobday = trim($('#dob_day').val()); 
	var dobmonth = trim($('#dob_month').val()); 
	var dobyr = trim($('#dob_year').val()); 	
	var displaydob = 2;
	if(ge('chkDisplayDob').checked)	displaydob = 1;
	var error = 0;
	var focusfld = '';
	/*var genderLen = frm.chkGender.length;
	var gender = '';
	for(index = 0; index < genderLen; index++){
		if(frm.chkGender[index].checked){
			gender = frm.chkGender[index].value;
			break;
		}
	}*/
//	aErrFields = new Array('err_dob','err_occasion', 'err_person_date', 'err_rel', 'err_person_email');
	//clearAllErrorFields(aErrFields);
	
	if(!hasValue(dobday, 'err_dob', 'dob_day', 'Please select the day') ){
		if(!error)	focusfld = 'dob_day';
		error = 1;
	}
	if(!hasValue(dobmonth, 'err_dob', 'dob_month', 'Please select the month') ){
		if(!error)	focusfld = 'dob_month';
		error = 1;	
	}
	if(!hasValue(dobyr, 'err_dob', 'dob_year', 'Please select the year') ){
		if(!error)	focusfld = 'dob_year';
		error = 1;
	}
	if(error){
		if(ge(focusfld))	ge(focusfld).focus();
		return {error:1, data:focusfld};
	}
	//if(!hasValue(gender, 'err_Gender', 'chkGender', 'Please select the gender') ) return;	
//	ge('err_occasion').innerHTML = 'Please wait... while adding your information';
	vData = 'preference=1&dobday='+dobday+'&dobmonth='+dobmonth+'&dobyr='+dobyr;
	vData += '&displaydob='+displaydob+'&';	
	return {error:0, data:vData};
	//vData += '&displaydob='+displaydob+'&gender='+gender+'&rand='+Math.random();	
//	ge('cmdExit').disabled = true;
	//ge('cmdNext').disabled = true;
	
	//alert(g_site_path+"ajax/finishMyDate" +vData);return;

	$.ajax({
		type : "POST",
		url	 : g_site_path+"ajax/finishMyDate",
		data : vData,
		dataType : 'xml',
		error: function(req, err, obj) {
        	alert ("Error in Ajax call: " + err + " - " + req.status); 			 
		},
		success: function(xml){
			$(xml).find('response').each( function(){
				var message = $(this).find('message').text();				
				$('#err_occasion').html(message);
				var vRedURL = $(this).find('redurl').text();
				setTimeout("window.location.href = '"+vRedURL+"'",3000);
			});
		}
	});
	
}

function OpenForgtPwdPopup(e, ispopup) {
	vContentMainDiv = ce('div');
	br = ce('br');		
	vContentMainDiv.appendChild(br);
	vErrDiv = ce('div');		
	$(vErrDiv).attr({ id: "err_ForgtEmail", className:"warning_black"});	
	vContentMainDiv.appendChild(vErrDiv);
	vContentLabelDiv = ce('div');
	vTextNode = document.createTextNode('Please enter your email address and we will mail you a replacement password');
	vContentLabelDiv.appendChild(vTextNode);
	vContentLabelDiv.appendChild(ce('br'));
	$(vContentLabelDiv).attr({style: "display:inline;"});	
	vContentMainDiv.appendChild(vContentLabelDiv);
	vContentFldDiv = ce('div');	
	vTxtFld= ce('input');	
	$(vTxtFld).attr({type: "text", name: "txtForgetEmail", id: "txtForgetEmail", maxlength: "60", className:"reg_text_field"});	
	vContentFldDiv.appendChild(vTxtFld);
	$(vContentFldDiv).attr({align:"left",style: "display:inline;"});
	vContentMainDiv.appendChild(vContentFldDiv);
	vBreak = ce('br');		
	vContentMainDiv.appendChild(vBreak);	
	vBreak = ce('br');		
	vContentMainDiv.appendChild(vBreak);	
	vButDiv = ce('div');		
	vBut = ce('input');		
	$(vBut).attr({type: "image", name: "cmdSend", id: "cmdSend", src:g_template_img+"btn_send_dark_gray.gif"});	
	vBut.onclick = ValidateForgetPwd;
	vButDiv.appendChild(vBut);
	vContentMainDiv.appendChild(vButDiv);
	$(vContentMainDiv).attr({align:"center",style: "vertical-align:bottom;width:100%"});
	if( ispopup ) {
		reloadpopup( {event:e, width:350, height:100} )
	} else {
		popup( {event:e, width:350, height:100} );
	}
	
	ge('popupcanvas').innerHTML = '';
	ge('popupcanvas').appendChild( vContentMainDiv );
	
	if ( document.all ){
		setTimeout( function(){
							 	if ( $('#cmdSend') ) $('#cmdSend').attr( 'src', g_template_img+"btn_send_dark_gray.gif" );
							 }, 100);
	}
	//popupresize();
	//popup.show();
	//popup.loaded = true;	
}
function ValidateForgetPwd(){
	ge('err_ForgtEmail').innerHTML = '';
	vEmail = ge('txtForgetEmail').value;
	if(!hasValue(vEmail, 'err_ForgtEmail', 'txtForgetEmail', 'Please enter your email address.') )	return;
	if ( !isValidEmail(vEmail) ){
		ge('err_ForgtEmail').innerHTML = 'Please enter the vaild email address.'
		ge('txtForgetEmail').focus();
		return;
	}
	ge('cmdSend').disabled = true;
	$('#err_ForgtEmail').html('Please wait...');
	$.ajax({
		type : "POST",
		url	 :  g_site_path+"ajax/send-password",
		data : 'user_email='+vEmail+'&rand='+Math.random(),
		dataType : 'xml',
		success: function(xml){
			$(xml).find('response').each( function(){
				var status  = parseInt($(this).find('status').text());
				var message = $(this).find('message').text();
				$('#err_ForgtEmail').html(message);	
				ge('cmdSend').disabled = false;
				setTimeout(function() { nopopup() }, 3000);
			});
		}
	});
}
