/* Clear the fields in a form */
function clearForm (f) {
	for (var i=0;i<f.elements.length;i++) {
		switch (f.elements[i].type) {
			case "text":
				f.elements[i].value="";
				break;
			case "select-one":				
			case "select":				
				f.elements[i].selectedIndex=-1;
				break;
			case "checkbox":
				f.elements[i].checked=false;
				break;
		}	
	}
}

//Get the value of the selected element in a combo box
function getSelectControlValue(control) {
	if (control.selectedIndex==-1) return '';
	return control.options[control.selectedIndex].value;
}
/**
* We store lists of values in single form fields by concatenating values with a pipe
* This function adds a value to a list
*/
function AddTextToListField(f,sFieldName,sValueToAdd) {
	var sList=f.elements[sFieldName].value;
	sList=AddTextToListVariable(sList,sValueToAdd);
	
	/*
	var aElements=sList.split('|');
	aElements=aElements.concat(sValueToAdd);
	sList=aElements.join('|');
	*/
	f.elements[sFieldName].value=sList;

	/*
	if (f.elements[sFieldName].value=='')  f.elements[sFieldName].value='|';
	f.elements[sFieldName].value=f.elements[sFieldName].value + sValueToAdd + '|'; 
	//The list will have pipes at the begining at and at the end: |element1|element2|
	*/
}

/**
* Return a pipe-separated list of values variable, adding one value to another list
*/
function AddTextToListVariable(slList,sValueToAdd) {
	var aElements=slList.split('|');
	aElements=aElements.concat(sValueToAdd);
	slList=aElements.join('|');
	return slList;
}

/**
* We store lists of values in single form fields by concatenating values with a pipe
* This function removes a value from the list
*/
function RemoveTextFromListField(f,sFieldName,sValueToRemove) {
	var sList=f.elements[sFieldName].value;
	/*
	var aElements=sList.split("|");
	var iPos=-1;
	for (i=0; i<aElements.length; i++) {
		if (aElements[i]==sValueToRemove) {
			iPos=i;
			break;
		}
	}

	if (iPos!=-1) aElements.splice(iPos,1);
	sList=aElements.join('|');
	*/
	sList=RemoveTextFromListVariable(sList,sValueToRemove);
	f.elements[sFieldName].value=sList;
	/*
	var reg = new RegExp('\\|' + sValueToRemove + '\\|', 'gi');
	f.elements[sFieldName].value=sList.replace(reg,'');
	if (f.elements[sFieldName].value.substr(0,1)!='|')
		f.elements[sFieldName].value = '|' + f.elements[sFieldName].value;
	if (f.elements[sFieldName].value.substr(f.elements[sFieldName].value.length-1,1)!='|')
		f.elements[sFieldName].value = f.elements[sFieldName].value + '|';
	if(f.elements[sFieldName].value=='|') f.elements[sFieldName].value='';
	*/
}

/**
* Remove, from a variable with a pipe-separated list of values, one of them and return the result
*/
function RemoveTextFromListVariable(slList,sValueToRemove) {
	var aElements=slList.split("|");
	var iPos=-1;
	for (i=0; i<aElements.length; i++) {
		if (aElements[i]==sValueToRemove) {
			iPos=i;
			break;
		}
	}

	if (iPos!=-1) aElements.splice(iPos,1);
	slList=aElements.join('|');
	return slList;
}

/**
* We store lists of values in single form fields by concatenating values with a pipe
* This function removes a value from the list by its position
*/
function RemoveTextFromListFieldByPos(f,sFieldName,iPos) {
	var sList=f.elements[sFieldName].value;
	var aElements=sList.split('|');
	aElements.splice(iPos,1);
	sList=aElements.join("|");
	f.elements[sFieldName].value=sList;
}

/**
* We store lists of values in single form fields by concatenating values with a pipe
* This function updates a value from the list by its position
*/
function UpdateTextInListFieldByPos(f,sFieldName,iPos,sText) {
	var sList=f.elements[sFieldName].value;
	var aElements=sList.split('|');
	aElements[iPos]=sText;
	sList=aElements.join("|");
	f.elements[sFieldName].value=sList;
}

/*
* Retrieve the position of a text in a list field
*/
function GetTextPositionInListField(f,sFieldName,sValueToSearch,iFromPos) {
	if (!iFromPos) iFromPos=0;
	var aVals=f.elements[sFieldName].value.split('|');
	for (i=iFromPos; i<aVals.length; i++) {
		if (aVals[i]==sValueToSearch) 
			return i;
	}
	return -1;
}

/*
* Retrieve the position of a text in a list field
*/
function GetTextInListFieldByPosition(f,sFieldName,iPos) {
	var sList=f.elements[sFieldName].value;
	return GetTextInListVariableByPosition(sList,iPos);
	//var aElements=sList.split('|');
	//return aElements[iPos];
}

/*
* Retrieve the position of a text in a list variable
*/
function GetTextInListVariableByPosition(slList,iPos) {
	var aElements=slList.split('|');
	//while (aElements[iPos]=='' && iPos<aElements.length) iPos++;
	return aElements[iPos];
}

