var pwf_action = ""; 
var pwf_actionData = "";
var root = ""; 
var pwf_actionDataArray; 


var err_action_not_setted = "You haven't use the submit function with a valid action name";
var err_action_data_not_well_formed = "action data are not well formed";

function pwf_onLoad()
{
  if(functionExist('onLoad')) 
  {
    onLoad(); 
  }
  if(functionExist('onControllerLoad'))
  {
	onControllerLoad();   
  }
}

var post = 
{
	get : function(id)
	{
	  resultGet = null; 
	  if(typeof(document.frm[id]) != "undefined")
	  {
		  isArray = typeof(document.frm[id].type) == "undefined"; 
		  type = isArray?document.frm[id][0].type:document.frm[id].type;
		  if(!isArray)
		  {
			  if(type == "text" || type == "textarea" || type == "select-one" || type == "hidden" || type == "password" || type == "file" || ((type == "checkbox" || type == "radio") && document.frm[id].checked == true) )
			  {
				  resultGet = document.frm[id].value.trim();
			  }
		  }
		  else 
		  {
			  if(type == "radio")
				{
				  for(i=0; i<document.frm[id].length; i++)
					{
					  if(document.frm[id][i].checked == true)
						{
						  resultGet = document.frm[id][i].value.trim(); 
						}
					}
				}
			  else 
				{
				  resultGet = new Array(); 
				  for(i=0; i<document.frm[id].length; i++)
					{
					  if(type != "checkbox" || (type == "checkbox" && document.frm[id][i].checked == true))
						{
						  resultGet.push(document.frm[id][i].value.trim());
						}
					}
				  if(type == "checkbox" && resultGet.length == 0)
					{
						resultGet = null; 
					}
				}
			} 
		}
	  return resultGet;
  }
  ,
  getList : function(id)
	{ 
	  resultGetList = new Object(); 
	  check = id + "_";
	  for(i=0;i<document.frm.elements.length; i++)
	  {
		   if(typeof(document.frm.elements[i].name) != "undefined" && document.frm.elements[i].name.length > check.length && document.frm.elements[i].name.substring(0,check.length) == check)
		   {
			   name = new String(document.frm.elements[i].name.substr(check.length));
			   resultGetList[name] = this.get(document.frm.elements[i].name).trim();
		   }
	  }
	  realListLength = 0;
		for(propertyName in resultGetList) 
		{ 
			realListLength++;
	  }
	  if(realListLength == 0)
		{
		  resultGetList = null;
		}
	  return resultGetList;
	}
  ,
  set : function(id,value)
  {
	  resultSet = false; 
	  if(typeof(document.frm[id]) != "undefined")
		{
		  isArray = typeof(document.frm[id].type) == "undefined"; 
		  type = isArray?document.frm[id][0].type:document.frm[id].type;
		  if(type == "select-one" )
			{
		      for(i=0; i<document.frm[id].length; i++)
		      {
		        if(document.frm[id].options[i].value == value)
		        {
		        	document.frm[id].selectedIndex = i;
		        	resultSet = true; 
		        }
		      }
			}
		  else if (type == "checkbox" || type == "radio")
		  {
			  if(!isArray)
				{
				  if(document.frm[id].value == value)
					{
					  document.frm[id].checked = true;
					  resultSet = true;
					}
				}
			  else
				{
			      for(i=0; i<document.frm[id].length; i++)
			      {
					  	if(document.frm[id][i].value == value)
							{
						  	document.frm[id][i].checked = true;
						  	resultSet = true; 
							}			    	  
			      }
				}
		  }
		  else if(type=="text" || type=="textarea" || type == "hidden" || type == "password")
		  {
			  document.frm[id].value = value;
			  resultSet = true;
		  } 
		}
		else 
		{
			input = document.createElement("input"); 
			input.setAttribute("type", "hidden");
			input.setAttribute("name", id);
			input.setAttribute("value", value);
			document.frm.appendChild(input);
			resultSet = true; 
		}
	  return resultSet; 
  }
  ,
  remove : function(id)
  {
	  resultRemove = false; 
	  if(typeof(document.frm[id]) != "undefined")
		{
		 
		  for(i=0; i<document.frm.elements.length; i++)
	    {
	  		node = document.frm.elements[i];
			  if(node.name == id)
				{
	  			delete document.frm[id];
			  	node.parentNode.removeChild(node); 
	  			resultRemove = true;
				}
	    }
		}
	  alert(typeof(document.frm[id]));
	  if(typeof(document.frm[id]) != "undefined")
		{
		  this.remove(id);
		}
	  return resultRemove;
  }
  ,
  removeAll : function()
  {
	  	while(document.frm.elements.length > 0)
	  	{
	  		node = document.frm.elements[0];
	  		id = node.name;
  			delete document.frm[id];
		  	node.parentNode.removeChild(node);	  		
	  	}
  }
}

