var action = ""; 
var actionData = "";
var root = ""; 

var err_action_not_setted = "You haven't use the submit function with a valid action name";

function pwf_onLoad()
{
  if(functionExist('onLoad')) 
  {
    onLoad(); 
  }
  if(functionExist('onControllerLoad'))
  {
	onControllerLoad();   
  }
}


function pwf_onSubmit()
{ 

  result = true;
  if(action=="")
  {
	alert(err_action_not_setted);
	result = false;   
  }


  if(functionExist('onControllerSubmit'))
  {
	  result = onControllerSubmit();
  }

  if(result && functionExist('onSubmit'))
  {
    onSubmit();	  
  }
  return result; 
}
function resetForm()
{
	document.frm.reset();
}

function submit(actionNameAndData)
{

  if(actionNameAndData.indexOf(":") != -1)
  {
	actionNameParts = actionNameAndData.split(":");
	actionUrl = actionNameParts[0]; 
	actionNameAndData = actionNameParts[1]; 
	if(actionUrl.indexOf("http://") == -1)
	{
	  actionUrl = root + "/" + actionUrl ; 
	}

	document.frm.action = actionUrl;
  }
  if(actionNameAndData.indexOf("?") != -1)
  {
    actionNameAndDataParts = actionNameAndData.split("?"); 
    action = actionNameAndDataParts[0]; 
    document.frm.pwfActionData.value = actionNameAndDataParts[1]; 
    actionData = actionNameAndDataParts[1];
  }
  else 
  {
    action = actionNameAndData;
  }
  document.frm.pwfAction.value = action; 
  
  if(pwf_onSubmit())
  {
    document.frm.submit(); 
  }
}

function functionExist(functionName)
{
  var result = typeof functionName == 'string' &&
	eval('typeof ' + functionName) == 'function';
  return result; 
}

String.prototype.count = 
  function(s1) 
  {
    return (this.length - this.replace(new RegExp(s1, "g"), '').length) / s1.length ;
  };

String.prototype.repeat = 
  function( num )
  {
    return new Array( num + 1 ).join( this );
  };

String.prototype.isInteger = 
  function()
  {
    return this.match(/^\d+$/);
  };

String.prototype.trim = 
  function() 
  {
    return this.replace(/^\s+|\s+$/g,"");
  };

function elementExists(elementName)
{
  if(element(elementName) == null)
  {
    return false; 
  }
  else
  {
    return true;  
  }
}

function getElementType(elementName)
{
  return eval("frm."+elementName+".type");
}

function setElementValue(elementName,value)
{
  if(elementExists(elementName))
  {
    type = getElementType(elementName);
    if(type=="select-one")
    {
      for(i=0; i<element(elementName).length; i++)
      {
        if(element(elementName).options[i].value == value)
        {
          element(elementName).selectedIndex = i;
        }
      }
    }
    else if(type == "checkbox")
    {
      element(elementName).checked = true;
    }
    else if(type == "radio")
    {
      setRadioValue(elementName,value);
    }
    else if(type=="text" || type=="textarea" || type == "hidden" || type == "password")
    {
      element(elementName).value = value;   
    }
  }
}

function element(elementName)
{
  return  document.getElementById(elementName);
}

function elementValue(elementName)
{
  return element(elementName).value;	
}

function getElementValue(elementName)
{
  return element(elementName).value;
}



function switcher(id)
{
  if (document.getElementById(id).style.display=='none')
  {
    document_show(id);
  }
  else
  {
    document_hide(id);
  }
}

function document_show(id)
{
  document.getElementById(id).style.display= '';
}

function document_hide(id)
{
  document.getElementById(id).style.display= 'none';
}

/**
 * ���������� �� index ��� ����� selected ���� radio field � -1 �� ��� ����� ���������� ������
 */
function radioIndex(radioName)
{
 var myOption = -1;
 for (var i=radioLength(radioName)-1; i > -1; i--)
 {
   var optionChecked = eval("frm."+radioName+"["+i+"].checked");
   if (optionChecked)
   {
      myOption = i;
   }
 }
  return myOption;
 
}
 
function radioLength(radioName)
{
  var radLen = eval("frm."+radioName+".length") + "";
  return radLen.isInteger()?parseInt(radLen):0;
}
/**
* ���������� ��� ���� ��� index ��� ����� selected ���� radio field � -1 �� ��� ����� ���������� ������
*/
function radioValue(radioName)
{
  var ind = radioIndex(radioName);
  return ind==-1?"":eval("frm."+radioName+"["+ind+"].value");
}

function setRadioValue(radioName,radioValue)
{
  radioObj = element(radioName);
  for(var i = 0; i < radioLength(radioName); i++) 
  {
    radioObj[i].checked = false;
	if(radioObj[i].value == newValue.toString()) 
	{
	  radioObj[i].checked = true;
	}
  }
}

function addElement(elementName,elementValue)
{
	if(!elementExists(elementName))
	{
	  hiddenField = "<input type='hidden' name='"+elementName+"' id='"+elementName+"' value='"+elementValue+"'>";
	  dom("pwfArea").innerHTML = dom("pwfArea").innerHTML + hiddenField;
	}
	else 
	{
	  dom(elementName).value = 	elementValue;
	}
}

//return the value of the radio button that is checked
//return an empty string if none are checked, or
//there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function dom(domName)
{
  return document.getElementById(domName);
}

