function doShowHideSecondProfile( sShow, eForm ) {
	if( sShow == 'yes' )
		$( '.form_second_col', eForm ).css( 'display', '' );
	else
		$( '.form_second_col', eForm ).css( 'display', 'none' );
}

function emailChangeSubmit(eForm){
	var sEmailOld = jQuery.trim(jQuery("input[name='hdn_email_old']").val());
	var sEmailNew = jQuery.trim(jQuery("input[name='Email[0]']").val());
	
	var sPassCon = jQuery.trim(jQuery("#input[name='Password_confirm[0]'").val());
	var sPass = jQuery.trim(jQuery("#input[name='Password[0]'").val());
	//alert(sEmailOld + "-----" + sEmailNew);
	
	if(sEmailOld == sEmailNew){
		eForm.submit();
		return;
	}else{
		validateJoinForm( eForm );
	}
	
	return false;
}

function validateJoinForm( eForm ) {
	if( !eForm )
		return false;
	
	hideJoinFormErrors( eForm );
	
	$(eForm).ajaxSubmit(function(sResponce) {
		//alert(sResponce);
		try {
			var aErrors = eval(sResponce);
		} catch(e) {
			return false;
		}
		
		doShowJoinErrors( aErrors, eForm );
	} );
	
	return false;
}


function validateJoinForm2(eForm){
	var idForm = jQuery(eForm).attr("id");

	var result = true;	
	var aIdRequire = new Array();
	jQuery("#"+idForm + " .require").each(function(i) { 
		var id = jQuery(this).attr("id");
		var name = jQuery(this).attr("name");
		//alert(id + "----" + name);
		if(jQuery(this).is("input")){
			// Checkbox Group
			if( jQuery(this).is("input[type='checkbox']") ){
				var aCheckbox = jQuery("input[name='"+name+"']:checked");
				if(aCheckbox.length==0){
					jQuery("#require_"+name).show();
					result = false;
				}
				
			// textbox
			}else if(jQuery(this).is("input[type='text']")){
				if(jQuery.trim(jQuery(this).val())=="") {
					jQuery("#require_"+name).show();
					aIdRequire.push(id);
					jQuery("#"+aIdRequire[0]).focus();
					result = false;
					var oNext = jQuery(this).next();
						jQuery(oNext).show();
				}else{
					if(jQuery(this).hasClass('smail')){
						var bIsEmail = emailValidate(jQuery(this).val());
						//alert(id + "----" + name + "-------"+bIsEmail);
						if(!bIsEmail){
							result = false;
						}
					}
				}
			
			// password
			}else if(jQuery(this).is("input[type='password']")){
				var val = jQuery(this).val();
				if(val.length<6){
					jQuery("#require_"+name).show();
					jQuery(this).focus();
					result = false;
				}else{
					jQuery("#require_"+name).hide();
				}			
			}
			
		}
	});
		
	if (jQuery("#TermsOfUse_agree").length > 0){
		if(!jQuery("#TermsOfUse_agree").is(":checked")){
			result = false;
			var oTNext = jQuery("label[for='TermsOfUse_agree']").next();
			jQuery(oTNext).show();
		}
	}
	
	if (jQuery("#parents_agreement").length > 0){
		if(!jQuery("#parents_agreement").is(":checked")){
			result = false;
			var oTNext = jQuery("#parents_agreement").next();
			var oTNextNext = jQuery(oTNext).next();
			jQuery(oTNextNext).show();
		}
	}
	return result;
}

function emailValidate(email) {
  	 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 	 // var address = document.forms[form_id].elements[email].value;
   	if(reg.test(email) == false) {
	  return false;
   	}else{
	   return true;
	}
}

function hideJoinFormErrors( eForm ) {
	$( 'img.form_warn_icon', eForm ).css( 'display', 'none' );
	$( '.input_erroneus', eForm ).removeClass( 'input_erroneus' );
}

function doShowJoinErrors( aErrors, eForm ) {
	if( !aErrors || !eForm )
		return false;
	
	var bHaveErrors = false;
	
	for( var iInd = 0; iInd < aErrors.length; iInd ++ ) {
		var aErrorsInd = aErrors[iInd];
		for( var sField in aErrorsInd ) {
			var sError = aErrorsInd[ sField ];
			bHaveErrors = true;
			
			doShowError( eForm, sField, iInd, sError );
		}
	}
	
	if( !bHaveErrors )
		eForm.submit();
}

function doShowError( eForm, sField, iInd, sError ) {
	var $Field = $( "[name='" + sField + "']", eForm ); // single (system) field
	if( !$Field.length ) // couple field
		$Field = $( "[name='" + sField + '[' + iInd + ']' + "']", eForm );
	if( !$Field.length ) // couple multi-select
		$Field = $( "[name='" + sField + '[' + iInd + '][]' + "']", eForm );
	if( !$Field.length ) // couple range (two fields)
		$Field = $( "[name='" + sField + '[' + iInd + '][0]' + "'],[name='" + sField + '[' + iInd + '][1]' + "']", eForm );
	
	//alert( sField + ' ' + $Field.length );
	
	$Field.addClass( 'input_erroneus' );
	
	$Icon = $Field.siblings( 'img.form_warn_icon' );
	$Icon.css( 'display', '' );
	$Icon.mouseover( function(){ showFloatDesc(sError) } );
}

