﻿var inAnim = false; // used in toggleJoin()
/*
*	function closeScreen
*	This functions closes a form screen
*/
function closeScreen(theScreen,theLink){
	// If theLink variable is passed show it
	if (theLink!=undefined){
		$(theLink).show(); 
	}
	// Hide the Form
	if (!Prototype.Browser.IE){
		new Effect.BlindUp(theScreen, {duration: 0.2}); 
	}else{
		$(theScreen).hide();	
	}
}
/*
*	function showScreen
*	This functions opens a form screen
*/
function showScreen(theScreen,theLink,theFrm,scrollPos){
	// If theLink variable is passed hide it
	if (theLink!=undefined && theLink!=''){
		$(theLink).hide(); 
	}
	// Get All text inputs in theFrm if passed and clear the values
	if (theFrm!=undefined){
		var iArray=$$('#' + theFrm + ' input[type=text]');
		iArray=iArray.concat($$('#' + theFrm + ' input[type=password]')); // push password elements as well
		iArray=iArray.concat($$('#' + theFrm + ' textarea')); // push textarea elements as well
		//consoleDump(iArray.length);
		iArray.each(function(i,index){
			//consoleDump(i);
			i.value='';							 
		})
	}
	// Show the Form - No animation for IE because it is a horrible browser
	if (!Prototype.Browser.IE){
		new Effect.BlindDown(theScreen,{
				duration: 0.2, 
				scaleFrom:5,
				afterFinish:function(){
						if (scrollPos!=undefined){
							Element.scrollTo(scrollPos)
						}
					}
				});
	}else{
		$(theScreen).show();
		if (scrollPos!=undefined){
			Element.scrollTo(scrollPos)
		}
	}
}

/*
*	function showHide (Closes one div object and displays another)
*
*	@show = the div to show
*	@hide = the div to hide
*/
function showHide(show,hide){
	// set display properties
	$(show).show();
	$(hide).hide();
}
/*
*	Returns the value of the selected radio button in the radio group, null if
*	none are selected, and false if the button group doesn't exist
*
*	@param {radio Object} or {radio id} el
*	OR
*	@param {form Object} or {form id} el
*	@param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}
/*
*	function doCheckAll
*	This function allows you to select/deselect a group of checkboxes
*	all that is required is the id of the;
*
*	@caller		the id of the calling checkbox
*	@child		the name of the children checkboxes
*	@group		optional - a div to single out the checkboxes in
*
*/
function doCheckAll(caller,child,group){
	
	var parent = $(caller).checked; 
	var children = '';
	
	if (group != undefined){
		children = $$('#' + group + ' input[name="'+ child +'"]');
	}else{
		children = $$('input[name="'+ child +'"]');
	}
	
	children.each(function(i){
		if (!i.disabled){
			if (parent){
				i.checked = true;	
			}else{
				i.checked = false;
			}
		}
	});

}

function handleDelete(frm,msg,ignoreButtons){

	var frm = $(frm);
	
	if (ignoreButtons == undefined){
		ignoreButtons = false;
	}
	
	if (confirm(msg)){
		// process form	
		if (!ignoreButtons){
			$('sbttn1').hide();
			$('proc1').show();
		}
		frm.submit();
	}
}


function externalRelocate(url, src, rec){

	document.externalRelocateForm.url.value = url;
	document.externalRelocateForm.src.value = src;
	document.externalRelocateForm.rec.value = rec;

	document.externalRelocateForm.submit();
}


function showBio(id){
	var d = '';
	
	if (!$('bioContentHolder')){
		d = '<div class="genericPopUp" id="bioContentHolder" style="display:none;"><a href="#" onclick="closeBio(); return false;" class="genericCloser">&nbsp;</a>';
		d = d + '<div class="holder"><div><h2 id="bioName"></h2><div class="content">';
		d = d + '<div id="bioContent"></div>';
		d = d + '<span class="uiBtn" id="bioContentClose"><a href="#" onclick="closeBio(); return false;">Close</a></span>';
		d = d + '<span class="uiBtn" id="bioContentPrint"><a href="#" onclick="window.print(); return false;">Print</a></span>';
		d = d + '</div></div></div></div>';		
		// insert bio window
		$$('body')[0].insert(d);
	}
		
	// add no print class to main container
	$('container').addClassName('noPrint');
	if($('containerMemberBar')){
		$('containerMemberBar').addClassName('noPrint');
	}
	if($('containerFooter')){
		$('containerFooter').addClassName('noPrint');
	}
	if ($('containerRegister')){
		$('containerRegister').addClassName('noPrint');
	}
	if($('containerBannerBar')){
		$('containerBannerBar').addClassName('noPrint');
	}	
	// update the bio div
	$('bioName').update($('bioName_'+id).innerHTML);
	$('bioContent').update($('bioContent_'+id).innerHTML);
	
	openGenWin('bioContentHolder');
}

