$(document).ready(function(){


	/*$("#nature_of_enquiry").change(function () {
          $("select option:selected").each(function () {
               var selectid = $(this).attr("value"); 
          if (selectid=='1') {
          	$("#contactus_one").show();
          } else {
          	$("#contactus_one").hide();
          }

          if (selectid=='2') {
          	$("#contactus_two").show();
				$("input[@type='radio']").click( function() {
				   var radval = $(this).val();
				   if (radval == '1') {
				   	$("#contactus_two_a").show();
				   } else {
				   	$("#contactus_two_a").hide();	   	
				   }
				   if (radval == '0') {
				   	$("#contactus_two_b").show();
				   } else {
				   	$("#contactus_two_b").hide();	   	
				   }	
					
				});          	
          } else {
          	$("#contactus_two").hide();
          }
          if (selectid=='4') {
          	$("#contactus_three").show();
				$("input[@type='radio']").click( function() {
				   var radval = $(this).val();
				   if (radval == '1') {
				   	$("#contactus_three_a").show();
				   } else {
				   	$("#contactus_three_a").hide();	   	
				   }
				   if (radval == '0') {
				   	$("#contactus_three_b").show();
				   } else {
				   	$("#contactus_three_b").hide();	   	
				   }	
					
				});             	
          } else {
          	$("#contactus_three").hide();
          }
          
          if (selectid=='5') {
          	$("#contactus_four").show();
          } else {
          	$("#contactus_four").hide();
          }          
          
          if ((selectid!='')&&(selectid!='1')&&(selectid!='2')&&(selectid!='4')&&(selectid!='5')) {
          	$("#contactus_default").show();
          } else {
          	$("#contactus_default").hide();
          }            
          
          });
          

          
        }) */

	
		$("#job_application input[@type='radio']").click( function() {
			   var radval = $(this).val();
			   var radname = $(this).attr("name");
			   if (radname=='located') {			   
				  if (radval == 'No') { $("#tbl_" + radname + "").show(); } else { $("#tbl_" + radname + "").hide(); }							   
			   } else {
			   	  if (radval == 'Yes') { $("#tbl_" + radname + "").show(); } else { $("#tbl_" + radname + "").hide(); }
			   }
		}); 

		$("#job_application input[@type='checkbox']").click( function() {
			   var radval = $(this).val();
		 
			   if ($(this).attr('checked') && radval=='Other') {			     
				 	$("#tbl_other").show();			   				   				 				   						
			   } else if (radval=='Other') {
			   		$("#tbl_other").hide();		
			   }

			   if ($(this).attr('checked') && radval=='Recruitment Website') {			     
			   	 	$("#tbl_rw").show();			   						 			   	 		   				  
			   } else if (radval=='Recruitment Website') {
			   		$("#tbl_rw").hide();
			   }
		}); 

		
});




//different way at display the hidden contact us divs (using selectedIndex) (I didn't know how to do it with jQuery)
function show_contact_us_fields(select_obj){
	
	var selectid = select_obj.selectedIndex
	
	if (selectid=='1') {
      	$("#contactus_one").show();
      } else {
      	$("#contactus_one").hide();
      }

      if (selectid=='2') {
      	$("#contactus_two").show();
			$("input[@type='radio']").click( function() {
			   var radval = $(this).val();
			   if (radval == 'Yes') {
			   	$("#contactus_two_a").show();
			   } else {
			   	$("#contactus_two_a").hide();	   	
			   }
			   if (radval == 'No') {
			   	$("#contactus_two_b").show();
			   } else {
			   	$("#contactus_two_b").hide();	   	
			   }	
				
			});          	
      } else {
      	$("#contactus_two").hide();
      }
      if (selectid=='4') {
      	$("#contactus_three").show();
			$("input[@type='radio']").click( function() {
			   var radval = $(this).val();
			   if (radval == 'Yes') {
			   	$("#contactus_three_a").show();
			   } else {
			   	$("#contactus_three_a").hide();	   	
			   }
			   if (radval == 'No') {
			   	$("#contactus_three_b").show();
			   } else {
			   	$("#contactus_three_b").hide();	   	
			   }	
				
			});             	
      } else {
      	$("#contactus_three").hide();
      }
      
      if (selectid=='5') {
      	$("#contactus_four").show();
      } else {
      	$("#contactus_four").hide();
      }          
      
      if ((selectid!='')&&(selectid!='1')&&(selectid!='2')&&(selectid!='4')&&(selectid!='5')) {
      	$("#contactus_default").show();
      } else {
      	$("#contactus_default").hide();
      }            
      
}

