function validatePwd(ObjAId,ObjBId){
	var myErr = "";
	if(document.getElementById(ObjAId).value != document.getElementById(ObjBId).value){
		myErr = '- Passwords do not match.\n';
		alert('Invalid data entry:\n'+myErr);
	}
	document.returnValue = (myErr == "");
}

function SubmitForm(FormName, Action) {
	document.forms[FormName].form.value = Action;
	document.forms[FormName].submit();
}

function SetOrder(FormName, ByValue, OrderValue) {
	document.forms[FormName].by.value = ByValue;
	document.forms[FormName].order.value = OrderValue;
	document.forms[FormName].submit();
}

function GroupCheckbox(theElement, ObjName) {
	var theForm = theElement.form;
	for(i=0; i<theForm.length; i++) {
		if(theForm[i].type == 'checkbox' && theForm[i].name.match(ObjName)) {
			theForm[i].checked = theElement.checked;
		}
	}
}

function CheckSelected(FormName, ObjName) {
	theForm = document.forms[FormName];
	var $counter = 0;
	for(i=0; i<theForm.length; i++) {
		if(theForm[i].type == 'checkbox' && theForm[i].name != 'checkall' && theForm[i].name.match(ObjName) && theForm[i].checked == true) {
			$counter++;
		}
	}
	if($counter==0) {
		alert('Please select at least one record.');
		return  false;
	} else {
		return true;
	}
}

function ChangeArrow(ObjId) {
	document.getElementById(ObjId).className = (document.getElementById(ObjId).className=='arrrgt') ? 'arrdwn' : 'arrrgt';
}

function ResetArrow(ObjId) {
	document.getElementById(ObjId).className = 'arrrgt';
}

function swapLang(firstObjId, secondObjId) {
	var x = document.getElementById(secondObjId).selectedIndex;
	var y = document.getElementById(firstObjId).selectedIndex;
	document.getElementById(firstObjId).selectedIndex = x;
	document.getElementById(secondObjId).selectedIndex = y;
}

function setMT(sourceObjectId,targetObjectId,desiredLanguageID,sourceParaID,taskID) {
	var myregexp = /<A[\s\S]*?<\/SPAN>([^<]*)<\/A>/im;
	var match = myregexp.exec(document.getElementById(sourceObjectId).innerHTML);
	if (match != null) {
		result = trim(match[1]);
		document.getElementById(targetObjectId).value = result;
		//document.getElementById(targetObjectId).onchange();
		SaveTranslation(result,desiredLanguageID,sourceParaID,taskID,2,0) //google tm, unbranded
		document.getElementById(targetObjectId).focus();
	}
}

function doResize(objectId,size) {
	var txt = document.getElementById(objectId).value;
	var arrtxt = txt.split('\n');
	var rowsbyline = arrtxt.length;
	var rowsbytext = Math.ceil(txt.length/size);
	document.getElementById(objectId).rows = (rowsbytext>rowsbyline) ? rowsbytext : rowsbyline ;
}

function moveTMbox(objectId) {
	var xTop = document.getElementById(objectId).offsetTop;
	//var oTopStr = document.getElementById('tabTable').style.top;
	//var oTopStrLen = oTopStr.length;
	//var oTopInt = oTopStr.substring(0,oTopStrLen-2);
	document.getElementById('tabTable').style.top = 533 + parseInt(xTop) + 'px' ;
}

function catchTab(item,e){
	if(navigator.userAgent.match("Gecko")){
		c=e.which;
	} else {
		c=e.keyCode;
	}
	if(c==9){
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);
		return false;
	}
		    
}

function replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
    
		if (selectionStart != selectionEnd){ 
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		} else {
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}

	} else if (document.selection) {
		var range = document.selection.createRange();

		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;

			 if (!isCollapsed)  {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  } else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function setTitleToSelectedText (select) {
	if (select.selectedIndex > -1) {
		select.title = select.options[select.selectedIndex].text;
	}
}

function goToURL() {
  var i, args=goToURL.arguments; document.returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

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 swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function swapImage() {
  var i,j=0,x,a=swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function setTextOfTextfield(objName,x,newText) {
  var obj = findObj(objName); if (obj) obj.value = newText;
}

function jumpMenuGo(selName,targ,restore){
  var selObj = findObj(selName); if (selObj) jumpMenu(targ,selObj,restore);
}

function jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function validateForm() {
  var i,p,q,nm,field,test,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { field=args[i+1]; test=args[i+2]; val=findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+field+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+field+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+field+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+field+' is required.\n'; }
  } if (errors) alert('Invalid data entry:\n'+errors);
  document.returnValue = (errors == '');
}

function openBrWindow(theURL,objectID,winName,features) {
	var ov = document.getElementById(objectID).value;
	window.open(theURL+ov,winName,features);
}

function openandclose(ObjID) {
	document.getElementById(ObjID).style.display=(document.getElementById(ObjID).style.display=="none")?"":"none";
}

function showandhide(barId,arrowId) {
	document.getElementById(barId).style.display=(document.getElementById(barId).style.display=="none")?"":"none";
	document.getElementById(arrowId).src=(document.getElementById(arrowId).src.indexOf("images/ico_plus.png")>0)?document.getElementById(arrowId).src.replace("images/ico_plus.png","images/ico_minus.png"):document.getElementById(arrowId).src.replace("images/ico_minus.png","images/ico_plus.png");
}

function show(barId,arrowId) {
	document.getElementById(barId).style.display="block";
	document.getElementById(arrowId).src="images/ico_minus.png";
}

function hide(barId,arrowId) {
	document.getElementById(barId).style.display="none";
	document.getElementById(arrowId).src="images/ico_plus.png";
}

function hidediv(DivId) {
	document.getElementById(DivId).style.display="none";
}

function ResetDiv(DivId) {
	document.getElementById(DivId).innerHTML = "";
}

function display(DivId) {
	document.getElementById(DivId).style.display = "block";
}

function JumpToTop() {
	location.href = location.href + '#';
}

function activatetab(listID,divID,otherlistID1,otherdivID1,otherlistID2,otherdivID2) {
	document.getElementById(listID).className="selected";
	document.getElementById(divID).className="";
	document.getElementById(otherlistID1).className="";
	document.getElementById(otherdivID1).className="tabcontent";
	document.getElementById(otherlistID2).className="";
	document.getElementById(otherdivID2).className="tabcontent";
}

function onandoff(textboxID,trID,imgID) {
	document.getElementById(trID).className=(document.getElementById(trID).className=="off")?"on":"off";
	document.getElementById(imgID).style.display=(document.getElementById(textboxID).value == "")?"none":"";
}

function compareFields(f1,f2,rule,errorMsg){
	var myErr = "";
	if(eval("findObj('"+f1+"').value"+rule+"findObj('"+f2+"').value")){
		alert(unescape(errorMsg));myErr += 'errorMsg';}
	document.returnValue = (myErr == "");
}

function ShowFlagIcon(flag,icon) {
	var x=document.getElementById(flag).value;
	if(x!=0) {
	document.getElementById(icon).style.display = "";
	document.getElementById(icon).src = 'images/flags/'+x;
	}
	else {
	document.getElementById(icon).style.display = "none";
	}
}

function insertRow() {
	var oTable = document.getElementById("fileBody");
	var oRow, oCell ,oDiv, oInput;
	// Create and insert rows and cells into the first body.
	oRow = document.createElement("TR");
	oTable.appendChild(oRow);
	
	//oCell = document.createElement("TD");
	//oRow.appendChild(oCell);
	
	oCell = document.createElement("TD");
	oCell.colSpan="2";
	oDiv = document.createElement("DIV");
	oDiv.id="textInput";
	oInput=document.createElement("INPUT");
	oInput.name="artworkFile[]";
	oInput.type="file";
	oCell.appendChild(oDiv);
	oDiv.appendChild(oInput);
	oRow.appendChild(oCell);
	oInput.focus();
}