function closeBio(){	
	// fade contentholder
	$('bioContentHolder').fade({duration:.25, to:0, afterFinish: function(){
		$('bioContent').update('');
		$('bioContent').innerHTML;	
		$('bioName').update('');
		$('bioName').innerHTML;	
		// remove no print class to main container
		$('container').removeClassName('noPrint');
		if($('containerMemberBar')){
			$('containerMemberBar').removeClassName('noPrint');
		}
		if($('containerFooter')){
			$('containerFooter').removeClassName('noPrint');
		}
		if ($('containerRegister')){
			$('containerRegister').removeClassName('noPrint');
		}
		if($('containerBannerBar')){
			$('containerBannerBar').removeClassName('noPrint');
		}
	}});
	closeGenWin('bioContentHolder');	
}

// JavaScript Document
function setResetFields(frm){
	
	var theFields = $$('#' + frm + ' input.required','#' + frm + ' select.required','#' + frm + ' input.setup');
	
	theFields.each(function(field){
			
				Event.observe(field,'focus',function(){
					this.removeClassName('withError');	
					$(this.id+'_error').hide();
				},false)
				
				
				Event.observe(field,'click',function(){
					this.removeClassName('withError');	
					$(this.id+'_error').hide();								
				},false)
			
		}
	);	
	
}

function valLoginFrm(a){
	
	if (a == undefined){
		a = '';	
	}
	var frm = $('loginFrm' + a);
	var msg = '';	
	
	if (!Validation.validate('l_email' + a,{showError:false})){
		msg = msg + 'Valid Email Address required for login\n';
	}
	
	if (!Validation.validate('l_password' + a,{showError:false})){
		msg = msg + 'Password required for login\n';
	}
	
	if (msg != ''){
		// show alert
		alert(msg);
		return false;
	}else{
		//frm.loginSbttn.disable();
		frm.loginSbttn.value = 'Processing';
		return true;
	}

}

function resetRegisterFrm(frm){
	
	var theFields = $$('#' + frm + ' input.required');
	theFields.each(function(field){
			
			field.value = '';
			field.removeClassName('withError');	
			$(field.id+'_error').hide();
			
			
		}
	);
}

function checkPassword(v){

	if ($F(v).length < 6){
		return false;	
	}else{
		return true;
	}

}

function valResetPwd(){
	
	var f = $('passwordFrm');
	var msg = '';	
	var pwdContinue = false;
	
	if (!Validation.validate('oPassword',{showError:false})){
		msg = msg + 'Old Password Required\n';
	}
	
	if (!Validation.validate('nPassword',{showError:false})){
		msg = msg + 'New Password Required\n';
	}else if (!checkPassword('nPassword')){
		msg = msg + 'Password Too Short. Minimum 6 characters.\n';		
	}else{
		pwdContinue = true;	
	}
	
	if (!Validation.validate('cPassword',{showError:false})){
		msg = msg + 'Confirmation Password Required\n';
	}else if (pwdContinue && $F('nPassword') != $F('cPassword')){
		msg = msg + 'Password entries did not match\n';			
	}
	
	if (msg != ''){
		// show alert
		alert(msg);
	}else{
		showHide('proc3','sbttn3');
		frm.submit();
	}
	
}

function valResetPwdFrm(){
	
	var msg = '';	
	
	if (!Validation.validate('n_password',{showError:false})){
		msg = msg + 'Password Required\n';
	}
	else if(!checkPassword('n_password')){
		msg = msg + 'Password must be at least 6 characters\n';	
	}
	
	if (!Validation.validate('c_password',{showError:false})){
		msg = msg + 'Confirmation Password Required\n';
	}
	
	if ($F('n_password') != '' && $F('c_password') != '' && ($F('n_password') != $F('c_password'))){
		msg = msg + 'Passwords did not match\n';
	}
	
	if (msg != ''){
		alert(msg);
		return false;
	}
	
	return true;
}

