//Add row to multiple entries ---------------------------- Start
function addRow(r, theCounter){
	
	//alert('r = ' + r);
	//alert('theCounter = ' + theCounter);
	
    var root = r.parentNode;//the root
    var allRows = root.getElementsByTagName('tr');//the rows' collection
    var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
    var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row
    var cInpT = cRow.getElementsByTagName('textarea');//the textareas' collection of the 1st row
    var cInpS = cRow.getElementsByTagName('span');//the textareas' collection of the 1st row
    var cInpSl = cRow.getElementsByTagName('select');//the textareas' collection of the 1st row       
        
    theField = eval("window.document.form1."+theCounter);
    theField.value = allRows.length;
    
    //alert('allRows.length = ' + allRows.length);
    
    //Input
    for(var i=0;i<cInp.length;i++){//changes the input name and id
        var x = cInp[i].getAttribute('name').length - 2;
        var theName = cInp[i].getAttribute('name').substr(0,x);
        cInp[i].setAttribute('name', theName +'_'+(allRows.length));
        cInp[i].setAttribute('id', theName +'_'+(allRows.length));
    //alert(cInp[i].getAttribute('name'));
        cInp[i].value = '';
    }
    //Textarea
    for(var i=0;i<cInpT.length;i++){//changes the textarea name and id
        var x = cInpT[i].getAttribute('name').length - 2;
        var theName = cInpT[i].getAttribute('name').substr(0,x);
        cInpT[i].setAttribute('name', theName +'_'+(allRows.length));
        cInpT[i].setAttribute('id', theName +'_'+(allRows.length));
    //alert('Textarea: '+ cInp[i].getAttribute('name'));
        cInpT[i].value = '';
    }
    //Span
    for(var i=0;i<cInpS.length;i++){//changes the span name and id
        var x = cInpS[i].getAttribute('id').length - 2;
        var theName = cInpS[i].getAttribute('id').substr(0,x);
        cInpS[i].setAttribute('id', theName +'_'+(allRows.length))
    //alert(cInpS[i].getAttribute('id'));
        cInpS[i].value = '';
    }
    //Select
    for(var i=0;i<cInpSl.length;i++){//changes the select name and id
        var x = cInpSl[i].getAttribute('name').length - 2;
        var y = cInpSl[i].getAttribute('id').length - 2;
        var theName = cInpSl[i].getAttribute('name').substr(0,x);
        var theId = cInpSl[i].getAttribute('id').substr(0,x);
        cInpSl[i].setAttribute('name', theName +'_'+(allRows.length))
        cInpSl[i].setAttribute('id', theId +'_'+(allRows.length))
    //alert(cInpSl[i].getAttribute('id'));
        cInpSl[i].value = '';
    }        
    root.appendChild(cRow);//appends the cloned row as a new row
}

function removeRow(r, theCounter, theChangeField, theAlert){
    var root = r.parentNode;//the root
    var allRows = root.getElementsByTagName('tr');//the rows' collection
    var numRows = eval("window.document.form1."+theCounter).value;
    
    //alert('numRows = ' + numRows);
    
    if(numRows >= 1){
	    var cRow = allRows[numRows]//the clone of the 1st row	    
	    root.removeChild(cRow);//appends the cloned row as a new row
	        
	    theField = eval("window.document.form1."+theCounter);
	    theField.value = allRows.length-1;
	    
	    theChangeField.value = 'yes';
    }
    else{
        alert(theAlert);
    }
}
//Add row to multiple entries ---------------------------- End

//Redirect ----------------------------------------------------------------- Start
function gotoPage(theUrl, theAction, theAlert){
    if(theAction == 'delete'){
        if(confirm(theAlert)){
            window.document.location.href = theUrl;
        }
    }   
    else {
        window.document.location.href = theUrl;
    }
}
//Redirect ----------------------------------------------------------------- End


//Redirect ----------------------------------------------------------------- Start
function openWindow(theUrl, theHeight, theWidth){
    var newWindow = window.open(theUrl,"newWindow","top=0px,left=0px,height="+theHeight+"px,width="+theWidth+"px,resizable,status,scrollbars");
}
//Redirect ----------------------------------------------------------------- End


//Counter for textareas ---------------------------------------------------- Start
function textCounter(field, countfield, maxlimit){
    if (field.value.length > maxlimit)
    field.value = field.value.substring(0, maxlimit);
else 
    countfield.value = maxlimit - field.value.length;
    countfield.value = " "+countfield.value
}

//Counter for textareas ---------------------------------------------------- End


//Clean text in textareas before upload ------------------------------------ Start
function cleanText(text, field) {
    var newText = text .replace(/\n/g,"<br/>");
    newText = newText .replace(/\r/g,"");
    newText = newText .replace(/\t/g,"     ");    
    field.value = newText ;
}
//Clean text in textareas before upload ------------------------------------ End


//Unclean text in textareas before display --------------------------------- Start
function uncleanText(field) {
    field.value = field.value.replace(/<br\/>/, "\r\n");
    if (field.value.search(/<br\/>/) > -1) {
        uncleanText(field); 
    }
    field.value = field.value.replace(/<br>/, "\r\n");
    if (field.value.search(/<br>/) > -1) {
        uncleanText(field); 
    }
    field.value = field.value.replace(/     /, "\t");
    if (field.value.search(/     /) > -1) {
        uncleanText(field); 
    }        
}
//Unclean text in textareas before display --------------------------------- End


function checkOther(theValue, theOther, theTextbox){
    if(theValue != theOther){
        theInput = eval("document."+theTextbox);
        theInput.value = '';
    }
}

function checkSelect(theValue, theName, theOtherSelection, theTextbox){
    if(theName == theOtherSelection && theValue == 0){
        theInput = eval("document."+theTextbox);
        theInput.value = '';
    }
}

function checkSelectOnly(theValue, theName, theTextbox){
    if(theValue == 0){
        theInput = eval("document."+theTextbox);
        theInput.value = '';
    }
}

function checkSelectMultiple(theValue, theName, theTextbox1, theTextbox2){
    if(theValue == 0){
        theInput1 = eval("document."+theTextbox1);
        theInput1.value = '';
        theInput2 = eval("document."+theTextbox2);
        theInput2.value = '';        
    }
}