function pwf_onSubmit()
{ 

  result = true;
  if(action()=="")
  {
	alert(err_action_not_setted);
	result = false;   
  }
  if(functionExist('onControllerSubmit'))
  {
	  proresult = onControllerSubmit();	
	  if(typeof(proresult) !== 'undefined') 
	  {
		  result = proresult;	
	  }	 
  }

  if(result && functionExist('onSubmit'))
  {
	  proresult = onSubmit();	
	  if(typeof(proresult) !== 'undefined') 
	  {
		  result = proresult;	
	  }	  
  }
  return result; 
}
function resetForm()
{
	document.frm.reset();
}

function action()
{
  return pwf_action; 
}

function actionData(id)
{	
	return pwf_actionDataArray[id] == undefined ? null : pwf_actionDataArray[id] ;
}

function setActionDataArray()
{
	pwf_actionDataArray = new Array(); 
	if(pwf_actionData!="")
	{
	  if(pwf_actionData.indexOf(",") != -1)
	  {
		  actionDataPairs = pwf_actionData.split(",");
		  for(i=0;i<actionDataPairs.length; i++)
		  {
			if(actionDataPairs[i].indexOf("=") == -1)
			{
				alert(err_action_data_not_well_formed);
			}
			else 
			{
				keyValue = actionDataPairs[i].split("=");
				pwf_actionDataArray[keyValue[0]] = keyValue[1]; 
			}
		  }
	  }
	  else
	  {
			if(pwf_actionData.indexOf("=") == -1)
			{
				alert(err_action_data_not_well_formed);
			}
			else 
			{
				keyValue = pwf_actionData.split("=");
				pwf_actionDataArray[keyValue[0]] = keyValue[1]; 
			}  
	  }
	}
}


function changeSubmit(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("?"); 
	    pwf_action = actionNameAndDataParts[0];  
	    pwf_actionData = actionNameAndDataParts[1];
	  }
	  else 
	  {
	    pwf_action = actionNameAndData;
	  }
	  setActionDataArray();
}

function submit(actionNameAndData)
{
  changeSubmit(actionNameAndData);
  if(pwf_onSubmit())
  {
	post.set("pwfActionData",pwf_actionData);
	post.set("pwfAction",pwf_action);
    document.frm.submit(); 
  }
}

// New code for PWF_FS_03




// 
function functionExist(functionName)
{
  var result = typeof functionName == 'string' &&
	eval('typeof ' + functionName) == 'function';
  return result; 
}

String.prototype.contains = 
	function(search)
	{
		return this.indexOf(search) > -1;
	};

	
String.prototype.replaceFirst = 
	function(search,replacement)
	{
		return this.contains(search)?this.replace(search,replacement):this;
	};

String.prototype.prepare = 
	function (replacement)
	{
		result = this.replaceAll("\\?", String.fromCharCode(178)); 
		if(replacement.constructor !== Array)
		{
			replacement = new Array(replacement);
		}
		for(key in replacement)
		{
			result = result.replaceFirst("\?",replacement[key]);
		}
		return result.replaceAll(String.fromCharCode(178), "?");
	};

String.prototype.replaceAll = 
	function(search,replace)
	{
		result = this; 
        while(result.indexOf(search) != -1)
        {
            result = result.replace(search,replace);
        }
        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,"");
  };

String.prototype.isCurrency = 
  function(decimalPoint)
  {
	if(this.count(decimalPoint)>1)
	{
	  return false;
	}
	else
	{
	  if(this.count(decimalPoint)==0)
	  {
	    return this.isInteger(); 
	  }
	  else
	  {
	    checkValues = this.split(decimalPoint); 
	    if(!checkValues[0].isInteger())
	    {
	      return false; 
	    }
	    else if(checkValues[1].trim()!="" && !checkValues[1].isInteger())
	    {
	      return false; 
	    }
	    else if(checkValues[1]>99)
	    {
	      return false;  	
	    }
	    else 
	    {
	      return true; 
	    }
	  }
	}
  }

