var countForm = 1;
var numSeq = 1;
function callcal(x,y) {
    if (y == 1) { 
            $(function() {
                var dest = document.getElementById("dDate"+x); 
                $('#dDate'+x).datepicker({ 
				constrainInput: true,
				buttonImageOnly: true,
				dateFormat: 'mm/dd/yy',
				onSelect:
                function (dateText, inst) {
                    dest.value = dateText;
                    $('#dDate'+x).datepicker( 'destroy' );
                }
                                        });
            });
    } else if(y == 2) { 
            $(function() {
                    var dest = document.getElementById("datem"+x); 
                    $('#datem'+x).datepicker({ 
					constrainInput: true,
					buttonImageOnly: true,
					dateFormat: 'mm/dd/yy',
					onSelect:
                    function (dateText, inst) {
                        dest.value = dateText;
                        $('#datem'+x).datepicker( 'destroy' );
                    }
                                            });
            });
    }
}

function transfer (List,From,To,act) {
	var from = document.getElementById(From)
	var to = document.getElementById(To)
	var list = document.getElementById(List)
	try {
		fromLength = from.length
	}
	catch (x) {
		var from = document.getElementById(From)
		var to = document.getElementById(To)
		var list = document.getElementById(List)
		fromLength = from.length
	}
	for (var i = 0; i < fromLength; i++){
		if (from.options[i].selected) {
			if (act != "del") {
				if (list.value) {
					list.value = list.value + ',' + from.options[i].value
				}
				else {
					list.value = from.options[i].value
				}
			}
			to.options[to.length] = new Option(from.options[i].text, from.options[i].value)
		}
	}
	for (var i = (fromLength -1); 0 <= i; i--) {
		if (from.options[i].selected) {
			if (act == 'del') {
				removeFromList(list, from.options[i].value)
			}
			from.options[i] = null
		}
		
	}
}

function removeFromList (List,str) {
	var out = new Array()
	var b = 0
	var list = document.getElementById(List)

	try {
		var mylist = list.value.split(",")
	}
	catch (x) {
		var list = List
		var mylist = list.value.split(",")
	}
	for (var a = 0; a < mylist.length; a++){
		if (mylist[a] != str) {
			out[b++] = mylist[a]
		}		
	}
	list.value = out
}