function show_job_app_fields(select_obj){
	
	var selectid = select_obj.selectedIndex;

	
	if (selectid=='1') {
      	$("#jobapp_one").show();
    } else {
      	$("#jobapp_one").hide();
    }

    if (selectid=='3' || selectid=='4') {
      	$("#jobapp_two").show();     	
      } else {
      	$("#jobapp_two").hide();
      }
   
      
}

var upload_number = 1;
function addFileInput() {
 	var d = document.createElement("div");
 	var file = document.createElement("input");
 	
 	file.setAttribute("type", "file");
 	file.setAttribute("name", "attachment"+upload_number);
 	file.setAttribute("id", "attachment"+upload_number);
 	file.setAttribute("onchange","get_file_info(this)");
 	d.appendChild(file);    
 	document.getElementById("moreUploads").appendChild(d);

 	upload_number++;

}

function get_file_info(this_obj) {	
	var filename = this_obj.value;
	var d = document.createElement("div");
 	var i = document.createElement("input");
 	var removebtn = document.createElement("a");
	var x = document.getElementById("toUpload");
 	var objid = this_obj.id;
 	
	var filelength = (parseInt(filename.length) - 3);
	var fileext = filename.substring(filelength,filelength + 3);
 
	// Check file extenstion
	if (fileext.toLowerCase() != "doc" &&  fileext.toLowerCase() != "pdf"){
	validext = false;
	alert("You can only upload .doc and .pdf files");
	this_obj.value = "";
	} else {
	validext = true;
	document.getElementById('moreUploadsLink').style.display = 'block';
	}
	
 	if (validext) {
	 	if (document.getElementById("fname"+objid) != undefined) {
	 		document.getElementById("fname"+objid).value = filename;
	 	} else {
	 	i.setAttribute("type","input");
	 	i.setAttribute("name","fname"+objid);
	 	i.setAttribute("id","fname"+objid);
	 	i.setAttribute("value",filename);
	// 	i.setAttribute("style","background: none;border:0;");
	
	    var removeText=document.createTextNode('Remove');

	 	removebtn.setAttribute("class","fname"+objid);
	 	removebtn.setAttribute("href","javascript:removeFile(this);");
	 	//removebtn.setAttribute("onclick","removeFile(this);");
		
		removebtn.appendChild(removeText);		
	 	d.appendChild(i);
	 	d.appendChild(removebtn);

	 	x.appendChild(d); 	
	 	}
 	}

}

function removeFile(fileobj) {
	var o = fileobj.className;
	alert(o);
	//var olength = parseInt(o.length);
	//var oname = o.substring(5,olength);

    //document.getElementById(oname).value = "";
	
	fileobj.parentNode.parentNode.removeChild(fileobj.parentNode);
	
}

function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */


	
	this.addElement = function( element ){

		
			
			
		
		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file'){

			// Element name -- what number am I?
			element.name = 'file_' + this.id++;
			element.id = 'file_' + this.id++;
			// Add reference to this object
			element.multi_selector = this;

			
			// What to do when a file is selected
			element.onchange = function(){
				
			if (element != undefined) {
				var validext;	
				var filename = element.value;
				var filelength = (parseInt(filename.length) - 3);
				var filelength2 = (parseInt(filename.length) - 4);
				var fileext =  filename.substring(filelength,filelength + 3);
				var fileext2 =  filename.substring(filelength2,filelength2 + 4);
				// Check file extenstion
							
				if (fileext.toLowerCase() != "doc" &&  fileext.toLowerCase() != "pdf" && fileext2.toLowerCase() != "docx" && fileext2.toLowerCase() != "jpeg" && fileext.toLowerCase() != "gif" && fileext.toLowerCase() != "jpg" && fileext.toLowerCase() != "png"){
									
					validext = false;
					alert("You are only allowed to upload .doc,.pdf,.jpg,.gif & .png files");
				//	document.getElementById("my_file_element").value = "";	
				} else {
					validext = true;
								
				}
			}
				if (validext) {	
					// New file input
					var new_element = document.createElement( 'input' );
					new_element.type = 'file';
	
					// Add new element
					this.parentNode.insertBefore( new_element, this );
	
					// Apply 'update' to element
					this.multi_selector.addElement( new_element );
	
					// Update list
					this.multi_selector.addListRow( this );
	
					// Hide this: we can't use display:none because Safari doesn't like it
					this.style.position = 'absolute';
					this.style.left = '-1000px';
				}
			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};
			
			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;

		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};


	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );

		var new_row_button = document.createElement( 'a' );
		var removeText=document.createTextNode('Remove');

		new_row_button.setAttribute("class","rembtn") 
	 	new_row_button.setAttribute("className","rembtn");
	 	new_row_button.setAttribute("href","#");
	 		 	
		new_row_button.appendChild(removeText);		
		
		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			if (this.parentNode.element.multi_selector.current_element != undefined)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		new_row.innerHTML = element.value;

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		

			
		};

};