String.prototype.endsWith = function(str)
{
    var lastIndex = this.lastIndexOf(str);
    return (lastIndex != -1) && (lastIndex + str.length == this.length);
}

String.prototype.startsWith = function(str)
{
	var firstIndex = this.indexOf(str);
	return firstIndex == 0; 
}

String.prototype.isEmail = function() 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(this);
}


function el(name)
{
  return eval("document.frm."+name);
}

function elementExists(elementName)
{
  if(element(elementName) == null)
  {
    return false; 
  }
  else
  {
    return true;  
  }
}

function getElementType(elementName)
{
  return eval("document.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)
{
  //alert("show:"+id);
  document.getElementById(id).style.display= '';
}

function document_hide(id)
{
	//alert("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("document.frm."+radioName+"["+i+"].checked");
   if (optionChecked)
   {
      myOption = i;
   }
 }
  return myOption;
 
}
 
function radioLength(radioName)
{
  var radLen = eval("document.frm."+radioName+".length") + "";
  return radLen.isInteger()?parseInt(radLen):1;
}
/**
 * ���������� ��� ���� ��� index ��� ����� selected ���� radio field � -1 �� ���
 * ����� ���������� ������
 */
function radioValue(radioName)
{
  if(radioLength(radioName)==1)
  {
    return eval("document.frm."+radioName+".value");
  }
  else 
  {
    var ind = radioIndex(radioName);
    return ind==-1?"":eval("document.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);
}



function load()
{
	arg = load.arguments;
	for(i=0; i<arg.length; i++)
	{
		
		file = arg[i].trim(); 
		type = ""; 
		if(file.startsWith("js>") || file.startsWith("css>") || file.startsWith("png>") || file.startsWith("gif>") || file.startsWith("jpg>"))
		{
			parts = file.split(">");
			type = parts[0]; 
			file = parts[1].trim(); 
		}
		fileL = file.toLowerCase(); 
		
		if(fileL.endsWith(".jpg") || fileL.endsWith(".gif") || fileL.endsWith(".png") || type == "jpg" || type == "gif" || type == "png")
		{
			ref = document.createElement("img");
			if(!fileL.startsWith("http"))
		    {
				file = root + "/Images/" + file; 
		    }
			ref.setAttribute("src", file)
		}
		else
		{
			if(fileL.endsWith(".js") || type == "js")
			{	
				ref = document.createElement("script");
				if(!fileL.startsWith("http"))
				{
					file = root + "/JavaScripts/" + file; 
				}
				ref.setAttribute("type","text/javascript");
				ref.setAttribute("src", file);
			}
			else if(fileL.endsWith(".css") || type == "css")
			{
				ref = document.createElement("link");
				if(!fileL.startsWith("http"))
				{
					file = root + "/Styels/" + file; 
				}
				ref.setAttribute("rel", "stylesheet");
				ref.setAttribute("type", "text/css");
				ref.setAttribute("href", file);
			}
		
			if ((typeof(ref) != "undefined"))
			{
				document.getElementsByTagName("head")[0].appendChild(ref);
			}
		}
	}
}

function preloadImages() 
{ 
  var d=document; 
  if(d.images)
  {
    if(!d.MM_p) 
    {
      d.MM_p=new Array();
    }
	var i,j=d.MM_p.length,a=preloadImages.arguments; 
	for(i=0; i<a.length; i++)
	{
	  if (a[i].indexOf("#")!=0)
	  { 
	    d.MM_p[j]=new Image; 
	    d.MM_p[j++].src=a[i];
	  }
	}
  }
}



function loadjscssfile(filename, filetype){
	 if (filetype=="js"){ //if filename is a external JavaScript file
	  var fileref=document.createElement('script')
	  fileref.setAttribute("type","text/javascript")
	  fileref.setAttribute("src", filename)
	 }
	 else if (filetype=="css"){ //if filename is an external CSS file
	  var fileref=document.createElement("link")
	  fileref.setAttribute("rel", "stylesheet")
	  fileref.setAttribute("type", "text/css")
	  fileref.setAttribute("href", filename)
	 }
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
	}