/**
* Indicate if a field list is empty
**/
function IsListFieldEmpty(f,sFieldName) {
	var bEmpty=true;
	var sList=f.elements[sFieldName].value;
	var aElements=sList.split('|');
	for (i=0; i<aElements.length; i++) {
		if (aElements[i]!='') {
			bEmpty=false;
			break;
		}
	}
	return bEmpty;
}

/*
* Retrieve the number of elements in a list field
*/
function GetNumItemsInListField(f,sFieldName, bDontCountEmpty) {
	if (typeof bDontCountEmpty =='undefined') bDontCountEmpty=false;
	var sList=f.elements[sFieldName].value;
	return GetNumItemsInListVariable(sList,bDontCountEmpty);
	/*
	var aElements=sList.split('|');
	var iNumElements=0;
	for (i=0; i<aElements.length; i++) {
		if (aElements[i]!='') iNumElements++;
	}
	return iNumElements;
	*/
}

/*
* Retrieve the number of elements in a list variable
*/
function GetNumItemsInListVariable(slList,bDontCountEmpty) {
	if (typeof bDontCountEmpty =='undefined') bDontCountEmpty=false;
	var aElements=slList.split('|');
	if (bDontCountEmpty) {	
		var iNumElements=0;
		for (i=0; i<aElements.length; i++) {
			if (aElements[i]!='') iNumElements++;
		}
	} else {
		iNumElements=aElements.length;
	}
	return iNumElements;
	/*
	var iNumElements=0;
	for (i=0; i<aElements.length; i++) {
		if (aElements[i]!='') iNumElements++;
	}
	return iNumElements;
	*/
}

/*
* Used to execute actions on pressing Enter
* Return true if the user pressed enter. e is the event object for the on keyup event.
*/
function UserPressedEnter(e) {
	var bEnter=false;
	if (e!=null && typeof(e)!='undefined') {
		if(e.keyCode==13) {
			bEnter=true;
		}
	} else if (typeof window.event != "undefined"){
		if(event.keyCode==13) {
			
			bEnter=true;
		}		
	}
	return bEnter;
}

/**
* Set a hidden control to 1 or 0 depending on the checked 
* status of a checkbox in the same form
**/
function SetCheckValue(checkControl, fieldName) {
	if (checkControl.checked) 
		checkControl.form.elements[fieldName].value=1;
	else
		checkControl.form.elements[fieldName].value=0;
}

/**
* Set a hidden control to 1 or 0 depending on the checked 
* status of a checkbox
**/
function SetCheckValueByElementId(checkControl, elementId) {
	if (checkControl.checked) 
		document.getElementById(elementId).value=1;
	else
		document.getElementById(elementId).value=0;
}

function GetRadioButtonsValue (rb) {
	var i;
    for (i=0;i<rb.length;i++){
       if (rb[i].checked)
          return rb[i].value; 
    }
    return '';
}


/*Check the format of an email entered by the user */
<!-- Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
function emailCheck (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
  		fits the user@domain format.  It also is used to separate the username
  		from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
  		characters.  We don't want to allow special characters in the address. 
  		These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    //alert("The username doesn't seem to be valid.")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

//To submit a form with a target that if a frame
function SubmitFormToFrame(f) {
	if (BrowserDetect.browser=='Safari') {
		var sActionURL = f.action;
		var sParams=GetFromURLParams(f);
		opener.top.frames.hidden.location= sActionURL + '?' + sParams;
	} else {
		f.submit();
	}
}

//To submit a form to a frame of the opener window
function SubmitFormToOpener(f,sFramename) {
	var sActionURL = f.action;
	var sParams=GetFromURLParams(f);
	opener.top.frames[sFramename].location= sActionURL + '?' + sParams;
}

//Get the URL params from a form input values
function GetFromURLParams(f) {
	var params='';
	for (i=0; i<f.elements.length;i++) {
		if (f.elements[i].type != 'submit' && f.elements[i].type != 'button') {
			if (f.elements[i].type == 'select-multiple') {
				params+=f.elements[i].name+"=";
				for (j=0;j<f.elements[i].options.length; j++) {
					if (f.elements[i].options[j].selected) {
						params+=f.elements[i].options[j].value + '|';
					}
				}
				if (params.substr(params.length-1)=='|')
					params=params.substr(0,params.length-1) + "&";
			} else {
				if (f.elements[i].type == 'checkbox') {
					if (f.elements[i].checked)
						params+=f.elements[i].name+"="+f.elements[i].value+"&";
				} else {
					if (f.elements[i].value!='') 
						params+= f.elements[i].name+"="+escape(f.elements[i].value)+"&";
				}
			}
		}
	}
	return params;
}

function checkNumber(sNumber){
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(sNumber)) {
		bOk=true;
	} else {
		var anum=/(^\d+$)|(^\d+\,\d+$)/;
		if (anum.test(sNumber)) {
			bOk=true;
		} else {
			bOk=false;
		}
	}
	return bOk;
}

function popupform(myform, windowname, iWidth, iHeight) {
	if (! window.focus)return true;
	var sStyle='dependent=yes,resizable=yes,scrollbars=yes,width=' + iWidth + ',height=' + iHeight;
	sStyle = sStyle + ',screenX=' + (window.pageXOffset + window.outerWidth/2 - iWidth/2);
		
	w=window.open('', windowname, sStyle);
	myform.target=windowname;
	return w;
}
