/* 
 * File:		$Id: wow.js 1165 2010-01-14 12:33:52Z marc $
 * Author:		Bas van Rijen
 * Description:	standard js file for consult
 * Needs:		any.js included as well
 */

// Needs anymeta/www/lib/any.js



//
//<!--@labelfile wow_labels.txt -->

// Function:	submitFromFlash
// Parameters:	
// Returns:		true if from is valid
// Description:	Validate the form

function submitFromFlash()
{
	if (document.forms["any_contribute"].elements["name_first"].value.length > 0)
	{
		document.forms["any_contribute"].submit();
	}
	else
	{
		$("#web_submit").show();
	}
}



// Function:	c_validate
// Parameters:	
// Returns:		true if from is valid
// Description:	Validate the form
//				Depending on alt attributes in form elements
//				If alt attribute and alt text is required then check that field
//
function c_validate(form_name)
{
	if (form_name)
	{
		var form = document.forms[form_name]
	}
	else
	{
		var form	= document.forms['any_consult'];
	}

	var isValid		= true;
	var	vFormElems	= new Array();
	for ( i = 0 ; i < form.elements.length ; i++ )
	{
		elem  = form.elements[i];
		vCode = ( elem['alt'] ) ? elem['alt'] : elem.getAttribute('alt');

		if (elem.name == 'order[birth_date][day]' 
		 || elem.name == 'order[birth_date][month]' 
		 || elem.name == 'order[birth_date][year]')
		{
			if (isNaN(elem.value)) isValid = false;
		}

		if ( !( typeof vCode == 'undefined' || vCode == null || vCode == "" ) )
		{
			if (jQuery.inArray(vFormElems, elem.name) != -1)
			{
				break;
			}
			else
			{
				v = jQuery.getValue(elem);
				if (vCode == 'required' && v.length < 1)
				{
					isValid = false;
				}
				vFormElems[i] = elem.name;
			}
		}
	}
	if (!isValid)
	{
		alert(required_fields);
	}
	return isValid;
}


// Function:	goto_anchor
// Parameters:	anchor			anchor tag containing the target
//				elt2uri			elements from optional post form to be copied to the redirect uri
//				noanswer		set this value if the answer shouldn't be posted
// Returns:		false
// Description:	Handles the consulting
//
function goto_anchor ( anchor, elt2uri, noanswer, form_name)
{
	if ((typeof anchor) == 'string')
	{
		anchor = document.getElementById(anchor);
	}

	if ((typeof form_name) == 'string')
	{
		f = form_name;
	}
	else
	{
		f = 'any_consult';
	}

	if (f == 'any_consult')
	{
		if (c_validate(f))
		{
			if (document.forms[f].elements['redirect'] != null)
			{
				if (	document.forms[f].elements['answer']	!= null
					&&  document.forms[f].elements['answer[]']	== null
					&&  noanswer == null)
				{
					// anchor.text is not working in IE :(
					// document.forms['any_consult'].elements['answer'].value = anchor.text;
					document.forms[f].elements['answer'].value = anchor.innerHTML;
				}
				document.forms[f].elements['redirect'].value = append_elt2uri(anchor.href, f, elt2uri);
				document.forms[f].submit();
			}
			else
			{
				alert('[goto_anchor()] Template error: any_consult form should have an hidden "redirect" field.');
			}
		}
		return false;
	}
	else
	{
		if (c_validate(f))
		{
			if (document.forms[f].elements['redirect'] != null)
			{
				document.forms[f].elements['redirect'].value = append_elt2uri(anchor.href, f, elt2uri);
			}
			document.forms[f].submit();
		}
		return false;
	}
}



// Function:	append_elt2uri
// Parameters:	uri			the uri to be expanded
//				form_name	the name of the form containing the elements
//				elts		the names of the elements to be copied
// Returns:		new uri with elts attached
// Description:	Appends the given elements and values to the uri.
//
var elt = '';
function append_elt2uri ( uri, form_name, elts )
{
	if (elts != null && document.forms[form_name])
	{
		var sep = '?';
		
		// Check if we have already a get argument
		for (var i = 0; i < uri.length && sep == '?'; i++)
		{
			if (uri.substring(i, i+1) == '?')
			{
				sep = '&';
			}
		}

		// Append all the fields from the form as get pars to the uri
		for (var i=0; i<elts.length; i++)
		{
			var names = split_elt2uri(elts[i]);
			var value = '';

			if (names[1].substring(0,1) == '#')
			{
				value = names[1].substring(1,names[1].length-1);
			}
			else if (document.forms[form_name].elements[names[1]])
			{
				value = jQuery.getValue(document.forms[form_name].elements[names[1]]);
			}
			else
			{
				alert('[append_elt2uri()] Template error: form "' + form_name + '" should have a "' + elt + '" field.');
			}

			if (value.length > 0)
			{
				uri = uri + sep + escape(names[0]) + '=' + escape(value);
				sep = '&';
			}
		}
	}
	return uri;
}


function set_post_offensive(thg_id, usr_id)
{
	resetForm('escalate');
	hideObjAll('fhid','fhid_escalate',true);
	document.forms['f_escalate'].thing_id.value = thg_id;
	document.forms['f_escalate'].usr_id.value = usr_id;
}

//