function makeForms(x) { 
	countForm++;
    numSeq++;
    if ((x == 3) && (countForm == 2)) { countForm++; }

    dest = document.getElementById("formDest");
    document.getElementById("numForms").value = countForm;

	if (x != '4') { 
    
                var newRow = dest.insertRow(-1);
	    var newCell = newRow.insertCell(-1);
		    newCell.appendChild(document.createTextNode('.')); 
            newCell.style.color = '#BDB76B';
	    var newCell = newRow.insertCell(-1);
		    newCell.appendChild(document.createTextNode('.')); 
            newCell.style.color = '#BDB76B';
    	// SEQ
	    var newRow   = dest.insertRow(-1);
       	var newCell  = newRow.insertCell(-1);
	        newCell.appendChild(document.createTextNode('Seq #'+numSeq));
            newCell.style.fontWeight = 'bold'
            
            
    	var newCell  = newRow.insertCell(-1);
	        var objInput = document.createElement("input");
    	        objInput.setAttribute("name","seq"+countForm);
                objInput.setAttribute("id","seq"+countForm);
        	    objInput.setAttribute("size","15");
                if(window.addEventListener) {// Mozilla, Netscape, Firefox
                    objInput.setAttribute("onKeyPress", "return numbersonly(event, false);");
                    objInput.setAttribute("onKeyUp", "return limit(this, 5);");
                } else {// IE
                    objInput.onkeypress=function numbersonly(e, decimal) {
                                                var key;
                                                var keychar;
                                                
                                                if(this.value.length>=5){
                                                    this.value=this.value.substring(0,4);
                                                }

                                                if (window.event) {
                                                    key = window.event.keyCode;
                                                } else if (e) {
                                                    key = e.which;
                                                } else {
                                                    return true;
                                                }

                                                keychar = String.fromCharCode(key);

                                                if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
                                                    return true;
                                                } else if ((("0123456789").indexOf(keychar) > -1)) {
                                                    return true;
                                                } else if (decimal && (keychar == ".")) { 
                                                    return true;
                                                } else
                                                    return false;
                                            }
                }
                
	            objInput.className = "button2";
    	newCell.appendChild(objInput);			

	    // End Seq

	    // Departure Date
	    var newRow   = dest.insertRow(-1);
    	    var newCell  = newRow.insertCell(-1);
        	    newCell.appendChild(document.createTextNode('Departure Date #'+numSeq));

        var newCell  = newRow.insertCell(-1);
            var objInputDD = document.createElement("input");
                objInputDD.setAttribute("name","dDate"+countForm);
                objInputDD.setAttribute("id","dDate"+countForm);
                objInputDD.setAttribute("size","15");
                objInputDD.className = "button2";
                if (document.all) {
                    objInputDD.onmouseover = (function(campo) { 
                                                            return function(){ 
                                                                callcal(campo,"1") 
                                                            } 
                                                        })
                                                        (countForm)
                }else{
                    objInputDD.setAttribute("onmouseover",'callcal('+countForm+',1)');
                }
        newCell.appendChild(objInputDD);			

            var objImg = document.createElement("img");
                objImg.src = "images/calendar.jpg";
        newCell.appendChild(objImg);
        //
		}
            
        if (x == '3') { 
            var newRow   = dest.insertRow(-1);
                var newCell  = newRow.insertCell(-1);
                    newCell.appendChild(document.createTextNode('Speaker Position #'+numSeq));

                var newCell  = newRow.insertCell(-1);
                    var objSelect = document.createElement("select");
                        objSelect.setAttribute("name","speaker"+countForm);
                        objSelect.className = "button2";

                    var objOption = document.createElement("option");
                        objOption.appendChild(document.createTextNode("Select"));
                        objSelect.appendChild(objOption);

                    var objOption = document.createElement("option");
                        objOption.appendChild(document.createTextNode("Yes"));
                        objOption.setAttribute("value","yes");
                
                    objSelect.appendChild(objOption);
                
                    var objOption = document.createElement("option");
                        objOption.appendChild(document.createTextNode("No"));
                        objOption.setAttribute("value","no");
                    objSelect.appendChild(objOption);

                newCell.appendChild(objSelect);
				
            // Datem

            var newRow   = dest.insertRow(-1);
                var newCell  = newRow.insertCell(-1);
                    //newCell.style.fontWeight = 'bold'
                    newCell.setAttribute("colspan","2");
                    newCell.appendChild(document.createTextNode('Trade To Date of Departure #'+numSeq));                        



            var newRow   = dest.insertRow(-1);
                var newCell  = newRow.insertCell(-1);
                    newCell.appendChild(document.createTextNode('Date #'+numSeq));

                var newCell  = newRow.insertCell(-1);
                    objTfild = document.createElement("input");
                        objTfild.setAttribute("id","datem"+countForm);
                        objTfild.setAttribute("name","datem"+countForm);
                        objTfild.setAttribute("size","15");					
                        objTfild.className = "button2";
                        if (document.all) {
                    objTfild.onmouseover = (function(campo) { 
                                                            return function(){ 
                                                                callcal(campo,"2") 
                                                            } 
                                                        })
                                                        (countForm)
                }else{
                    objTfild.setAttribute("onmouseover",'callcal('+countForm+',2)');
                }
                    newCell.appendChild(objTfild);
                
                    var objImg = document.createElement("img");
                        objImg.src = "images/calendar.jpg";
                    newCell.appendChild(objImg);
            // End Datem


				var newRow = dest.insertRow(-1);
					var newCell = newRow.insertCell(-1);
					newCell.innerHTML = "&nbsp;"
					newCell.setAttribute('colspan','2')
					
				var newRow = dest.insertRow(-1);
					var newCell = newRow.insertCell(-1);
					newCell.innerHTML = "<strong>Trip Type:  Will Fly</strong> (Add to box in order of preference):"
					newCell.setAttribute('colspan','2')

				var newRow = dest.insertRow(-1);
					var newCell = newRow.insertCell(-1);
					newCell.appendChild(document.createTextNode('Selections'));
					newCell.setAttribute('width','25%')
					newCell.setAttribute('valign','top')
					
					var newCell = newRow.insertCell(-1);

						newTable = document.createElement("table");
						newTable.setAttribute('border','0')
						newTRow = newTable.insertRow(-1);
							newTCell = newTRow.insertCell(-1);
							newSelect = document.createElement("select");
								newSelect.setAttribute('name','list'+((countForm * 2) - 1))
								newSelect.setAttribute('id','list'+((countForm * 2) - 1))
								newSelect.setAttribute('multiple','multiple')
								newSelect.setAttribute('size','12')
								newSelect.setAttribute('class','button2')
                                objInput.setAttribute("onmouseover",'callcal('+countForm+',1)');
                                newSelect.ondblclick = (function(lst,l1,l2,act) { 
                                                            return function(){ 
                                                                transfer(lst,l1,l2,act) 
                                                            } 
                                                        })
                                                        ('newRight'+((countForm * 2) - 1),'list'+((countForm * 2) - 1),'list'+(countForm * 2),"add")
							newTCell.appendChild(newSelect)
						newCell.appendChild(newTable)
						
						var a = 0;
						for (var key in tripType) {
							newSelect.options[a++] = new Option(tripType[key],key)
						}
						
						var newCell = newTRow.insertCell(-1)
							newCell.setAttribute('valign','middle')
							newCell.setAttribute('align','center')

							newButton = document.createElement('input')
							newButton.setAttribute('class','button')
							newButton.setAttribute('type','button')
							newButton.setAttribute('value','>>')
							if (document.all) {
								newButton.onclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+((countForm * 2) - 1),'list'+(countForm * 2),"add")  
							}	
							else { 
								newButton.setAttribute('onclick','transfer(\'newRight'+((countForm * 2) - 1)+'\',\'list'+((countForm * 2) - 1)+'\',\'list'+(countForm * 2)+'\',"add")') 
							}
						newCell.appendChild(newButton)



						newCell.appendChild(document.createElement('br'))
						newCell.appendChild(document.createElement('br'))
						
							newButton = document.createElement('input')
							newButton.setAttribute('class','button')
							newButton.setAttribute('type','button')
							newButton.setAttribute('value','<<')
							if (document.all) {
								newButton.onclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+(countForm * 2),'list'+((countForm * 2) - 1),"del")  
							}	
							else { 
								newButton.setAttribute('onclick','transfer(\'newRight'+((countForm * 2) - 1)+'\',\'list'+(countForm * 2)+'\',\'list'+((countForm * 2) - 1)+'\',"del")') 
							}
						newCell.appendChild(newButton)

						var newCell = newTRow.insertCell(-1)
							newSelect = document.createElement("select");
								newSelect.setAttribute('name','list'+(countForm * 2))
								newSelect.setAttribute('id','list'+(countForm * 2))
								newSelect.setAttribute('multiple','multiple')
								newSelect.setAttribute('size','12')
								newSelect.setAttribute('class','button2')
                                newSelect.ondblclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+(countForm * 2),'list'+((countForm * 2) - 1),"del")
							newCell.appendChild(newSelect)
							
						var newOpt = document.createElement('option')
							newOpt.appendChild(document.createTextNode('Add Selections'))
							newSelect.appendChild(newOpt)
							
						var newRow = dest.insertRow(-1)
							var newCell = newRow.insertCell(-1)
								newCell.setAttribute('width','25%')
								newCell.appendChild(document.createTextNode('You Selected:'))
							
							var newCell = newRow.insertCell(-1)
								newCell.setAttribute('width','75%')
									newInput = document.createElement('input')
										newInput.setAttribute('name','newRight'+((countForm * 2) - 1))
										newInput.setAttribute('id','newRight'+((countForm * 2) - 1))
										newInput.setAttribute('size','40')
										newInput.setAttribute('class','button2')
								newCell.appendChild(newInput)
							
						
						
						countForm++;
						/// Part 2
						
				var newRow = dest.insertRow(-1);
					var newCell = newRow.insertCell(-1);
					newCell.innerHTML = "<b>Trip Position</b> (Add to box in order of preference):"
					newCell.setAttribute('colspan','2')

				var newRow = dest.insertRow(-1);
					var newCell = newRow.insertCell(-1);
					newCell.appendChild(document.createTextNode('Selections'));
					newCell.setAttribute('width','25%')
					newCell.setAttribute('valign','top')
					
					var newCell = newRow.insertCell(-1);

						newTable = document.createElement("table");
						newTable.setAttribute('border','0')
						newTRow = newTable.insertRow(-1);
							newTCell = newTRow.insertCell(-1);
							newSelect = document.createElement("select");
								newSelect.setAttribute('name','list'+((countForm * 2) - 1))
								newSelect.setAttribute('id','list'+((countForm * 2) - 1))
								newSelect.setAttribute('multiple','multiple')
								newSelect.setAttribute('size','12')
								newSelect.setAttribute('class','button2')
                                newSelect.ondblclick = (function(lst,l1,l2,act) { 
                                                            return function(){ 
                                                                transfer(lst,l1,l2,act) 
                                                            } 
                                                        })
                                                        ('newRight'+((countForm * 2) - 1),'list'+((countForm * 2) - 1),'list'+(countForm * 2),"add")
							newTCell.appendChild(newSelect)
						newCell.appendChild(newTable)
						var a = 0;
						for (var key in tripPositions) {
							newSelect.options[a++] = new Option(tripPositions[key],key)
						} 

						var newCell = newTRow.insertCell(-1)
							newCell.setAttribute('valign','middle')
							newCell.setAttribute('align','center')

							newButton = document.createElement('input')
							newButton.setAttribute('class','button')
							newButton.setAttribute('type','button')
							newButton.setAttribute('value','>>')
							if (document.all) {
								newButton.onclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+((countForm * 2) - 1),'list'+(countForm * 2),"add")  
							}	
							else { 
								newButton.setAttribute('onclick','transfer(\'newRight'+((countForm * 2) - 1)+'\',\'list'+((countForm * 2) - 1)+'\',\'list'+(countForm * 2)+'\',"add")') 
							}
						newCell.appendChild(newButton)

						newCell.appendChild(document.createElement('br'))
						newCell.appendChild(document.createElement('br'))
						
							newButton = document.createElement('input')
							newButton.setAttribute('class','button')
							newButton.setAttribute('type','button')
							newButton.setAttribute('value','<<')
							if (document.all) {
								newButton.onclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+(countForm * 2),'list'+((countForm * 2) - 1),"del")  
							}	
							else { 
								newButton.setAttribute('onclick','transfer(\'newRight'+((countForm * 2) - 1)+'\',\'list'+(countForm * 2)+'\',\'list'+((countForm * 2) - 1)+'\',"del")') 
							}
						newCell.appendChild(newButton)

						var newCell = newTRow.insertCell(-1)
							newSelect = document.createElement("select");
								newSelect.setAttribute('name','list'+(countForm * 2))
								newSelect.setAttribute('id','list'+(countForm * 2))
								newSelect.setAttribute('multiple','multiple')
								newSelect.setAttribute('size','12')
								newSelect.setAttribute('class','button2')
                                newSelect.ondblclick = (function(lst,l1,l2,act) { return function(){ transfer(lst,l1,l2,act) } }) ('newRight'+((countForm * 2) - 1),'list'+(countForm * 2),'list'+((countForm * 2) - 1),"del")
							newCell.appendChild(newSelect)
							
						var newOpt = document.createElement('option')
							newOpt.appendChild(document.createTextNode('Add Selections'))
							newSelect.appendChild(newOpt)
							
						var newRow = dest.insertRow(-1)
							var newCell = newRow.insertCell(-1)
								newCell.setAttribute('width','25%')
								newCell.appendChild(document.createTextNode('You Selected:'))
							
							var newCell = newRow.insertCell(-1)
								newCell.setAttribute('width','75%')
									newInput = document.createElement('input')
										newInput.setAttribute('name','newRight'+((countForm * 2) - 1))
										newInput.setAttribute('id','newRight'+((countForm * 2) - 1))
										newInput.setAttribute('size','40')
										newInput.setAttribute('class','button2')
								newCell.appendChild(newInput)

        }

		// Pickup trade
		else if (x == '4')  {
			// Datem
            var newRow   = dest.insertRow(-1);
                var newCell  = newRow.insertCell(-1);
                    newCell.appendChild(document.createTextNode('Date '+countForm));

                var newCell  = newRow.insertCell(-1);
                    objTfild = document.createElement("input");
                        objTfild.setAttribute("id","datem"+((countForm * 2) - 1));
                        objTfild.setAttribute("name","datem"+((countForm * 2) - 1));
                        objTfild.setAttribute("size","15");					
                        objTfild.className = "button2";
                        objTfild.setAttribute("onmouseover",'callcal('+((countForm * 2) - 1)+',2)');
                    newCell.appendChild(objTfild);
					newCell.appendChild(document.createTextNode(' '));

                    var objImg = document.createElement("img");
                        objImg.src = "images/calendar.jpg";
                    newCell.appendChild(objImg);

					var newCell  = newRow.insertCell(-1);
                    	newCell.appendChild(document.createTextNode('to'));

	                var newCell  = newRow.insertCell(-1);
    	                objTfild = document.createElement("input");
        	                objTfild.setAttribute("id","datem"+(countForm * 2));
            	            objTfild.setAttribute("name","datem"+(countForm * 2));
                	        objTfild.setAttribute("size","15");					
                    	    objTfild.className = "button2";
	                        objTfild.setAttribute("onmouseover",'callcal('+(countForm * 2)+',2)');
    	                newCell.appendChild(objTfild);
						newCell.appendChild(document.createTextNode(' '));
	
                    var objImg = document.createElement("img");
						objImg.src = "images/calendar.jpg";
                    newCell.appendChild(objImg);

            // End Datem
		}

}