function simpleValidateForm(f,submitBtn,notifyVw,fnc){
	
	/*
	*	@f			is the id of the form
	*	@submitBtn	is the submit button which is hidden (requires notify element)
	*	@notifyVw	is the notify element we show on submit (requires submit button)
	*	@fnc		is a function to run before submittion - pass it as a full function string (ie: function())
	*/
	
	var frm = $(f);
	var fields = $$(
					'#' + f + ' input[class|=validate]'+',#' + f + ' input[class~=required],' + 
					'#' + f + ' textarea[class|=validate]'+',#' + f + ' textarea[class~=required],' + 
					'#' + f + ' select[class|=validate]'+',#' + f + ' select[class~=required]'
					);
	var msg = '';
	
	fields.each(function(i){
						 
		// validate the field
		if (!Validation.validate(i.id,{showError:false})){
			// to show a proper error we grab it from the alt attribut and if it doesn't exists we use global ones here
			if (i.alt == undefined || i.alt == ''){
				// if required and has no value we tell the user the field is required
				if(i.hasClassName('required') && f.value == ''){
					msg = msg + '\tThe ' + i.name + ' field is required.\n';
				}else{
				// it is not required or has a value we tell the user there is an error
					msg = msg + '\tThe ' + i.name + ' field contains an error.\n';					
				}
			}else{
				// show error message from tag alt attribute
				msg = msg + '\t' + i.alt + '\n';	
			}
		}
	});	

	if (msg != ''){
		// show alert
		alert('The following errors occured:\n' + msg);
	}else{
		// set process notification
		if ((submitBtn != undefined && submitBtn != '') && (notifyVw != undefined && notifyVw != '')){
			showHide(notifyVw,submitBtn);
		}
		if (fnc != undefined && fnc != ''){
			// run a function
			eval(fnc);
		}else{
			// submit form
			frm.submit();
		}
	}

}

function showErr(a){
	var b = $(a+'_error');
	b.setStyle({marginLeft:-(b.getWidth()+2)+'px'});
	$(a).addClassName('withError');
	b.appear({duration:.25,scope: 'errscope'});
}

function validateFeatureForm(){
	var msg = '';	
	showHide('su_proc', 'su_sbttn');
	
	if (!Validation.validate('inf_field_Email',{showError:false})){
		msg = msg + 'Valid Email Required\n';
		showErr('inf_field_Email');
	}
	
	if (!Validation.validate('inf_field_FirstName',{showError:false})){
		msg = msg + 'First Name Required\n';
		showErr('inf_field_FirstName');
	}
	
	if (!Validation.validate('inf_field_LastName',{showError:false})){
		msg = msg + 'Last Name Required\n';
		showErr('inf_field_LastName');
	}
	
	if (!Validation.validate('inf_field_StreetAddress1',{showError:false})){
		msg = msg + 'Address Required\n';
		showErr('inf_field_StreetAddress1');
	}
	
	if (!Validation.validate('inf_field_City',{showError:false})){
		msg = msg + 'City Required\n';
		showErr('inf_field_City');
	}
	
	if (!Validation.validate('inf_field_State',{showError:false})){
		msg = msg + 'State Required\n';
		showErr('inf_field_State');
	}
	
	if (!Validation.validate('inf_field_Phone1',{showError:false})){
		msg = msg + 'Phone Required\n';
		showErr('inf_field_Phone1');
	}
	
	if (!Validation.validate('inf_field_PostalCode',{showError:false})){
		msg = msg + 'Postal Code Required\n';
		showErr('inf_field_PostalCode');
	}
	
	if (!Validation.validate('inf_field_Country',{showError:false})){
		msg = msg + 'Country Required\n';
		showErr('inf_field_Country');
	}
	
	if (!Validation.validate('inf_field_Website',{showError:false})){
		msg = msg + 'Website Required\n';
		showErr('inf_field_Website');
	}
	
	if (!Validation.validate('inf_custom_PickaCatergory',{showError:false})){
		msg = msg + 'Category Required\n';
		showErr('inf_custom_PickaCatergory');
	}
	
	//var pars = $('featureBusinessFrm').serialize();
	showHide('su_proc', 'su_sbttn');
	// do ajax call if validation passed
	if (msg == '') {	
		$('featureBusinessFrm').submit();
		showHide('su_sbttn','su_proc');
		/*new Ajax.Request('/deals/featureYourBusiness.html', {
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,
			onSuccess: function(transport){
				//$('responseRaw').update(transport.responseText);
				//$('responseContent').update($('container').innerHTML);
				//$('responseContent').update(transport.responseText);
				//openGenWin('featureConfirmation','resultContainer');
				//$(submitFrm).submit();
			}
		});*/
	} else {
		showHide('su_sbttn','su_proc');
	}
}
